elm-community/array-extra/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

Array.Extra

Convenience functions for working with Array

Transformations

update : Int -> (a -> a) -> Array a -> Array a

Update the element at the index using a function. Returns the array unchanged if the index is out of bounds.

update  1 ((+)10) (fromList [1,2,3]) == fromList [1,12,3]
update  4 ((+)10) (fromList [1,2,3]) == fromList [1,2,3]
update -1 ((+)10) (fromList [1,2,3]) == fromList [1,2,3]
sliceFrom : Int -> Array a -> Array a

Drop n first elements from an array. In other words, slice an array from an index until the very end. Given negative argument, count the end of the slice from the end of the array.

sliceFrom  5 (fromList [0..9]) == fromList [5,6,7,8,9]
sliceFrom -3 (fromList [0..9]) == fromList [7,8,9]
sliceUntil : Int -> Array a -> Array a

Take n first elements from an array. In other words, slice an array from the very beginning until index not including. Given negative argument, count the beginning of the slice from the end of the array.

sliceUntil  5 (fromList [0..9]) == fromList [0,1,2,3,4]
sliceUntil -3 (fromList [0..9]) == fromList [0,1,2,3,4,5,6]

Higher order helpers

filterMap : (a -> Maybe b) -> Array a -> Array b

Apply a function that may succeed to all values in the array, but only keep the successes.

String.toInt : String -> Maybe Int
filterMap String.toInt (fromList ["3", "4.0", "5", "hats"]) == fromList [3,5]
apply : Array (a -> b) -> Array a -> Array b

Apply an array of functions to an array of values.

map2 : (a -> b -> result) -> Array a -> Array b -> Array result

Combine two arrays, combining them with the given function. If one array is longer, the extra elements are dropped.

map2 (+) [1,2,3] [1,2,3,4] == [2,4,6]
map2 (,) [1,2,3] ['a','b'] == [ (1,'a'), (2,'b') ]
pairs : Array a -> Array b -> Array (a,b)
pairs lefts rights =
    map2 (,) lefts rights
map3 : (a -> b -> c -> result) -> Array a -> Array b -> Array c -> Array result
(a -> b -> c -> d -> result)
-> Array a
-> Array b
-> Array c
-> Array d
-> Array result
(a -> b -> c -> d -> e -> result)
-> Array a
-> Array b
-> Array c
-> Array d
-> Array e
-> Array result
removeWhen : (a -> Bool) -> Array a -> Array a

Take a predicate and an array, return an array that contains elements which fails to satisfy the predicate. This is equivalent to Array.filter (not << predicate) list

removeWhen isEven [1,2,3,4] == [1,3]

Zips

zip : Array a -> Array b -> Array ( a, b )

Zip arrays into tuples

zip3 : Array a -> Array b -> Array c -> Array ( a, b, c )
zip4 : Array a -> Array b -> Array c -> Array d -> Array ( a, b, c, d )
Array a
-> Array b
-> Array c
-> Array d
-> Array e
-> Array ( a, b, c, d, e )

Slicing / resizing

resizelRepeat : Int -> a -> Array a -> Array a

Resize an array from the left, padding the right-hand side with the given value.

resizerRepeat : Int -> a -> Array a -> Array a

Resize an array from the right, padding the left-hand side with the given value.

resizelIndexed : Int -> (Int -> a) -> Array a -> Array a

Resize an array from the left, padding the right-hand side with the given index function.

resizerIndexed : Int -> (Int -> a) -> Array a -> Array a

Resize an array from the right, padding the left-hand side with the given index function.

splitAt : Int -> Array a -> ( Array a, Array a )

Split an array into two arrays, the first ending at and the second starting at the given index

removeAt : Int -> Array a -> Array a

Remove the element at the given index

Unsafe

getUnsafe : Int -> Array a -> a

Unsafe version of get, don't use this unless you know what you're doing!