Skip to content

Add findMapWithIndex #166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions src/Data/Array.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,20 @@ exports.findLastIndexImpl = function (just) {
};
};

exports.findMapWithIndexImpl = function (nothing) {
return function (isJust) {
return function (f) {
return function (xs) {
for (var i = 0, l = xs.length; i < l; i++) {
var y = f(i)(xs[i]);
if (isJust(y)) return y;
}
return nothing;
};
};
};
};

exports._insertAt = function (just) {
return function (nothing) {
return function (i) {
Expand Down
20 changes: 20 additions & 0 deletions src/Data/Array.purs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ module Data.Array
, findMap
, findIndex
, findLastIndex
, findMapWithIndex
, insertAt
, deleteAt
, updateAt
Expand Down Expand Up @@ -499,6 +500,25 @@ foreign import findLastIndexImpl
-> Array a
-> Maybe Int

-- | Find the first element which satisfies a predicate mapping.
-- | The mapping function has access to both index and value at the index.
-- |
-- | ```purescript
-- | findMapWithIndex (\i x -> if contains (Pattern "b") x then Just {i,x} else Nothing) ["a", "bb", "b", "d"] = Just { i: 1, x: "bb" }
-- | findMapWithIndex (\i x -> if contains (Pattern "x") x then Just {i,x} else Nothing) ["a", "bb", "b", "d"] = Nothing
-- | ```
-- |
findMapWithIndex :: forall a b. (Int -> a -> Maybe b) -> Array a -> Maybe b
findMapWithIndex = findMapWithIndexImpl Nothing isJust

foreign import findMapWithIndexImpl
:: forall a r
. (forall b. Maybe b)
-> (forall b. Maybe b -> Boolean)
-> (Int -> a -> Maybe r)
-> Array a
-> Maybe r

-- | Insert an element at the specified index, creating a new array, or
-- | returning `Nothing` if the index is out of bounds.
-- |
Expand Down
4 changes: 4 additions & 0 deletions test/Test/Data/Array.purs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,10 @@ testArray = do
, [4,0,0,1,25,36,458,5842,23757]
]

log "findMapWithIndex should return the value returned for the first item for which the given function returned a Just"
assert $ (A.findMapWithIndex (\i x -> if x /= 1 then Just { index: i, value: x } else Nothing) [1, 2, 1]) == Just { index: 1, value: 2 }
assert $ (A.findMapWithIndex (\i x -> if x == 3 then Just { index: i, value: x } else Nothing) [1, 2, 1]) == Nothing

nea :: Array ~> NEA.NonEmptyArray
nea = unsafePartial fromJust <<< NEA.fromArray

Expand Down