Array operations that can be applied in either direction.
Set the element at an index in a direction.
import LinearDirection exposing (LinearDirection(..))
import Array
Array.fromList [ "I", "am", "ok" ]
|> Array.Linear.replaceAt 2 FirstToLast "confusion"
--> Array.fromList [ "I", "am", "confusion" ]
Array.fromList [ "I", "am", "ok" ]
|> Array.Linear.replaceAt 1 LastToFirst "feel"
--> Array.fromList [ "I", "feel", "ok" ]If the index is out of range, the array is unaltered.
import LinearDirection exposing (LinearDirection(..))
import Array
Array.fromList [ "I", "am", "ok" ]
|> Array.Linear.replaceAt -1 FirstToLast "is"
--> Array.fromList [ "I", "am", "ok" ]Kick an element out of an Array at a given index in a direction.
import LinearDirection exposing (LinearDirection(..))
import Array
Array.fromList [ 'a', 'a', 'b' ]
|> Array.Linear.removeAt 1 FirstToLast
--> Array.fromList [ 'a', 'b' ]
Array.fromList [ 'a', 'b', 'c', 'd' ]
|> Array.Linear.removeAt 0 LastToFirst
--> Array.fromList [ 'a', 'b', 'c' ]If the index is out of bounds, nothing is changed.
import LinearDirection exposing (LinearDirection(..))
import Array
Array.Linear.removeAt -1 FirstToLast
(Array.fromList [ 'a', 'a', 'b' ])
--> Array.fromList [ 'a', 'a', 'b' ]
Array.Linear.removeAt 100 FirstToLast
(Array.fromList [ 'a', 'a', 'b' ])
--> Array.fromList [ 'a', 'a', 'b' ]Put an element in an Array at a given index in a direction.
import LinearDirection exposing (LinearDirection(..))
import Array
Array.fromList [ 'a', 'c', 'd' ]
|> Array.Linear.insertAt 1 FirstToLast 'b'
--> Array.fromList [ 'a', 'b', 'c', 'd' ]
Array.fromList [ 'a', 'c', 'd' ]
|> Array.Linear.insertAt 2 LastToFirst 'b'
--> Array.fromList [ 'a', 'b', 'c', 'd' ]If the index is out of bounds, nothing is inserted.
import LinearDirection exposing (LinearDirection(..))
import Array
Array.fromList [ 'a', 'c', 'd' ]
|> Array.Linear.insertAt -1 FirstToLast 'b'
--> Array.fromList [ 'a', 'c', 'd' ]
Array.fromList [ 'a', 'c', 'd' ]
|> Array.Linear.insertAt 4 FirstToLast 'b'
--> Array.fromList [ 'a', 'c', 'd' ]squeezeInAt allows inserting a whole Array of elements.
Resize an Array in a direction, always padding with the same given value.
import LinearDirection exposing (LinearDirection(..))
import Array
Array.fromList [ 1, 2 ]
|> Array.Linear.padToLength 4 LastToFirst 0
--> Array.fromList [ 0, 0, 1, 2 ]
Array.fromList [ 1, 2, 3 ]
|> Array.Linear.padToLength 2 LastToFirst 0
--> Array.fromList [ 2, 3 ]
Array.fromList [ 1, 2 ]
|> Array.Linear.padToLength 4 FirstToLast 0
--> Array.fromList [ 1, 2, 0, 0 ]
Array.fromList [ 1, 2, 3 ]
|> Array.Linear.padToLength 2 FirstToLast 0
--> Array.fromList [ 1, 2 ]The result is an empty array if the index is negative.
import LinearDirection exposing (LinearDirection(..))
Array.fromList [ 1, 2 ]
|> Array.Linear.padToLength -1 LastToFirst 0
--> Array.emptyPut elements of a given Array between the elements left and right to the given index in a direction.
import LinearDirection exposing (LinearDirection(..))
import Array
Array.fromList [ 'a', 'd', 'e' ]
|> Array.Linear.squeezeInAt 1
FirstToLast
(Array.fromList [ 'b', 'c' ])
--> Array.fromList [ 'a', 'b', 'c', 'd', 'e' ]
Array.fromList [ 'a', 'd', 'e' ]
|> Array.Linear.squeezeInAt 2
LastToFirst
(Array.fromList [ 'b', 'c' ])
--> Array.fromList [ 'a', 'b', 'c', 'd', 'e' ]If the index is out of bounds, nothing is inserted.
import LinearDirection exposing (LinearDirection(..))
import Array
Array.fromList [ 'a', 'd', 'e' ]
|> Array.Linear.squeezeInAt -1
FirstToLast
(Array.fromList [ 'b', 'c' ])
--> Array.fromList [ 'a', 'd', 'e' ]
Array.fromList [ 'a', 'd', 'e' ]
|> Array.Linear.squeezeInAt 4
FirstToLast
(Array.fromList [ 'b', 'c' ])
--> Array.fromList [ 'a', 'd', 'e' ]insertAt allows inserting a single element.
A certain number of elements from one side.
import LinearDirection exposing (LinearDirection(..))
import Array
Array.fromList [ 1, 2, 3, 4, 5, 6 ]
|> Array.Linear.take 4 FirstToLast
|> Array.Linear.take 2 LastToFirst
--> Array.fromList [ 3, 4 ]
Array.fromList [ 1, 2, 3 ]
|> Array.Linear.take 100 FirstToLast
--> Array.fromList [ 1, 2, 3 ]
Array.fromList [ 1, 2, 3 ]
|> Array.Linear.take -100 FirstToLast
--> Array.emptyAfter a certain number of elements from one side.
import LinearDirection exposing (LinearDirection(..))
import Array
Array.fromList [ 1, 2, 3, 4, 5, 6 ]
|> Array.Linear.drop 2 LastToFirst
--> Array.fromList [ 1, 2, 3, 4 ]
Array.fromList [ 1, 2, 3 ]
|> Array.Linear.drop 100 FirstToLast
--> Array.emptyNothing is dropped if the index is negative.
import LinearDirection exposing (LinearDirection(..))
import Array
Array.fromList [ 1, 2, 3 ]
|> Array.Linear.drop -1 FirstToLast
--> Array.fromList [ 1, 2, 3 ]Split the Array into equal-sized chunks. The elements left over on one side are in less.
import LinearDirection exposing (LinearDirection(..))
import Array
Array.fromList [ 1, 2, 3, 4, 5, 6, 7 ]
|> Array.Linear.toChunksOf 3 FirstToLast
--> { chunks =
--> Array.fromList
--> [ Array.fromList [ 1, 2, 3 ]
--> , Array.fromList [ 4, 5, 6 ]
--> ]
--> , remainder = Array.fromList [ 7 ]
--> }
Array.fromList [ 1, 2, 3, 4, 5, 6, 7 ]
|> Array.Linear.toChunksOf 3 LastToFirst
--> { remainder = Array.fromList [ 1 ]
--> , chunks =
--> Array.fromList
--> [ Array.fromList [ 2, 3, 4 ]
--> , Array.fromList [ 5, 6, 7 ]
--> ]
--> }Reduce an Array in a direction.
import LinearDirection exposing (LinearDirection(..))
import Array
Array.fromList [ 'l', 'i', 'v', 'e' ]
|> Array.Linear.foldFrom "" FirstToLast String.cons
--> "evil"
Array.fromList [ 'l', 'i', 'v', 'e' ]
|> Array.Linear.foldFrom "" LastToFirst String.cons
--> "live"
Justthe element at an index in a direction.Nothing, if the index is out of range.