lue-bird/elm-linear-direction/3.0.0

NOTE: the latest version is 11.2.0

List.Linear

List operations that can be applied in either direction.

scan

at : Int -> LinearDirection -> List element -> Maybe element

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

import LinearDirection exposing (LinearDirection(..))


[ 0, 1, 2, 3 ]
    |> List.Linear.at 0 LastToFirst
--> Just 3


[ 0, 1, 2, 3 ]
    |> List.Linear.at 2 FirstToLast
--> Just 2

Returns Nothing if the index is out of range:

import LinearDirection exposing (LinearDirection(..))


[ 0, 1, 2, 3 ]
    |> List.Linear.at 4 FirstToLast
--> Nothing


[ 0, 1, 2, 3 ]
    |> List.Linear.at -1 FirstToLast
--> Nothing

If you're using this operation often, consider using an Array instead of a List to get O(log n) vs. O(n) random access performance.

alter

part

take : Int -> LinearDirection -> List element -> List element

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 ]
drop : LinearDirection -> Int -> List element -> List element

Remove a number of elements from one side.

import LinearDirection exposing (LinearDirection(..))


tail =
    List.Linear.drop FirstToLast 1


removeLast =
    List.Linear.drop LastToFirst 1
Int
-> List element
-> { chunks : List (List element)
, remainder : List element
}

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

transform

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

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"