List operations that can be applied in either direction.
Reduce a List in a direction.
import Linear exposing (DirectionLinear(..))
[ 'l', 'i', 'v', 'e' ]
|> List.Linear.foldFrom ( "", Up, String.cons )
--> "evil"
[ 'l', 'i', 'v', 'e' ]
|> List.Linear.foldFrom ( "", Down, String.cons )
--> "live"Alter the element at the given index in a direction:
import Linear exposing (DirectionLinear(..))
[ 1, 2, 2 ]
|> Linear.at ( Down, 0 )
|> List.Linear.alter (\n -> n + 1)
--> [ 1, 2, 3 ]Do nothing if the index is out of range:
import Linear exposing (DirectionLinear(..))
[ 0, 1, 2, 3 ]
|> Linear.at ( Up, 4 )
|> List.Linear.alter (\_ -> 123)
--> [ 0, 1, 2, 3 ]
[ 0, 1, 2, 3 ]
|> Linear.at ( Up, -1 )
|> List.Linear.alter (\_ -> 123)
--> [ 0, 1, 2, 3 ]If you're using at-operations often, consider using an Array instead of a List
to get O(log n) vs. O(n) random access performance.
A given number of elements from one side.
import Linear exposing (DirectionLinear(..))
[ 1, 2, 3, 4 ]
|> List.Linear.take ( Up,2 )
--> [ 1, 2 ]
[ 1, 2, 3, 4 ]
|> List.Linear.take ( Down, 2 )
--> [ 3, 4 ][] if the amount of elements to take is negative.
import Linear exposing (DirectionLinear(..))
[ 1, 2, 3 ]
|> List.Linear.take ( Up, -100 )
--> []Remove a given number of elements from one side.
import Linear exposing (DirectionLinear(..))
removeFirst =
List.Linear.drop ( Up, 1 )
removeLast =
List.Linear.drop ( Down, 1 )Nothing is dropped if the amount of elements to drop is negative.
import Linear exposing (DirectionLinear(..))
[ 1, 2, 3 ]
|> List.Linear.drop ( Up, -1 )
--> [ 1, 2, 3 ]Split the List into equal-length chunks.
The left over elements to one side are in remainder.
import Linear exposing (DirectionLinear(..))
[ 1, 2, 3, 4, 5, 6, 7 ]
|> List.Linear.toChunks
{ length = 3, remainder = Up }
--> { chunks = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
--> , remainder = [ 7 ]
--> }
[ 1, 2, 3, 4, 5, 6, 7 ]
|> List.Linear.toChunks
{ length = 3, remainder = Down }
--> { remainder = [ 1 ]
--> , chunks = [ [ 2, 3, 4 ], [ 5, 6, 7 ] ]
--> }Array.Linear.at directlyJust the element at the given index in the list in a direction:
import Linear exposing (DirectionLinear(..))
[ 0, 1, 2, 3 ]
|> Linear.at ( Down, 0 )
|> List.Linear.access
--> Ok 3
[ 0, 1, 2, 3 ]
|> Linear.at ( Up, 2 )
|> List.Linear.access
--> Ok 2Err if the index is out of range:
import Linear exposing (DirectionLinear(..), ExpectedIndexInRange(..))
[ 0, 1, 2, 3 ]
|> Linear.at ( Up, 5 )
|> List.Linear.access
--> Err (ExpectedIndexForLength 4)
[ 0, 1, 2, 3 ]
|> Linear.at ( Up, -1 )
|> List.Linear.access
--> Err (ExpectedIndexForLength 4)If you're using at-operations often, consider using an Array instead of a List
to get O(log n) vs. O(n) random access performance.
Justthe element at the given index in the list in a direction:Errif the index is out of range:If you're using at-operations often, consider using an
Arrayinstead of aListto getO(log n)vs.O(n)random access performance.