Skip to content

Commit 90b1c3d

Browse files
authored
Merge pull request #68 from damncabbage/topic/mapWithIndex
Adds mapWithIndex (map with a generated index)
2 parents 8259242 + 2727de4 commit 90b1c3d

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/Data/Array.purs

+9
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ module Data.Array
6868
, filterM
6969
, mapMaybe
7070
, catMaybes
71+
, mapWithIndex
7172

7273
, sort
7374
, sortBy
@@ -411,6 +412,14 @@ mapMaybe f = concatMap (maybe [] singleton <<< f)
411412
catMaybes :: forall a. Array (Maybe a) -> Array a
412413
catMaybes = mapMaybe id
413414

415+
-- | Apply a function to each element in an array, supplying a generated
416+
-- | zero-based index integer along with the element, creating an array
417+
-- | with the new elements.
418+
mapWithIndex :: forall a b. (Int -> a -> b) -> Array a -> Array b
419+
mapWithIndex f xs =
420+
zipWith f (range 0 (length xs - 1)) xs
421+
422+
414423
--------------------------------------------------------------------------------
415424
-- Sorting ---------------------------------------------------------------------
416425
--------------------------------------------------------------------------------

test/Test/Data/Array.purs

+4-1
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, 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, 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(..))
@@ -201,6 +201,9 @@ testArray = do
201201
log "catMaybe should take an array of Maybe values and throw out Nothings"
202202
assert $ catMaybes [Nothing, Just 2, Nothing, Just 4] == [2, 4]
203203

204+
log "mapWithIndex applies a function with an index for every element"
205+
assert $ mapWithIndex (\i x -> x - i) [9,8,7,6,5] == [9,7,5,3,1]
206+
204207
log "sort should reorder a list into ascending order based on the result of compare"
205208
assert $ sort [1, 3, 2, 5, 6, 4] == [1, 2, 3, 4, 5, 6]
206209

0 commit comments

Comments
 (0)