Convenience functions for working with Array
Drop n first elements from 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 5 (fromList (List.range 0 9)) == fromList [ 5, 6, 7, 8, 9 ]
sliceFrom -3 (fromList (List.range 0 9)) == fromList [ 7, 8, 9 ]Take n first elements from 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 5 (fromList (List.range 0 9)) == fromList [ 0, 1, 2, 3, 4 ]
sliceUntil -3 (fromList (List.range 0 9)) == fromList [ 0, 1, 2, 3, 4, 5, 6 ]Remove the last element from an array.
pop (fromList [ 1 2 3 ]) == fromList [ 1 2 ]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]Apply an array of functions to an array of values.
Apply a function to the array, collecting the result in a List. This is useful for building HTML out of an array:
Html.text : String -> Html msg
mapToList Html.text : Array String -> List (Html msg)Apply a function to the array with the index as the first argument, collecting the results 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 two arrays, combining them with the given function. If one array is longer, the extra elements are dropped.
map2 (+) [1,2,3] [1,2,3,4] == [2,4,6]
map2 (,) [1,2,3] ['a','b'] == [ (1,'a'), (2,'b') ]
pairs : Array a -> Array b -> Array (a,b)
pairs lefts rights =
map2 (,) lefts rightsTake a predicate and an array, return an array that contains elements which fails to satisfy the predicate.
This is equivalent to Array.filter (not << predicate) list
removeWhen isEven [ 1, 2, 3, 4 ] == [ 1, 3 ]Zip arrays into tuples
Resize an array from the left, padding the right-hand side with the given value.
Resize an array from the right, padding the left-hand side with the given value.
Resize an array from the left, padding the right-hand side with the given index function.
Resize an array from the right, padding the left-hand side with the given index function.
Split an array into two arrays, the first ending at and the second starting at the given index
Remove the element at the given index
Insert an element at the given index.
insertAt 0 'b' (fromList [ 'a', 'c' ]) == fromList [ 'b', 'a', 'c' ]
insertAt 1 'b' (fromList [ 'a', 'c' ]) == fromList [ 'a', 'b', 'c' ]
insertAt -1 'b' (fromList [ 'a', 'c' ]) == fromList [ 'a', 'c' ]
insertAt 10 'b' (fromList [ 'a', 'c' ]) == fromList [ 'a', 'c' ]
Update the element at the index using a function. Returns the array unchanged if the index is out of bounds.