indique/elm-linear-direction/1.0.3

NOTE: the latest version is 2.0.0

LinearDirection.Array

Array operations that can be applied in either direction.

-> (element -> result -> result)
-> result
-> Array element
-> result

Reduce an Array in a direction.

Array.fromList [ "l", "i", "v", "e" ]
    |> Array.fold FirstToLast (++) ""
--> "live"


Array.fromList [ "l", "i", "v", "e" ]
    |> Array.fold LastToFirst (++) ""
--> "evil"
take : Int -> LinearDirection -> Array a -> Array a

A certain number of elements from one side.

Array.fromList [ 1, 2, 3, 4, 5, 6 ]
    |> Array.take 4 FirstToLast
    |> Array.take 2 LastToFirst
--> Array.fromList [ 3, 4 ]


Array.fromList [ 1, 2, 3 ]
    |> Array.take 100
--> Array.fromList [ 1, 2, 3 ]
at : Int -> LinearDirection -> Array a -> Maybe a

Just the element at an index in a direction.

Array.fromList [ "lose", "win", "lose" ]
    |> Array.at 0 LastToFirst
--> Just "lose"


Array.fromList [ 1, 2, 3 ]
    |> Array.at 0 FirstToLast
--> Just "lose"

Nothing, if the index is out of range.

Array.fromList [ 1, 2, 3 ]
    |> Array.at -1
--> Nothing
replaceAt : Int -> LinearDirection -> element -> Array element -> Array element

Set the element at an index in a direction.

Array.fromList [ "I", "am", "ok" ]
    |> Array.replaceAt 2 FirstToLast "confusion"
--> Array.fromList [ "I", "am", "confusion" ]


Array.fromList [ "I", "am", "ok" ]
    |> Array.replaceAt 1 LastToFirst "feel"
--> Array.fromList [ "I", "feel", "ok" ]

If the index is out of range, the array is unaltered.

Array.fromList [ "I", "am", "ok" ]
    |> Array.replaceAt -1 FirstToLast "is"
--> Array.fromList [ "I", "am", "ok" ]
removeAt : Int -> LinearDirection -> Array a -> Array a

Kick an element out of an Array at a given index in a direction.

Array.fromList [ 'a', 'a', 'b' ]
    |> Array.removeAt 1 FirstToLast
--> Array.fromList [ 'a', 'b', 'c', 'd' ]
Array.fromList [ 'a', 'b', 'c', 'd' ]
    |> Array.removeAt 0 LastToFirst
--> Array.fromList [ 'a', 'b', 'c' ]

If the index is out of bounds, the Array is unaltered.

Array.fromList [ 'a', 'a', 'b' ]
    |> Array.removeAt -1 FirstToLast
--> Array.fromList [ 'a', 'a', 'b' ]
insertAt : Int -> LinearDirection -> a -> Array a -> Array a

Put an element in an Array at a given index in a direction.

Array.fromList [ 'a', 'c', 'd' ]
    |> Array.insertAt 1 FirstToLast 'b'
--> Array.fromList [ 'a', 'b', 'c', 'd' ]
Array.fromList [ 'a', 'c', 'd' ]
    |> Array.insertAt 2 LastToFirst 'b'
--> Array.fromList [ 'a', 'b', 'c', 'd' ]

If the index is out of bounds, nothing gets inserted.

Array.fromList [ 'a', 'c', 'd' ]
    |> Array.insertAt -1 'b'
--> Array.fromList [ 'a', 'c', 'd' ]
Array.fromList [ 'a', 'c', 'd' ]
    |> Array.insertAt 10 'b'
--> Array.fromList [ 'a', 'c', 'd' ]