Convenience functions for working with Array
Whether at least some elements satisfy a given test.
import Array exposing (fromList, empty)
fromList [ 6, 3 ] |> any (\x -> x < 5)
--> True
fromList [ 12, 33 ] |> any (\x -> x < 5)
--> False
empty |> any (\x -> x < 5)
--> FalseWhether a given value is contained.
import Array exposing (fromList)
fromList [ "Leonardo", "Michelangelo", "Donatello", "Raphael" ]
|> member "Donatello"
--> True
fromList [ "Leonardo", "Michelangelo" ]
|> member "Raphael"
--> FalseFor checking if some aspect is present, use any.
Flip the element order.
import Array exposing (fromList)
fromList [ 1, 2, 3, 4 ] |> reverse
--> fromList [ 4, 3, 2, 1 ]Place a value between all elements.
import Array exposing (fromList)
fromList [ "turtles", "turtles", "turtles" ]
|> intersperse "on"
--> fromList
--> [ "turtles", "on", "turtles", "on", "turtles" ]To interlace an Array, interweave.
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)
fromList [ 1, 2, 3 ] |> update 1 (\n -> n + 10)
--> fromList [ 1, 12, 3 ]
fromList [ 1, 2, 3 ] |> update 4 (\n -> n + 10)
--> fromList [ 1, 2, 3 ]
fromList [ 1, 2, 3 ] |> update -1 (\n -> n + 10)
--> fromList [ 1, 2, 3 ]Remove the last element.
import Array exposing (fromList, empty)
fromList [ 1, 2, 3 ] |> pop
--> fromList [ 1, 2 ]
empty |> pop
--> emptyRemove the element at a given index. If the index is out of bounds, nothing is changed.
import Array exposing (fromList)
fromList [ 1, 2, 3, 4 ] |> removeAt 2
--> fromList [ 1, 2, 4 ]
fromList [ 1, 2, 3, 4 ] |> removeAt -1
--> fromList [ 1, 2, 3, 4 ]
fromList [ 1, 2, 3, 4 ] |> removeAt 100
--> fromList [ 1, 2, 3, 4 ]Insert an element at a given index. If the index is out of bounds, nothing is changed.
import Array exposing (fromList)
fromList [ 'a', 'c' ] |> insertAt 1 'b'
--> fromList [ 'a', 'b', 'c' ]
fromList [ 'a', 'c' ] |> insertAt -1 'b'
--> fromList [ 'a', 'c' ]
fromList [ 'a', 'c' ] |> insertAt 100 'b'
--> fromList [ 'a', 'c' ]Only keep elements which fail to satisfy a given predicate.
This is equivalent to Array.filter (not << predicate).
import Array exposing (fromList)
fromList [ -1, 92, 0, 14, -3 ]
|> removeWhen (\x -> x < 0)
--> fromList [ 92, 0, 14 ]Try transforming all elements but only keep the successes.
import Array exposing (fromList)
fromList [ "3", "4.0", "5", "hats" ]
|> filterMap String.toInt
--> fromList [ 3, 5 ]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)
fromList (List.range 0 6) |> sliceFrom 3
--> fromList [ 3, 4, 5, 6 ]
fromList (List.range 0 6) |> sliceFrom -3
--> fromList [ 4, 5, 6 ]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)
fromList (List.range 0 6) |> sliceUntil 3
--> fromList [ 0, 1, 2 ]
fromList (List.range 0 6) |> sliceUntil -3
--> fromList [ 0, 1, 2, 3 ]Split into two Arrays, the first ending before and the second starting with a given index.
import Array exposing (fromList, empty)
fromList [ 1, 2, 3, 4 ] |> splitAt 2
--> ( fromList [ 1, 2 ], fromList [ 3, 4 ] )
fromList [ 1, 2, 3, 4 ] |> splitAt 100
--> ( fromList [ 1, 2, 3, 4 ], empty )
fromList [ 1, 2, 3, 4 ] |> splitAt -1
--> ( empty, fromList [ 1, 2, 3, 4 ] )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' ]
--> )Return an array that contains elements from the two provided, in alternate order. If one array runs out of items, append the items from the remaining array.
import Array exposing (fromList, repeat)
interweave_ (fromList [ "turtles", "turtles", "turtles" ]) (repeat 2 "on")
--> fromList [ "turtles", "on", "turtles", "on", "turtles" ]
interweave_ (fromList [ "turtles", "turtles", "turtles" ]) (repeat 5 "on")
--> fromList [ "turtles", "on", "turtles", "on", "turtles", "on", "on", "on" ]
interweave_ (fromList [ "turtles", "turtles", "turtles" ]) (repeat 1 "on")
--> fromList [ "turtles", "on", "turtles", "turtles" ]See documentation for interweave to find out why the strange name.
Map functions taking multiple arguments over multiple arrays. Each array should be of the same length; extra elements are dropped.
import Array exposing (Array)
toIntFunctions : Array (Float -> Int)
toIntFunctions =
Array.fromList
[ round
, floor
, ceiling
, truncate
]
toIntFunctions
|> andMap (Array.fromList [ -1.5, -1.5, -1.5, -1.5 ])
--> Array.fromList [ -1, -2, -1, -1 ]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 (\a b -> a + b)
(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.
Combine the elements of four Arrays with the given function. See map2.
Combine the elements of five Arrays with the given function. See map2.
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' ) ]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" )
--> ]Resize from the left, padding the right-hand side with a given value.
import Array exposing (fromList, empty)
fromList [ 1, 2 ] |> resizelRepeat 4 0
--> fromList [ 1, 2, 0, 0 ]
fromList [ 1, 2, 3 ] |> resizelRepeat 2 0
--> fromList [ 1, 2 ]
fromList [ 1, 2 ] |> resizelRepeat -1 0
--> emptyResize from the right, padding the left-hand side with a given value.
import Array exposing (fromList, empty)
fromList [ 1, 2 ] |> resizerRepeat 4 0
--> fromList [ 0, 0, 1, 2 ]
fromList [ 1, 2, 3 ] |> resizerRepeat 2 0
--> fromList [ 2, 3 ]
fromList [ 1, 2 ] |> resizerRepeat -1 0
--> emptyResize from the left, padding the right-hand side with a given value based on index.
import Array exposing (fromList, empty)
fromList [ 'a', 'b', 'c' ]
|> resizelIndexed 5 toLetterInAlphabet
--> fromList [ 'a', 'b', 'c', 'd', 'e' ]
fromList [ 'a', 'b', 'c' ]
|> resizelIndexed 2 toLetterInAlphabet
--> fromList [ 'a', 'b' ]
fromList [ 'a', 'b', 'c' ]
|> resizelIndexed -1 toLetterInAlphabet
--> empty
toLetterInAlphabet : Int -> Char
toLetterInAlphabet inAlphabet =
Char.fromCode ((Char.toCode 'a') + inAlphabet)Resize from the right, padding the left-hand side with a given value based on index.
import Array exposing (fromList, empty)
fromList [ 10, 25, 36 ]
|> resizerIndexed 5 (\n -> n * 5)
--> fromList [ 0, 5, 10, 25, 36 ]
fromList [ 10, 25, 36 ]
|> resizerIndexed 2 (\n -> n * 5)
--> fromList [ 25, 36 ]
fromList [ 10, 25, 36 ]
|> resizerIndexed -1 (\n -> n * 5)
--> emptyApply a function to the elements in the array and collect the result in a List.
import Array exposing (fromList)
import Html
fromList [ "a", "b", "c" ]
|> mapToList Html.text
--> [ Html.text "a", Html.text "b", Html.text "c" ]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 =
indexedMapToList renderExercise
>> Html.div []These functions are deprecated and will be removed in the next major version of this library.
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)
repeat 5 100
|> apply
(fromList
[ \x -> -x, identity, (+) 10 ]
)
--> fromList [ -100, 100, 110 ]@deprecated in favour of Array.Extra.andMap (note the reversed argument order).
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" ]@deprecated Beware: For historical reasons, this function takes it's arguments in the opposite order to List.Extra.interweave.
As such, this function is deprecated in v1 and will be removed in v2. You should switch to interweave_ which has the correct argument order. We plan to re-introduce interweave with the new argument order in a future release, but since the chance of subtle breakage is quite high, we will only do this gradually.
Whether all elements satisfy a given test.