elm-community/array-extra/2.1.0

NOTE: the latest version is 2.6.0

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 (List.range 0 9)) == fromList [ 5, 6, 7, 8, 9 ]


sliceFrom -3 (fromList (List.range 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 (List.range 0 9)) == fromList [ 0, 1, 2, 3, 4 ]


sliceUntil -3 (fromList (List.range 0 9)) == fromList [ 0, 1, 2, 3, 4, 5, 6 ]
pop : Array a -> Array a

Remove the last element from an array.

pop (fromList [ 1 2 3 ]) == fromList [ 1 2 ]

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.

mapToList : (a -> b) -> Array a -> List b

Apply a function to the array, collecting the result in a List. This is useful for building HTML out of an array:

Html.text : String -> Html msg
mapToList Html.text : Array String -> List (Html msg)
indexedMapToList : (Int -> a -> b) -> Array a -> List b

Apply a function to the array with the index as the first argument, collecting the results in a List.

type alias Exercise = { name : String }


renderExercise : Int -> Exercise -> Html msg
renderExercise index exercise =
    String.concat
        [ "Exercise #"
        , String.fromInt (index + 1)
        , " - "
        , exercise.name
        ]
        |> Html.text


renderExercises : Array Exercise -> Html msg
renderExercises exercises =
    indexedMapToList renderExercise exercises
        |> Html.div []
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 )

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