List operations that can be applied in either Direction
Alter the element at the given index in a Direction
import Linear exposing (Direction(..))
[ 1, 2, 2 ]
|> List.Linear.elementAlter ( Down, 0 )
(\n -> n + 1)
--> [ 1, 2, 3 ]Do nothing if the index is out of range
import Linear exposing (Direction(..))
[ 0, 1, 2, 3 ]
|> List.Linear.elementAlter ( Up, 4 )
(\n -> n + 1)
--> [ 0, 1, 2, 3 ]
[ 0, 1, 2, 3 ]
|> List.Linear.elementAlter ( Up, -1 )
(\n -> n + 1)
--> [ 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
Reduce in a Direction
from a given initial accumulated thing
import Linear exposing (Direction(..))
[ 'l', 'i', 'v', 'e' ]
|> List.Linear.foldFrom "" Up String.cons
--> "evil"
[ 'l', 'i', 'v', 'e' ]
|> List.Linear.foldFrom "" Down String.cons
--> "live"foldTraceFrom its first element
looking in a given Direction
import Linear exposing (Direction(..))
[ 1, 2, 3 ]
|> List.Linear.foldTrace Down (\el soFar -> soFar + el)
--> [ 3, 5, 6 ]
[ 1, 2, 3 ]
|> List.Linear.foldTrace Down (\el soFar -> soFar - el)
--> [ 3, 1, 0 ]Reduce in a given Direction,
stacking up all of the intermediate results
import Linear exposing (Direction(..))
[ 1, 2, 3, 4 ]
|> List.Linear.foldTraceFrom 0 Up (\el soFar -> soFar + el)
--> [ 1, 3, 6, 10 ]
[ 1, 2, 3 ]
|> List.Linear.foldTraceFrom 0 Down (\el soFar -> soFar - el)
--> [ -3, -5, -6 ]A given number of elements from one side in a Direction
import Linear exposing (Direction(..))
[ 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 (Direction(..))
[ 1, 2, 3 ]
|> List.Linear.take Up -100
--> []Remove a given number of elements from one side in a Direction
import Linear exposing (Direction(..))
removeFirst =
List.Linear.drop Up 0
removeLast =
List.Linear.drop Down 0Nothing is dropped if the amount of elements to drop is negative
import Linear exposing (Direction(..))
[ 1, 2, 3 ]
|> List.Linear.drop Up -1
--> [ 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(..))
[ 1, 2, 3, 4, 5, 6, 7 ]
|> List.Linear.toChunksOf Up 3
--> { chunks = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
--> , remainder = [ 7 ]
--> }
[ 1, 2, 3, 4, 5, 6, 7 ]
|> List.Linear.toChunksOf Down 3
--> { remainder = [ 1 ]
--> , chunks = [ [ 2, 3, 4 ], [ 5, 6, 7 ] ]
--> }
Justthe element at the given index in aDirectionNothingif 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