lue-bird/elm-linear-direction/3.0.0

NOTE: the latest version is 11.2.0

Array.Linear

Array operations that can be applied in either direction.

scan

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

Just the element at an index in a direction.

import LinearDirection exposing (LinearDirection(..))
import Array


Array.fromList [ "lose", "win", "lose" ]
    |> Array.Linear.at 0 LastToFirst
--> Just "lose"




Array.fromList [ "lose", "win", "lose" ]
    |> Array.Linear.at 0 FirstToLast
--> Just "lose"

Nothing, if the index is out of range.

import LinearDirection exposing (LinearDirection(..))
import Array


Array.fromList [ 1, 2, 3 ]
    |> Array.Linear.at -1 FirstToLast
--> Nothing


Array.fromList [ 1, 2, 3 ]
    |> Array.Linear.at 100 FirstToLast
--> Nothing

alter

replaceAt : Int -> LinearDirection -> element -> Array element -> Array element

Set the element at an index in a direction.

import LinearDirection exposing (LinearDirection(..))
import Array


Array.fromList [ "I", "am", "ok" ]
    |> Array.Linear.replaceAt 2 FirstToLast "confusion"
--> Array.fromList [ "I", "am", "confusion" ]


Array.fromList [ "I", "am", "ok" ]
    |> Array.Linear.replaceAt 1 LastToFirst "feel"
--> Array.fromList [ "I", "feel", "ok" ]

If the index is out of range, the array is unaltered.

import LinearDirection exposing (LinearDirection(..))
import Array


Array.fromList [ "I", "am", "ok" ]
    |> Array.Linear.replaceAt -1 FirstToLast "is"
--> Array.fromList [ "I", "am", "ok" ]
removeAt : Int -> LinearDirection -> Array element -> Array element

Kick an element out of an Array at a given index in a direction.

import LinearDirection exposing (LinearDirection(..))
import Array


Array.fromList [ 'a', 'a', 'b' ]
    |> Array.Linear.removeAt 1 FirstToLast
--> Array.fromList [ 'a', 'b' ]


Array.fromList [ 'a', 'b', 'c', 'd' ]
    |> Array.Linear.removeAt 0 LastToFirst
--> Array.fromList [ 'a', 'b', 'c' ]

If the index is out of bounds, nothing is changed.

import LinearDirection exposing (LinearDirection(..))
import Array


Array.Linear.removeAt -1 FirstToLast
    (Array.fromList [ 'a', 'a', 'b' ])
--> Array.fromList [ 'a', 'a', 'b' ]


Array.Linear.removeAt 100 FirstToLast
    (Array.fromList [ 'a', 'a', 'b' ])
--> Array.fromList [ 'a', 'a', 'b' ]
insertAt : Int -> LinearDirection -> element -> Array element -> Array element

Put an element in an Array at a given index in a direction.

import LinearDirection exposing (LinearDirection(..))
import Array


Array.fromList [ 'a', 'c', 'd' ]
    |> Array.Linear.insertAt 1 FirstToLast 'b'
--> Array.fromList [ 'a', 'b', 'c', 'd' ]


Array.fromList [ 'a', 'c', 'd' ]
    |> Array.Linear.insertAt 2 LastToFirst 'b'
--> Array.fromList [ 'a', 'b', 'c', 'd' ]

If the index is out of bounds, nothing is inserted.

import LinearDirection exposing (LinearDirection(..))
import Array


Array.fromList [ 'a', 'c', 'd' ]
    |> Array.Linear.insertAt -1 FirstToLast 'b'
--> Array.fromList [ 'a', 'c', 'd' ]


Array.fromList [ 'a', 'c', 'd' ]
    |> Array.Linear.insertAt 4 FirstToLast 'b'
--> Array.fromList [ 'a', 'c', 'd' ]

squeezeInAt allows inserting a whole Array of elements.

padToLength : Int -> LinearDirection -> element -> Array element -> Array element

Resize an Array in a direction, always padding with the same given value.

import LinearDirection exposing (LinearDirection(..))
import Array


Array.fromList [ 1, 2 ]
    |> Array.Linear.padToLength 4 LastToFirst 0
--> Array.fromList [ 0, 0, 1, 2 ]


Array.fromList [ 1, 2, 3 ]
    |> Array.Linear.padToLength 2 LastToFirst 0
--> Array.fromList [ 2, 3 ]


Array.fromList [ 1, 2 ]
    |> Array.Linear.padToLength 4 FirstToLast 0
--> Array.fromList [ 1, 2, 0, 0 ]


Array.fromList [ 1, 2, 3 ]
    |> Array.Linear.padToLength 2 FirstToLast 0
--> Array.fromList [ 1, 2 ]

The result is an empty array if the index is negative.

import LinearDirection exposing (LinearDirection(..))


Array.fromList [ 1, 2 ]
    |> Array.Linear.padToLength -1 LastToFirst 0
--> Array.empty

glue

Int
-> Array element
-> Array element
-> Array element

Put elements of a given Array between the elements left and right to the given index in a direction.

import LinearDirection exposing (LinearDirection(..))
import Array


Array.fromList [ 'a', 'd', 'e' ]
    |> Array.Linear.squeezeInAt 1
        FirstToLast
        (Array.fromList [ 'b', 'c' ])
--> Array.fromList [ 'a', 'b', 'c', 'd', 'e' ]


Array.fromList [ 'a', 'd', 'e' ]
    |> Array.Linear.squeezeInAt 2
        LastToFirst
        (Array.fromList [ 'b', 'c' ])
--> Array.fromList [ 'a', 'b', 'c', 'd', 'e' ]

If the index is out of bounds, nothing is inserted.

import LinearDirection exposing (LinearDirection(..))
import Array


Array.fromList [ 'a', 'd', 'e' ]
    |> Array.Linear.squeezeInAt -1
        FirstToLast
        (Array.fromList [ 'b', 'c' ])
--> Array.fromList [ 'a', 'd', 'e' ]


Array.fromList [ 'a', 'd', 'e' ]
    |> Array.Linear.squeezeInAt 4
        FirstToLast
        (Array.fromList [ 'b', 'c' ])
--> Array.fromList [ 'a', 'd', 'e' ]

insertAt allows inserting a single element.

part

take : Int -> LinearDirection -> Array element -> Array element

A certain number of elements from one side.

import LinearDirection exposing (LinearDirection(..))
import Array


Array.fromList [ 1, 2, 3, 4, 5, 6 ]
    |> Array.Linear.take 4 FirstToLast
    |> Array.Linear.take 2 LastToFirst
--> Array.fromList [ 3, 4 ]


Array.fromList [ 1, 2, 3 ]
    |> Array.Linear.take 100 FirstToLast
--> Array.fromList [ 1, 2, 3 ]


Array.fromList [ 1, 2, 3 ]
    |> Array.Linear.take -100 FirstToLast
--> Array.empty
drop : Int -> LinearDirection -> Array element -> Array element

After a certain number of elements from one side.

import LinearDirection exposing (LinearDirection(..))
import Array


Array.fromList [ 1, 2, 3, 4, 5, 6 ]
    |> Array.Linear.drop 2 LastToFirst
--> Array.fromList [ 1, 2, 3, 4 ]


Array.fromList [ 1, 2, 3 ]
    |> Array.Linear.drop 100 FirstToLast
--> Array.empty

Nothing is dropped if the index is negative.

import LinearDirection exposing (LinearDirection(..))
import Array


Array.fromList [ 1, 2, 3 ]
    |> Array.Linear.drop -1 FirstToLast
--> Array.fromList [ 1, 2, 3 ]
Int
-> Array element
-> { chunks : Array (Array element)
, remainder : Array element
}

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

import LinearDirection exposing (LinearDirection(..))
import Array


Array.fromList [ 1, 2, 3, 4, 5, 6, 7 ]
    |> Array.Linear.toChunksOf 3 FirstToLast
--> { chunks =
-->     Array.fromList
-->         [ Array.fromList [ 1, 2, 3 ]
-->         , Array.fromList [ 4, 5, 6 ]
-->         ]
--> , remainder = Array.fromList [ 7 ]
--> }


Array.fromList [ 1, 2, 3, 4, 5, 6, 7 ]
    |> Array.Linear.toChunksOf 3 LastToFirst
--> { remainder = Array.fromList [ 1 ]
--> , chunks =
-->     Array.fromList
-->         [ Array.fromList [ 2, 3, 4 ]
-->         , Array.fromList [ 5, 6, 7 ]
-->         ]
--> }

transform

accumulationValue
-> (element -> accumulationValue -> accumulationValue)
-> Array element
-> accumulationValue

Reduce an Array in a direction.

import LinearDirection exposing (LinearDirection(..))
import Array


Array.fromList [ 'l', 'i', 'v', 'e' ]
    |> Array.Linear.foldFrom "" FirstToLast String.cons
--> "evil"


Array.fromList [ 'l', 'i', 'v', 'e' ]
    |> Array.Linear.foldFrom "" LastToFirst String.cons
--> "live"