Skip to content

Commit 88a531e

Browse files
committed
Add decent mapMaybe implementation
1 parent ee26d59 commit 88a531e

File tree

2 files changed

+4
-14
lines changed

2 files changed

+4
-14
lines changed

README.md

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

5454
map :: forall a b. (a -> b) -> [a] -> [b]
5555

56+
mapMaybe :: forall a b. (a -> Maybe b) -> [a] -> [b]
57+
5658
nub :: forall a. (Eq a) => [a] -> [a]
5759

5860
nubBy :: forall a. (a -> a -> Prim.Boolean) -> [a] -> [a]

src/Data/Array.purs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -200,20 +200,8 @@ foreign import map
200200
\ };\
201201
\}" :: forall a b. (a -> b) -> [a] -> [b]
202202

203-
foreign import mapMaybe
204-
"function mapMaybe (f) {\
205-
\ return function (arr) {\
206-
\ var l = arr.length, n = 0;\
207-
\ var result = new Array();\
208-
\ for (var i = 0; i < l; i++) {\
209-
\ var x = f(arr[i]);\
210-
\ if (x.ctor === 'Data.Maybe.Just') {\
211-
\ result[n++] = x.values[0];\
212-
\ }\
213-
\ }\
214-
\ return result;\
215-
\ };\
216-
\}" :: forall a b. (a -> Maybe b) -> [a] -> [b]
203+
mapMaybe :: forall a b. (a -> Maybe b) -> [a] -> [b]
204+
mapMaybe f = concatMap (maybe [] singleton <<< f)
217205

218206
foreign import filter
219207
"function filter (f) {\

0 commit comments

Comments
 (0)