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"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(..))
[ 1, 2, 3 ]
|> List.Linear.mapFoldFrom 0
Down
(\state ->
{ element = state.folded
, folded = state.folded + state.element
}
)
--> { mapped = [ 5, 3, 0 ], folded = 6 }
mapIndexed : Direction -> (Int -> a -> b) -> (List a -> List b)
mapIndexed indexDirection mapAtIndex =
List.Linear.mapFoldFrom 0
indexDirection
(\state ->
{ element = state.element |> mapAtIndex state.folded
, folded = state.folded + 1
}
)
>> .mapped
[ 'h', 'i', 'y', 'o' ]
|> mapIndexed Up Tuple.pair
--> [ ( 0, 'h' ), ( 1, 'i' ), ( 2, 'y' ), ( 3, 'o' ) ]
[ 'h', 'i', 'y', 'o' ]
|> mapIndexed Down Tuple.pair
--> [ ( 3, 'h' ), ( 2, 'i' ), ( 1, 'y' ), ( 0, 'o' ) ]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