Skip to content

Commit 2e00d86

Browse files
committed
Merge pull request #3 from purescript/lookup
lookup function
2 parents c81a68d + c438706 commit 2e00d86

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

README.md

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

4444
for_ :: forall a b f m. (Functor m, Applicative m, Foldable f) => f a -> (a -> m b) -> m { }
4545

46+
lookup :: forall a b f. (Eq a, Foldable f) => a -> f (Tuple a b) -> Maybe b
47+
4648
mconcat :: forall f m. (Foldable f, Monoid m) => f m -> m
4749

4850
notElem :: forall a f. (Eq a, Foldable f) => a -> f a -> Prim.Boolean

src/Data/Foldable.purs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Data.Either
66
import Data.Eq
77
import Data.Maybe
88
import Data.Monoid
9+
import Data.Monoid.First
910
import Data.Tuple
1011

1112
class Foldable f where
@@ -97,6 +98,9 @@ find :: forall a f. (Foldable f) => (a -> Boolean) -> f a -> Maybe a
9798
find p f = case foldMap (\x -> if p x then [x] else []) f of
9899
(x:_) -> Just x
99100
[] -> Nothing
101+
102+
lookup :: forall a b f. (Eq a, Foldable f) => a -> f (Tuple a b) -> Maybe b
103+
lookup a f = runFirst $ foldMap (\(Tuple a' b) -> First (if a == a' then Just b else Nothing)) f
100104

101105
foreign import foldrArray
102106
"function foldrArray(f) {\

0 commit comments

Comments
 (0)