Skip to content

Commit 74ed604

Browse files
committed
Added the find function
1 parent 90b1c3d commit 74ed604

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/Data/Array.purs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ module Data.Array
5252
, (!!), index
5353
, elemIndex
5454
, elemLastIndex
55+
, find
5556
, findIndex
5657
, findLastIndex
5758
, insertAt
@@ -280,6 +281,12 @@ elemIndex x = findIndex (_ == x)
280281
elemLastIndex :: forall a. Eq a => a -> Array a -> Maybe Int
281282
elemLastIndex x = findLastIndex (_ == x)
282283

284+
-- | Find the first element for which a predicate holds.
285+
find :: forall a. (a -> Boolean) -> Array a -> Maybe a
286+
find predicate arr = do
287+
idx <- findIndex predicate arr
288+
index arr idx
289+
283290
-- | Find the first index for which a predicate holds.
284291
findIndex :: forall a. (a -> Boolean) -> Array a -> Maybe Int
285292
findIndex = findIndexImpl Just Nothing

test/Test/Data/Array.purs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Prelude
55
import Control.Monad.Eff (Eff)
66
import Control.Monad.Eff.Console (log, CONSOLE)
77

8-
import Data.Array (range, foldM, unzip, zip, zipWithA, zipWith, intersectBy, intersect, (\\), deleteBy, delete, unionBy, union, nubBy, nub, groupBy, group', group, span, dropWhile, drop, takeWhile, take, sortBy, sort, catMaybes, mapMaybe, mapWithIndex, filterM, filter, concat, concatMap, reverse, alterAt, modifyAt, updateAt, deleteAt, insertAt, findLastIndex, findIndex, elemLastIndex, elemIndex, (!!), uncons, init, tail, last, head, insertBy, insert, snoc, (:), length, null, singleton, fromFoldable)
8+
import Data.Array (range, foldM, unzip, zip, zipWithA, zipWith, intersectBy, intersect, (\\), deleteBy, delete, unionBy, union, nubBy, nub, groupBy, group', group, span, dropWhile, drop, takeWhile, take, sortBy, sort, catMaybes, mapMaybe, mapWithIndex, filterM, filter, concat, concatMap, reverse, alterAt, modifyAt, updateAt, deleteAt, insertAt, findLastIndex, find, findIndex, elemLastIndex, elemIndex, (!!), uncons, init, tail, last, head, insertBy, insert, snoc, (:), length, null, singleton, fromFoldable)
99
import Data.Foldable (for_, foldMapDefaultR, class Foldable, all)
1010
import Data.Maybe (Maybe(..), isNothing, fromJust)
1111
import Data.Tuple (Tuple(..))
@@ -128,6 +128,10 @@ testArray = do
128128
assert $ (elemLastIndex 1 [1, 2, 1]) == Just 2
129129
assert $ (elemLastIndex 4 [1, 2, 1]) == Nothing
130130

131+
log "find should return the item that a predicate returns true for in an array"
132+
assert $ (find (_ /= "foo") ["foo", "bar", "baz"]) == Just "bar"
133+
assert $ (find (_ == "qux") ["foo", "bar", "baz"]) == Nothing
134+
131135
log "findIndex should return the index of an item that a predicate returns true for in an array"
132136
assert $ (findIndex (_ /= 1) [1, 2, 1]) == Just 1
133137
assert $ (findIndex (_ == 3) [1, 2, 1]) == Nothing

0 commit comments

Comments
 (0)