Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@

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

find :: forall a. (a -> Prim.Boolean) -> [a] -> Maybe a

findIndex :: forall a. (a -> Prim.Boolean) -> [a] -> Prim.Number

head :: forall a. [a] -> Maybe a

init :: forall a. [a] -> Maybe [a]
Expand Down
17 changes: 17 additions & 0 deletions src/Data/Array.purs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ module Data.Array
, updateAt
, concatMap
, filter
, findIndex
, find
, range
, zipWith
, nub
Expand Down Expand Up @@ -203,6 +205,21 @@ foreign import map
mapMaybe :: forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe f = concatMap (maybe [] singleton <<< f)

foreign import findIndex
"function findIndex (f) {\
\ return function (arr) {\
\ for (var i = 0, l = arr.length; i < l; i++) {\
\ if (f(arr[i])) {\
\ return i;\
\ }\
\ }\
\ return -1;\
\ };\
\}" :: forall a. (a -> Boolean) -> [a] -> Number

find :: forall a. (a -> Boolean) -> [a] -> Maybe a
find f xs = xs !! findIndex f xs

foreign import filter
"function filter (f) {\
\ return function (arr) {\
Expand Down