Array operations that can be applied in either direction.
Reduce an Array in a direction.
import Linear exposing (DirectionLinear(..))
import Array
Array.fromList [ 'l', 'i', 'v', 'e' ]
|> Array.Linear.foldFrom ( "", Up, String.cons )
--> "evil"
Array.fromList [ 'l', 'i', 'v', 'e' ]
|> Array.Linear.foldFrom ( "", Down, String.cons )
--> "live"Kick an element out of an Array at a given index in a direction.
import Linear exposing (DirectionLinear(..))
import Array
Array.fromList [ 'a', 'a', 'b' ]
|> Linear.at ( Up, 1 )
|> Array.Linear.remove
--> Array.fromList [ 'a', 'b' ]
Array.fromList [ 'a', 'b', 'c', 'd' ]
|> Linear.at ( Down, 0 )
|> Array.Linear.remove
--> Array.fromList [ 'a', 'b', 'c' ]If the index is out of bounds, nothing is changed.
import Linear exposing (DirectionLinear(..))
import Array
Array.fromList [ 'a', 'a', 'b' ]
|> Linear.at ( Up, -1 )
|> Array.Linear.remove
--> Array.fromList [ 'a', 'a', 'b' ]
Array.fromList [ 'a', 'a', 'b' ]
|> Linear.at ( Up, 100 )
|> Array.Linear.remove
--> Array.fromList [ 'a', 'a', 'b' ]Set the element at an index in a direction.
import Linear exposing (DirectionLinear(..))
import Array
Array.fromList [ "I", "am", "ok" ]
|> Linear.at ( Up, 2 )
|> Array.Linear.replaceWith (\() -> "confusion")
--> Array.fromList [ "I", "am", "confusion" ]
Array.fromList [ "I", "am", "ok" ]
|> Linear.at ( Down, 1 )
|> Array.Linear.replaceWith (\() -> "feel")
--> Array.fromList [ "I", "feel", "ok" ]If the index is out of range, the Array is unaltered.
import Linear exposing (DirectionLinear(..))
import Array
Array.fromList [ "I", "am", "ok" ]
|> Linear.at ( Up, -1 )
|> Array.Linear.replaceWith (\() -> "is")
--> Array.fromList [ "I", "am", "ok" ]Put an element in an Array at a given index in a direction.
import Linear exposing (DirectionLinear(..))
import Array
Array.fromList [ 'a', 'c', 'd' ]
|> Linear.at ( Up, 1 )
|> Array.Linear.insert (\() -> 'b')
--> Array.fromList [ 'a', 'b', 'c', 'd' ]
Array.fromList [ 'a', 'c', 'd' ]
|> Linear.at ( Down, 2 )
|> Array.Linear.insert (\() -> 'b')
--> Array.fromList [ 'a', 'b', 'c', 'd' ]If the index is out of bounds, nothing is inserted.
import Linear exposing (DirectionLinear(..))
import Array
Array.fromList [ 'a', 'c', 'd' ]
|> Linear.at ( Up, -1 )
|> Array.Linear.insert (\() -> 'b')
--> Array.fromList [ 'a', 'c', 'd' ]
Array.fromList [ 'a', 'c', 'd' ]
|> Linear.at ( Up, 4 )
|> Array.Linear.insert (\() -> 'b')
--> Array.fromList [ 'a', 'c', 'd' ]squeezeIn allows inserting a whole Array of elements.
Pad an Array in a direction.
import Linear exposing (DirectionLinear(..))
import Array
Array.fromList [ 1, 2 ]
|> Array.Linear.padTo
{ lengthMinimum = 4
, pad = ( Up, \n -> Array.repeat n 0 )
}
--> Array.fromList [ 1, 2, 0, 0 ]
Array.fromList [ 1, 2 ]
|> Array.Linear.padTo
{ lengthMinimum = 4
, pad = ( Down, \n -> Array.repeat n 0 )
}
--> Array.fromList [ 0, 0, 1, 2 ]padTo { .. length .. direction .. } |> take ( direction, length )
→ "resize" behavior
Array.fromList [ 1, 2, 3 ]
|> Array.Linear.padTo
{ lengthMinimum = 2
, pad = ( Up, \n -> Array.repeat n 0 )
}
|> Array.Linear.take ( Up, 2 )
--> Array.fromList [ 1, 2 ]
Array.fromList [ 1, 2, 3 ]
|> Array.Linear.padTo
{ lengthMinimum = 2
, pad = ( Down, \n -> Array.repeat n 0 )
}
|> Array.Linear.take ( Down, 2 )
--> Array.fromList [ 2, 3 ]A given number of elements from one side.
import Linear exposing (DirectionLinear(..))
import Array
Array.fromList [ 1, 2, 3, 4, 5, 6 ]
|> Array.Linear.take ( Up, 4 )
|> Array.Linear.take ( Down, 2 )
--> Array.fromList [ 3, 4 ]
Array.fromList [ 1, 2, 3 ]
|> Array.Linear.take ( Up, 100 )
--> Array.fromList [ 1, 2, 3 ]Array.empty if the amount of elements to take is negative.
import Linear exposing (DirectionLinear(..))
import Array
Array.fromList [ 1, 2, 3 ]
|> Array.Linear.take ( Up, -100 )
--> Array.emptyRemove a given number of elements from one side.
import Linear exposing (DirectionLinear(..))
import Array
Array.fromList [ 1, 2, 3, 4, 5, 6 ]
|> Array.Linear.drop ( Down, 2 )
--> Array.fromList [ 1, 2, 3, 4 ]
Array.fromList [ 1, 2, 3 ]
|> Array.Linear.drop ( Up, 100 )
--> Array.emptyNothing is dropped if the amount of elements to drop is negative.
import Linear exposing (DirectionLinear(..))
import Array
Array.fromList [ 1, 2, 3 ]
|> Array.Linear.drop ( Up, -1 )
--> Array.fromList [ 1, 2, 3 ]Split the Array into equal-length chunks.
The left over elements to one side are in remainder.
import Linear exposing (DirectionLinear(..))
import Array
Array.fromList [ 1, 2, 3, 4, 5, 6, 7 ]
|> Array.Linear.toChunks
{ length = 3, remainder = Up }
--> { 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.toChunks
{ length = 3, remainder = Down }
--> { remainder = Array.fromList [ 1 ]
--> , chunks =
--> Array.fromList
--> [ Array.fromList [ 2, 3, 4 ]
--> , Array.fromList [ 5, 6, 7 ]
--> ]
--> }Put elements of a given Array between the elements left and right to the given index in a direction.
import Linear exposing (DirectionLinear(..))
import Array
Array.fromList [ 'a', 'd', 'e' ]
|> Linear.at ( Up, 1 )
|> Array.Linear.squeezeIn
(\() -> Array.fromList [ 'b', 'c' ])
--> Array.fromList [ 'a', 'b', 'c', 'd', 'e' ]
Array.fromList [ 'a', 'd', 'e' ]
|> Linear.at ( Down, 2 )
|> Array.Linear.squeezeIn
(\() -> Array.fromList [ 'b', 'c' ])
--> Array.fromList [ 'a', 'b', 'c', 'd', 'e' ]If the index is out of bounds, nothing is inserted.
import Linear exposing (DirectionLinear(..))
import Array
Array.fromList [ 'a', 'd', 'e' ]
|> Linear.at ( Up, -1 )
|> Array.Linear.squeezeIn
(\() -> Array.fromList [ 'b', 'c' ])
--> Array.fromList [ 'a', 'd', 'e' ]
Array.fromList [ 'a', 'd', 'e' ]
|> Linear.at ( Up, 4 )
|> Array.Linear.squeezeIn
(\() -> Array.fromList [ 'b', 'c' ])
--> Array.fromList [ 'a', 'd', 'e' ]insert allows inserting a single element.
Array.Linear.at directly.The element at an index in a direction.
import Linear exposing (DirectionLinear(..))
import Array
Array.fromList [ "lose", "win", "lose" ]
|> Linear.at ( Down, 0 )
|> Array.Linear.access
--> "lose" |> Ok
Array.fromList [ "lose", "win", "lose" ]
|> Linear.at ( Up, 0 )
|> Array.Linear.access
--> "lose" |> OkErr if the index is out of range.
import Linear exposing (DirectionLinear(..), ExpectedIndexInRange(..))
import Array
Array.fromList [ 1, 2, 3 ]
|> Linear.at ( Up, -1 )
|> Array.Linear.access
--> Err (ExpectedIndexForLength 3)
Array.fromList [ 1, 2, 3 ]
|> Linear.at ( Up, 100 )
|> Array.Linear.access
--> Err (ExpectedIndexForLength 3)
The element at an index in a direction.
Errif the index is out of range.