elm-community/array-extra/2.5.0

NOTE: the latest version is 2.6.0

Array.Extra

Convenience functions for working with Array

scan

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

Whether all elements satisfy a given test.

import Array exposing (fromList, empty)


all (\x -> x < 5) (fromList [ 2, 4 ])
--> True


all (\x -> x < 5) (fromList [ 4, 16 ])
--> False


all (\x -> x < 5) empty
--> True
any : (a -> Bool) -> Array a -> Bool

Whether at least some elements satisfy a given test.

import Array exposing (fromList, empty)


any (\x -> x < 5) (fromList [ 6, 3 ])
--> True


any (\x -> x < 5) (fromList [ 12, 33 ])
--> False


any (\x -> x < 5) empty
--> False

alter

reverse : Array a -> Array a

Flip the element order.

import Array exposing (fromList)


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

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

import Array exposing (fromList)


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 ]
pop : Array a -> Array a

Remove the last element.

import Array exposing (fromList, empty)


pop (fromList [ 1, 2, 3 ])
--> fromList [ 1, 2 ]


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

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

import Array exposing (fromList)


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

Only keep elements which fail to satisfy a given predicate. This is equivalent to Array.filter (not << predicate).

import Array exposing (fromList)


removeWhen (\x -> x < 0)
    (fromList [ -1, 92, 0, 14, -3 ])
--> fromList [ 92, 0, 14 ]
insertAt : Int -> a -> Array a -> Array a

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

import Array exposing (fromList)


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' ]
intersperse : a -> Array a -> Array a

Place a value between all members.

import Array exposing (fromList)


intersperse "on"
    (fromList [ "turtles", "turtles", "turtles" ])
--> fromList
-->     [ "turtles", "on", "turtles", "on", "turtles" ]

To interlace an Array, interweave.

part

sliceFrom : Int -> Array a -> Array a

Drop a given number of elements from the start. In other words, slice the Array from an index until the very end. Given a negative argument, count the end of the slice from the end.

import Array exposing (fromList)


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. In other words, slice the Array from the very beginning until not including the index. Given a negative argument, count the beginning of the slice from the end.

import Array exposing (fromList)


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 into two Arrays, the first ending before and the second starting with a given index.

import Array exposing (fromList, empty)


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 ] )

combine

interweave : Array element -> Array element -> Array element

Place all elements of a given Array between all current elements. Extra elements of either Array are glued to the end without anything in between.

import Array exposing (fromList, repeat)


fromList [ "turtles", "turtles", "turtles" ]
    |> interweave (repeat 2 "on")
--> fromList [ "turtles", "on", "turtles", "on", "turtles" ]


fromList [ "turtles", "turtles", "turtles" ]
    |> interweave (repeat 5 "on")
--> fromList [ "turtles", "on", "turtles", "on", "turtles", "on", "on", "on" ]


fromList [ "turtles", "turtles", "turtles" ]
    |> interweave (repeat 1 "on")
--> fromList [ "turtles", "on", "turtles", "turtles" ]
apply : Array (a -> b) -> Array a -> Array b

Apply a given Array of changes to all elements. If one Array is longer, its extra elements are not used.

import Array exposing (fromList, repeat)


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 a given function. If one Array is longer, its extra elements are not used.

import Array exposing (fromList)


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 )

Combine the elements of two Arrays into tuples. If one is longer, its extra elements are not used.

import Array exposing (fromList)


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 indexes of the shortest Array are used.

import Array exposing (fromList)


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 )

Split all tuple elements into a tuple of one Array with the first and one with the second values.

import Array exposing (fromList)


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

resize

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

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

import Array exposing (fromList, empty)


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 from the right, padding the left-hand side with a given value.

import Array exposing (fromList, empty)


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 from the left, padding the right-hand side with a given value based on index.

import Array exposing (fromList, empty)


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 from the right, padding the left-hand side with a given value based on index.

import Array exposing (fromList, empty)


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

transform

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

Try transforming all elements but only keep the successes.

import Array exposing (fromList)


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.

import Array exposing (fromList)
import Html


mapToList Html.text
    (fromList [ "a", "b", "c" ])
--> [ Html.text "a", Html.text "b", Html.text "c" ]
indexedMapToList : (Int -> a -> b) -> Array a -> List b

Transform all elements with their indexes as the first argument and collect the result in a List.

import Array exposing (Array, fromList)
import Html exposing (Html)


type alias Exercise =
    { name : String }


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


exercisesRender : Array Exercise -> Html msg
exercisesRender exercises =
    indexedMapToList renderExercise exercises
        |> Html.div []