Array operations that can be applied in either Direction
Reduce in a Direction
import Linear exposing (Direction(..))
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"Set the element at an index in a Direction
import Linear exposing (Direction(..))
import Array
Array.fromList [ "I", "am", "ok" ]
|> Array.Linear.elementReplace ( Up, 2 )
(\() -> "confusion")
--> Array.fromList [ "I", "am", "confusion" ]
Array.fromList [ "I", "am", "ok" ]
|> Array.Linear.elementReplace ( Down, 1 )
(\() -> "feel")
--> Array.fromList [ "I", "feel", "ok" ]If the index is out of range, the Array is unaltered
import Linear exposing (Direction(..))
import Array
Array.fromList [ "I", "am", "ok" ]
|> Array.Linear.elementReplace ( Up, -1 )
(\() -> "is")
--> Array.fromList [ "I", "am", "ok" ]Change the element at an index in a Direction
import Linear exposing (Direction(..))
import Array
Array.fromList [ "I", "am", "ok" ]
|> Array.Linear.elementAlter ( Up, 2 )
String.toUpper
--> Array.fromList [ "I", "am", "OK" ]
Array.fromList [ "I", "am", "ok" ]
|> Array.Linear.elementAlter ( Down, 0 )
String.toUpper
--> Array.fromList [ "I", "am", "OK" ]If the index is out of range, the Array is unaltered
import Linear exposing (Direction(..))
import Array
Array.fromList [ "I", "am", "ok" ]
|> Array.Linear.elementAlter ( Up, -1 )
String.toUpper
--> Array.fromList [ "I", "am", "ok" ]Put in a given element at a given index in a Direction
import Linear exposing (Direction(..))
import Array
Array.fromList [ 'a', 'c', 'd' ]
|> Array.Linear.insert ( Up, 1 )
(\() -> 'b')
--> Array.fromList [ 'a', 'b', 'c', 'd' ]
Array.fromList [ 'a', 'c', 'd' ]
|> Array.Linear.insert ( Down, 2 )
(\() -> 'b')
--> Array.fromList [ 'a', 'b', 'c', 'd' ]If the index is out of bounds, nothing is inserted
import Linear exposing (Direction(..))
import Array
Array.fromList [ 'a', 'c', 'd' ]
|> Array.Linear.insert ( Up, -1 )
(\() -> 'b')
--> Array.fromList [ 'a', 'c', 'd' ]
Array.fromList [ 'a', 'c', 'd' ]
|> Array.Linear.insert ( Up, 4 )
(\() -> 'b')
--> Array.fromList [ 'a', 'c', 'd' ]squeezeIn allows inserting a whole Array of elements
Kick an element out at a given index in a Direction
import Linear exposing (Direction(..))
import Array
Array.fromList [ 'a', 'a', 'b' ]
|> Array.Linear.remove ( Up, 1 )
--> Array.fromList [ 'a', 'b' ]
Array.fromList [ 'a', 'b', 'c', 'd' ]
|> Array.Linear.remove ( Down, 0 )
--> Array.fromList [ 'a', 'b', 'c' ]If the index is out of bounds, nothing is changed
import Linear exposing (Direction(..))
import Array
Array.fromList [ 'a', 'a', 'b' ]
|> Array.Linear.remove ( Up, -1 )
--> Array.fromList [ 'a', 'a', 'b' ]
Array.fromList [ 'a', 'a', 'b' ]
|> Array.Linear.remove ( Up, 100 )
--> Array.fromList [ 'a', 'a', 'b' ]Pad in a Direction based on how much it takes to reach at a given length
import Linear exposing (Direction(..))
import Array
Array.fromList [ 1, 2 ]
|> Array.Linear.padToLength Up
(\l -> Array.repeat l 0)
4
--> Array.fromList [ 1, 2, 0, 0 ]
Array.fromList [ 1, 2 ]
|> Array.Linear.padToLength Down
(\l -> Array.repeat l 0)
4
--> Array.fromList [ 0, 0, 1, 2 ]padToLength direction ... length |> take ( direction, length )
→ "resize" behavior
Array.fromList [ 1, 2, 3 ]
|> Array.Linear.padToLength Up
(\l -> Array.repeat l 0)
2
|> Array.Linear.take ( Up, 2 )
--> Array.fromList [ 1, 2 ]
Array.fromList [ 1, 2, 3 ]
|> Array.Linear.padToLength Down
(\l -> Array.repeat l 0)
2
|> Array.Linear.take ( Down, 2 )
--> Array.fromList [ 2, 3 ]A given number of elements from one side
import Linear exposing (Direction(..))
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 (Direction(..))
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 (Direction(..))
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 (Direction(..))
import Array
Array.fromList [ 1, 2, 3 ]
|> Array.Linear.drop ( Up, -1 )
--> Array.fromList [ 1, 2, 3 ]Split into equal-sized chunks of a given length in a given Direction.
The left over elements to one side are in remainder
import Linear exposing (Direction(..))
import Array
Array.fromList [ 1, 2, 3, 4, 5, 6, 7 ]
|> Array.Linear.toChunksOf Up 3
--> { 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 Down 3
--> { remainder = Array.fromList [ 1 ]
--> , chunks =
--> Array.fromList
--> [ Array.fromList [ 2, 3, 4 ]
--> , Array.fromList [ 5, 6, 7 ]
--> ]
--> }Attach elements of an Array
to the end in a given direction
import Linear exposing (Direction(..))
import Array
Array.fromList [ 1, 2, 3 ]
|> Array.Linear.glue Up (Array.fromList [ 4, 5, 6 ])
--> Array.fromList [ 1, 2, 3, 4, 5, 6 ]
Array.fromList [ 1, 2, 3 ]
|> Array.Linear.glue Down (Array.fromList [ 4, 5, 6 ])
--> Array.fromList [ 4, 5, 6, 1, 2, 3 ]Put elements of a given Array between the elements left and right to the given index in a Direction
import Linear exposing (Direction(..))
import Array
Array.fromList [ 'a', 'd', 'e' ]
|> Array.Linear.squeezeIn ( Up, 1 )
(\() -> Array.fromList [ 'b', 'c' ])
--> Array.fromList [ 'a', 'b', 'c', 'd', 'e' ]
Array.fromList [ 'a', 'd', 'e' ]
|> Array.Linear.squeezeIn ( Down, 2 )
(\() -> Array.fromList [ 'b', 'c' ])
--> Array.fromList [ 'a', 'b', 'c', 'd', 'e' ]If the index is outside of the Array's range, nothing is inserted
import Linear exposing (Direction(..))
import Array
Array.fromList [ 'a', 'd', 'e' ]
|> Array.Linear.squeezeIn ( Up, -1 )
(\() -> Array.fromList [ 'b', 'c' ])
--> Array.fromList [ 'a', 'd', 'e' ]
Array.fromList [ 'a', 'd', 'e' ]
|> Array.Linear.squeezeIn ( Up, 4 )
(\() -> Array.fromList [ 'b', 'c' ])
--> Array.fromList [ 'a', 'd', 'e' ]insert allows inserting a single element
The element at a given index in a
DirectionNothingif the index is out of range