lue-bird/elm-linear-direction/2.3.0

NOTE: the latest version is 11.2.0

List.LinearDirection

scan

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

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

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


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

Returns Nothing if the index is out of range:

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


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

transform

-> (element -> result -> result)
-> result
-> List element
-> result

Reduce a List in a direction.

[ 'l', 'i', 'v', 'e' ]
    |> List.fold FirstToLast String.cons ""
--> "evil"


[ 'l', 'i', 'v', 'e' ]
    |> List.fold LastToFirst String.cons ""
--> "live"

modify

order : LinearDirection -> List a -> List a

Keep the order if FirstToLast, reverse if LastToFirst.

[ 1, 2, 3 ] |> List.order LastToFirst
--> [ 3, 2, 1 ]


[ 1, 2, 3 ] |> List.order FirstToLast
--> [ 1, 2, 3 ]

part

Int
-> List a
-> { groups : List (List a)
, less : List a
}

Split the Array into equal-sized chunks. The elements left over on one side are in less.

[ 1, 2, 3, 4, 5, 6, 7 ]
    |> List.group 3 FirstToLast
--> { groups = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
--> , less = Array.fromList [ 7 ]
--> }


[ 1, 2, 3, 4, 5, 6, 7 ]
    |> List.group 3 LastToFirst
--> { less = [ 1 ]
--> , groups = [ [ 2, 3, 4 ], [ 5, 6, 7 ] ]
--> }
takeFrom : LinearDirection -> Int -> List a -> List a

Only use a number of elements from one side.

[ 1, 2, 3, 4 ]
    |> List.takeFrom FirstToLast 2
--> [ 1, 2 ]


[ 1, 2, 3, 4 ]
    |> List.takeFrom LastToFirst 2
--> [ 3, 4 ]

Named this way to avoid name clashes with List.take when importing List.LinearDirection as List.

dropFrom : LinearDirection -> Int -> List a -> List a

Remove a number of elements from one side.

tail =
    List.dropFrom FirstToLast 1


removeLast =
    List.dropFrom LastToFirst 1

Named this way to avoid name clashes with List.drop when importing List.LinearDirection as List.