List operations that can be applied in either direction.
Only use a number of elements from one side.
import LinearDirection exposing (LinearDirection(..))
[ 1, 2, 3, 4 ]
|> List.Linear.take 2 FirstToLast
--> [ 1, 2 ]
[ 1, 2, 3, 4 ]
|> List.Linear.take 2 LastToFirst
--> [ 3, 4 ]Remove a number of elements from one side.
import LinearDirection exposing (LinearDirection(..))
tail =
List.Linear.drop FirstToLast 1
removeLast =
List.Linear.drop LastToFirst 1Split the Array into equal-sized chunks. The elements left over on one side are in less.
import LinearDirection exposing (LinearDirection(..))
[ 1, 2, 3, 4, 5, 6, 7 ]
|> List.Linear.toChunksOf 3 FirstToLast
--> { chunks = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
--> , remainder = [ 7 ]
--> }
[ 1, 2, 3, 4, 5, 6, 7 ]
|> List.Linear.toChunksOf 3 LastToFirst
--> { remainder = [ 1 ]
--> , chunks = [ [ 2, 3, 4 ], [ 5, 6, 7 ] ]
--> }Reduce a List in a direction.
import LinearDirection exposing (LinearDirection(..))
[ 'l', 'i', 'v', 'e' ]
|> List.Linear.foldFrom "" FirstToLast String.cons
--> "evil"
[ 'l', 'i', 'v', 'e' ]
|> List.Linear.foldFrom "" LastToFirst String.cons
--> "live"
Returns
Justthe element at the given index in the list in a direction:Returns
Nothingif the index is out of range:If you're using this operation often, consider using an
Arrayinstead of aListto getO(log n)vs.O(n)random access performance.