Skip to content

Commit b5cc736

Browse files
authored
Merge pull request #6 from garyb/fromFoldableWithIndex
Add `fromFoldableWithIndex` for `Map`
2 parents cfebb6d + 0f01752 commit b5cc736

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/Data/Map.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Data.Map
55

66
import Prelude
77

8-
import Data.Map.Internal (Map, alter, checkValid, delete, empty, filter, filterKeys, filterWithKey, findMax, findMin, foldSubmap, fromFoldable, fromFoldableWith, insert, isEmpty, isSubmap, lookup, lookupGE, lookupGT, lookupLE, lookupLT, member, pop, showTree, singleton, size, submap, toUnfoldable, toUnfoldableUnordered, union, unionWith, unions, difference, update, values, mapMaybeWithKey, mapMaybe)
8+
import Data.Map.Internal (Map, alter, checkValid, delete, empty, filter, filterKeys, filterWithKey, findMax, findMin, foldSubmap, fromFoldable, fromFoldableWith, fromFoldableWithIndex, insert, isEmpty, isSubmap, lookup, lookupGE, lookupGT, lookupLE, lookupLT, member, pop, showTree, singleton, size, submap, toUnfoldable, toUnfoldableUnordered, union, unionWith, unions, difference, update, values, mapMaybeWithKey, mapMaybe)
99
import Data.Set (Set)
1010
import Unsafe.Coerce (unsafeCoerce)
1111

src/Data/Map/Internal.purs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module Data.Map.Internal
2020
, submap
2121
, fromFoldable
2222
, fromFoldableWith
23+
, fromFoldableWithIndex
2324
, toUnfoldable
2425
, toUnfoldableUnordered
2526
, delete
@@ -46,7 +47,7 @@ import Prelude
4647

4748
import Data.Eq (class Eq1)
4849
import Data.Foldable (foldl, foldMap, foldr, class Foldable)
49-
import Data.FoldableWithIndex (class FoldableWithIndex, foldrWithIndex)
50+
import Data.FoldableWithIndex (class FoldableWithIndex, foldlWithIndex, foldrWithIndex)
5051
import Data.FunctorWithIndex (class FunctorWithIndex, mapWithIndex)
5152
import Data.List (List(..), (:), length, nub)
5253
import Data.List.Lazy as LL
@@ -555,6 +556,10 @@ fromFoldableWith f = foldl (\m (Tuple k v) -> alter (combine v) k m) empty where
555556
combine v (Just v') = Just $ f v v'
556557
combine v Nothing = Just v
557558

559+
-- | Convert any indexed foldable collection into a map.
560+
fromFoldableWithIndex :: forall f k v. Ord k => FoldableWithIndex k f => f v -> Map k v
561+
fromFoldableWithIndex = foldlWithIndex (\k m v -> insert k v m) empty
562+
558563
-- | Convert a map to an unfoldable structure of key/value pairs where the keys are in ascending order
559564
toUnfoldable :: forall f k v. Unfoldable f => Map k v -> f (Tuple k v)
560565
toUnfoldable m = unfoldr go (m : Nil) where

0 commit comments

Comments
 (0)