elm-community/array-extra/2.4.0

NOTE: the latest version is 2.6.0

Array.Extra

Convenience functions for working with Array

Transform

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 ]
mapToList : (a -> b) -> Array a -> List b

Apply a function to the elements in the array and collect the result in a List.

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 elements in the array with their indices as the first argument and collect the result 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 []

Combine

apply : Array (a -> b) -> Array a -> Array b

Apply an array of functions to an array of values. If one array is longer, its extra elements are not used.

apply
    (fromList
        [ \x -> -x, identity, (+) 10 ]
    )
    (repeat 5 100)
    == fromList [ -100, 100, 110 ]
map2 : (a -> b -> combined) -> Array a -> Array b -> Array combined

Combine the elements of two arrays with the given function. If one array is longer, its extra elements are not used.

map2 (+)
    (fromList [ 1, 2, 3 ])
    (fromList [ 1, 2, 3, 4 ])
    == fromList [ 2, 4, 6 ]


map2 Tuple.pair
    (fromList [ 1, 2, 3 ])
    (fromList [ 'a', 'b' ])
    == fromList [ ( 1, 'a' ), ( 2, 'b' ) ]

Note: zip can be used instead of map2 Tuple.pair.

map3 : (a -> b -> c -> combined) -> Array a -> Array b -> Array c -> Array combined

Combine the elements of three arrays with the given function. See map2.

Note: zip3 can be used instead of map3 (\a b c -> ( a, b, c )).

(a -> b -> c -> d -> combined)
-> Array a
-> Array b
-> Array c
-> Array d
-> Array combined

Combine the elements of four arrays with the given function. See map2.

(a -> b -> c -> d -> e -> combined)
-> Array a
-> Array b
-> Array c
-> Array d
-> Array e
-> Array combined

Combine the elements of five arrays with the given function. See map2.

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

Zip the elements of two arrays into tuples. If one array is longer, its extra elements are not used.

zip
    (fromList [ 1, 2, 3 ])
    (fromList [ 'a', 'b' ])
    == fromList [ ( 1, 'a' ), ( 2, 'b' ) ]
zip3 : Array a -> Array b -> Array c -> Array ( a, b, c )

Zip the elements of three arrays into 3-tuples. Only the indices of the shortest array are used.

zip3
    (fromList [ 1, 2, 3 ])
    (fromList [ 'a', 'b' ])
    (fromList [ "a", "b", "c", "d" ])
    == fromList
        [ ( 1, 'a', "a" )
        , ( 2, 'b', "b" )
        ]
unzip : Array ( a, b ) -> ( Array a, Array b )

Unzip an array of tuples into a tuple containing one array with the first and one with the second values.

unzip
    (fromList
        [ ( 1, 'a' ), ( 2, 'b' ), ( 3, 'c' ) ]
    )
    == ( fromList [ 1, 2, 3 ]
       , fromList [ 'a', 'b', 'c' ]
       )

Modify

pop : Array a -> Array a

Remove the last element from an array.

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


pop empty
    == empty
removeAt : Int -> Array a -> Array a

Remove the element at the given index. If the index is out of bounds, nothing is changed.

removeAt 2 (fromList [ 1, 2, 3, 4 ])
    == fromList [ 1, 2, 4 ]


removeAt -1 (fromList [ 1, 2, 3, 4 ])
    == fromList [ 1, 2, 3, 4 ]


removeAt 100 (fromList [ 1, 2, 3, 4 ])
    == fromList [ 1, 2, 3, 4 ]
insertAt : Int -> a -> Array a -> Array a

Insert an element at the given index. If the index is out of bounds, nothing is changed.

insertAt 1 'b' (fromList [ 'a', 'c' ])
    == fromList [ 'a', 'b', 'c' ]


insertAt -1 'b' (fromList [ 'a', 'c' ])
    == fromList [ 'a', 'c' ]


insertAt 100 'b' (fromList [ 'a', 'c' ])
    == fromList [ 'a', 'c' ]
update : Int -> (a -> a) -> Array a -> Array a

Update the element at the index based on its current value. If the index is out of bounds, nothing is changed.

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 ]
removeWhen : (a -> Bool) -> Array a -> Array a

Return an array that only contains elements which fail to satisfy the predicate. This is equivalent to Array.filter (not << predicate).

removeWhen isEven
    (fromList [ 1, 2, 3, 4 ])
    == fromList [ 1, 3 ]
reverse : Array a -> Array a

Reverse an array.

reverse (fromList [ 1, 2, 3, 4 ])
    == fromList [ 4, 3, 2, 1 ]
intersperse : a -> Array a -> Array a

Place a value between all members of the given array.

intersperse "on"
    (fromList [ "turtles", "turtles", "turtles" ])
    == fromList
        [ "turtles", "on", "turtles", "on", "turtles" ]

Slice

sliceFrom : Int -> Array a -> Array a

Drop a number of elements from the start of 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 3 (fromList (List.range 0 6))
    == fromList [ 3, 4, 5, 6 ]


sliceFrom -3 (fromList (List.range 0 6))
    == fromList [ 4, 5, 6 ]
sliceUntil : Int -> Array a -> Array a

Take a number of elements from the start of 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 3 (fromList (List.range 0 6))
    == fromList [ 0, 1, 2 ]


sliceUntil -3 (fromList (List.range 0 6))
    == fromList [ 0, 1, 2, 3 ]
splitAt : Int -> Array a -> ( Array a, Array a )

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

splitAt 2 (fromList [ 1, 2, 3, 4 ])
    == ( fromList [ 1, 2 ], fromList [ 3, 4 ] )


splitAt 100 (fromList [ 1, 2, 3, 4 ])
    == ( fromList [ 1, 2, 3, 4 ], empty )


splitAt -1 (fromList [ 1, 2, 3, 4 ])
    == ( empty, fromList [ 1, 2, 3, 4 ] )

Resize

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

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

resizelRepeat 4 0 (fromList [ 1, 2 ])
    == fromList [ 1, 2, 0, 0 ]


resizelRepeat 2 0 (fromList [ 1, 2, 3 ])
    == fromList [ 1, 2 ]


resizelRepeat -1 0 (fromList [ 1, 2 ])
    == empty
resizerRepeat : Int -> a -> Array a -> Array a

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

resizerRepeat 4 0 (fromList [ 1, 2 ])
    == fromList [ 0, 0, 1, 2 ]


resizerRepeat 2 0 (fromList [ 1, 2, 3 ])
    == fromList [ 2, 3 ]


resizerRepeat -1 0 (fromList [ 1, 2 ])
    == empty
resizelIndexed : Int -> (Int -> a) -> Array a -> Array a

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

resizelIndexed 5
    toLetterInAlphabet
    (fromList [ 'a', 'b', 'c' ])
    == fromList [ 'a', 'b', 'c', 'd', 'e' ]


resizelIndexed 2
    toLetterInAlphabet
    (fromList [ 'a', 'b', 'c' ])
    == fromList [ 'a', 'b' ]


resizelIndexed -1
    toLetterInAlphabet
    (fromList [ 'a', 'b', 'c' ])
    == empty


toLetterInAlphabet : Int -> Char
toLetterInAlphabet inAlphabet =
    (Char.toCode 'a') + inAlphabet
        |> Char.fromCode
resizerIndexed : Int -> (Int -> a) -> Array a -> Array a

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

resizerIndexed 5
    ((*) 5)
    (fromList [ 10, 25, 36 ])
    == fromList [ 0, 5, 10, 25, 36 ]


resizerIndexed 2
    ((*) 5)
    (fromList [ 10, 25, 36 ])
    == fromList [ 25, 36 ]


resizerIndexed -1
    ((*) 5)
    (fromList [ 10, 25, 36 ])
    == empty

Scan

all : (a -> Bool) -> Array a -> Bool

Whether all elements satisfy a test.

all isEven (fromList [ 2, 4 ])
    == True


all isEven (fromList [ 2, 3 ])
    == False


all isEven empty
    == True
any : (a -> Bool) -> Array a -> Bool

Whether any elements satisfy a test.

any isEven (fromList [ 1, 3 ])
--> True


any isEven (fromList [ 1, 2 ])
--> False


any isEven empty
--> False