Convenience functions for working with Array
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
--> FalseFlip the element order.
import Array exposing (fromList)
reverse (fromList [ 1, 2, 3, 4 ])
--> fromList [ 4, 3, 2, 1 ]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 ]Remove the last element.
import Array exposing (fromList, empty)
pop (fromList [ 1, 2, 3 ])
--> fromList [ 1, 2 ]
pop empty
--> emptyRemove 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 ]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 ]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' ]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.
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 ]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 ]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 ] )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 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 ]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.
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" )
--> ]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 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 ])
--> emptyResize 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 ])
--> emptyResize 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.fromCodeResize 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 ])
--> emptyTry transforming all elements but only keep the successes.
import Array exposing (fromList)
filterMap String.toInt
(fromList [ "3", "4.0", "5", "hats" ])
--> fromList [ 3, 5 ]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" ]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 []
Whether all elements satisfy a given test.