Array operations that can be applied in either Direction
Reduce in a given Direction
from a given initial accumulated thing
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"Map each element using information collected from previous steps,
folding in a given Direction from given initial information.
Both the mapped Array and the folded information will be returned
You'll often find this under the name "mapAccum"
import Linear exposing (Direction(..))
import Array exposing (Array)
Array.fromList [ 1, 2, 3 ]
|> Array.Linear.mapFoldFrom 0
Down
(\state ->
{ element = state.folded
, folded = state.folded + state.element
}
)
--> { mapped = Array.fromList [ 5, 3, 0 ], folded = 6 }
mapIndexed : Direction -> (Int -> a -> b) -> (Array a -> Array b)
mapIndexed indexDirection mapAtIndex =
Array.Linear.mapFoldFrom 0
indexDirection
(\state ->
{ element = state.element |> mapAtIndex state.folded
, folded = state.folded + 1
}
)
>> .mapped
Array.fromList [ 'h', 'i', 'y', 'o' ]
|> mapIndexed Up Tuple.pair
--> Array.fromList [ ( 0, 'h' ), ( 1, 'i' ), ( 2, 'y' ), ( 3, 'o' ) ]
Array.fromList [ 'h', 'i', 'y', 'o' ]
|> mapIndexed Down Tuple.pair
--> Array.fromList [ ( 3, 'h' ), ( 2, 'i' ), ( 1, 'y' ), ( 0, 'o' ) ]Set the element at a given index in a given 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 a given index in a given 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 given 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 given 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.padToAtLeast Up
4
(\l -> Array.repeat l 0)
--> Array.fromList [ 1, 2, 0, 0 ]
Array.fromList [ 1, 2 ]
|> Array.Linear.padToAtLeast Down
4
(\l -> Array.repeat l 0)
--> Array.fromList [ 0, 0, 1, 2 ]To achieve "resize" behavior, use
padToAtLeast direction length followed by take direction length
Array.fromList [ 1, 2, 3 ]
|> Array.Linear.padToAtLeast Up
2
(\l -> Array.repeat l 0)
|> Array.Linear.take Up 2
--> Array.fromList [ 1, 2 ]
Array.fromList [ 1, 2, 3 ]
|> Array.Linear.padToAtLeast Down
2
(\l -> Array.repeat l 0)
|> Array.Linear.take Down 2
--> Array.fromList [ 2, 3 ]A given number of elements from one side
in a given Direction
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 ]The amount of elements to take is negative? → Array.empty
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 a given Array
to the end in a given direction
import Linear exposing (Direction(..))
import Array
Array.fromList [ 1, 2, 3 ]
|> Array.Linear.attach Up (Array.fromList [ 4, 5, 6 ])
--> Array.fromList [ 1, 2, 3, 4, 5, 6 ]
Array.fromList [ 1, 2, 3 ]
|> Array.Linear.attach 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 given 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 given
DirectionNothingif the index is out of range