lue-bird/elm-linear-direction/11.0.0

NOTE: the latest version is 11.2.0

List.Linear

List operations that can be applied in either Direction

scan

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

Just the element at the given index in a Direction

import Linear exposing (Direction(..))


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


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

Nothing if the index is out of range:

import Linear exposing (Direction(..))


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


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

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

alter

( 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 )
        (\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

transform

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

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"
accumulationValue
-> ( { element : element
, folded : accumulationValue
}
-> { element : mappedElement
, folded : accumulationValue
}
)
-> List element
-> { mapped : List mappedElement
, folded : accumulationValue
}

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' ) ]

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