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

Commit 354a34b

Browse files
committed
Add Foldable StrMap instance
1 parent 3668e60 commit 354a34b

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@
140140

141141
instance eqStrMap :: (P.Eq a) => P.Eq (StrMap a)
142142

143+
instance foldableStrMap :: Foldable StrMap
144+
143145
instance functorStrMap :: P.Functor StrMap
144146

145147
instance semigroupStrMap :: (P.Semigroup a) => P.Semigroup (StrMap a)

src/Data/StrMap.purs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import qualified Data.Array as A
3737
import Data.Maybe
3838
import Data.Function
3939
import Data.Tuple
40-
import Data.Foldable (foldl)
40+
import Data.Foldable (Foldable, foldl, foldr)
4141
import Data.Monoid
4242
import Data.Monoid.All
4343

@@ -55,8 +55,6 @@ foreign import _fmapStrMap
5555
instance functorStrMap :: P.Functor StrMap where
5656
(<$>) f m = runFn2 _fmapStrMap m f
5757

58-
-- It would be nice to have a Foldable instance, but we're essentially unordered
59-
6058
foreign import _foldM
6159
"function _foldM(bind) {\
6260
\ return function(f) {\
@@ -83,6 +81,11 @@ foldMap f = fold (\acc k v -> acc P.<> f k v) mempty
8381
foldM :: forall a m z. (P.Monad m) => (z -> String -> a -> m z) -> z -> StrMap a -> m z
8482
foldM f z = _foldM P.(>>=) f (P.pure z)
8583

84+
instance foldableStrMap :: Foldable StrMap where
85+
foldl f = fold (\z _ -> f z)
86+
foldr f z m = foldr f z (values m)
87+
foldMap f = foldMap (P.const f)
88+
8689
-- Unfortunately the above are not short-circuitable (consider using purescript-machines)
8790
-- so we need special cases:
8891

0 commit comments

Comments
 (0)