Convenience functions for working with Array
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)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 []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 ]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.
Combine the elements of four arrays with the given function. See map2.
Combine the elements of five arrays with the given function. See map2.
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' ) ]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 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' ]
)Remove the last element from an array.
pop (fromList [ 1, 2, 3 ])
== fromList [ 1, 2 ]
pop empty
== emptyRemove 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 ]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 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 ]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 an array.
reverse (fromList [ 1, 2, 3, 4 ])
== fromList [ 4, 3, 2, 1 ]Place a value between all members of the given array.
intersperse "on"
(fromList [ "turtles", "turtles", "turtles" ])
== fromList
[ "turtles", "on", "turtles", "on", "turtles" ]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 ]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 ]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 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 ])
== emptyResize 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 ])
== emptyResize 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.fromCodeResize 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 ])
== emptyWhether all elements satisfy a test.
all isEven (fromList [ 2, 4 ])
== True
all isEven (fromList [ 2, 3 ])
== False
all isEven empty
== TrueWhether any elements satisfy a test.
any isEven (fromList [ 1, 3 ])
--> True
any isEven (fromList [ 1, 2 ])
--> False
any isEven empty
--> False
Apply a function that may succeed to all values in the array, but only keep the successes.