lue-bird/elm-linear-direction/5.0.0

NOTE: the latest version is 11.2.0

List.Linear

List operations that can be applied in either direction.

scan

( DirectionLinear, Int )
-> List element
-> Result ExpectedIndexInRange element

Just the element at the given index in the list in a direction:

import Linear exposing (DirectionLinear(..))


[ 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 (DirectionLinear(..), ExpectedIndexInRange(..))


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


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

transform

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

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

Alter the element at the given index in a direction:

import Linear exposing (DirectionLinear(..))


[ 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 (DirectionLinear(..))


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

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

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

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 ] ]
--> }