Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

Commit c673935

Browse files
authored
Merge pull request #76 from sammthomson/tounfoldable
More efficient toUnfoldable without the Ord constraint
2 parents 338ccdb + 2fa4b8b commit c673935

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/Data/Map.purs

+9-7
Original file line numberDiff line numberDiff line change
@@ -385,13 +385,15 @@ toList (Two left k v right) = toList left <> Tuple k v : toList right
385385
toList (Three left k1 v1 mid k2 v2 right) = toList left <> Tuple k1 v1 : toList mid <> Tuple k2 v2 : toList right
386386

387387
-- | Convert a map to an unfoldable structure of key/value pairs
388-
toUnfoldable :: forall f k v. (Ord k, Unfoldable f) => Map k v -> f (Tuple k v)
389-
toUnfoldable = unfoldr go
390-
where
391-
go :: Map k v -> Maybe (Tuple (Tuple k v) (Map k v))
392-
go Leaf = Nothing
393-
go (Two left k v right) = Just $ Tuple (Tuple k v) (left <> right)
394-
go (Three left k1 v1 mid k2 v2 right) = Just $ Tuple (Tuple k1 v1) (insert k2 v2 (left <> mid <> right))
388+
toUnfoldable :: forall f k v. Unfoldable f => Map k v -> f (Tuple k v)
389+
toUnfoldable m = unfoldr go (m : Nil) where
390+
go Nil = Nothing
391+
go (hd : tl) = case hd of
392+
Leaf -> go tl
393+
Two left k v right ->
394+
Just $ Tuple (Tuple k v) (left : right : tl)
395+
Three left k1 v1 mid k2 v2 right ->
396+
Just $ Tuple (Tuple k1 v1) (singleton k2 v2 : left : mid : right : tl)
395397

396398
-- | Get a list of the keys contained in a map
397399
keys :: forall k v. Map k v -> List k

0 commit comments

Comments
 (0)