lue-bird/elm-linear-direction/6.0.0

NOTE: the latest version is 11.2.0

List.Linear

List operations that can be applied in either Direction

scan

( Direction, Int )
-> List element
-> Result IndexIntOutOfRange element

Just the element at the given index in a Direction

import Linear exposing (Direction(..))


[ 0, 1, 2, 3 ]
    |> List.Linear.element ( Down, 0 )
--> Ok 3


[ 0, 1, 2, 3 ]
    |> List.Linear.element ( Up, 2 )
--> Ok 2

Err if the index is out of range:

import Linear exposing (Direction(..), IndexIntOutOfRange(..))


[ 0, 1, 2, 3 ]
    |> List.Linear.element ( Up, 5 )
--> Err IndexIntBeyondElements


[ 0, 1, 2, 3 ]
    |> List.Linear.element ( Up, -1 )
--> Err IndexIntNegative

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

transform

accumulationValue
-> (element -> accumulationValue -> accumulationValue)
-> List element
-> accumulationValue

Reduce in a Direction

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"
-> (element -> element -> element)
-> List element
-> List element

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 ]
accumulationValue
-> (element -> accumulationValue -> accumulationValue)
-> List element
-> List accumulationValue

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 ]
( Direction, Int )
-> (element -> element)
-> List element
-> List element

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 )
        (\_ -> 123)
--> [ 0, 1, 2, 3 ]


[ 0, 1, 2, 3 ]
    |> List.Linear.elementAlter ( Up, -1 )
        (\_ -> 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.

part

take : ( Direction, Int ) -> List element -> List element

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 )
--> []
drop : ( Direction, Int ) -> List element -> List element

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, 0 )

Nothing 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 ]
-> Int
-> List element
-> { chunks : List (List element)
, remainder : List element
}

Split into equal-length chunks. 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 ] ]
--> }