Orasund / elm-static-array / StaticArray.Index

This module contains the Index type which can be though of as a range type. A value of type Index n is in between 0 and n.

This type will be replaced in compile type with an Int. Resulting in no performance loss.

Number Types


type One
    = One


type OnePlus a
    = OnePlus a

Constructor


type alias Index n =
StaticArray.Internal.Index n

An Index is integer between 0 and n-1

range : StaticArray.Internal.Length n -> List (Index n)

Returns the list of all indexes of an array with length n

first : Index n

The first index of an array

last : StaticArray.Internal.Length n -> Index n

The last element of an array with length n

import StaticArray.Length as Length
import StaticArray.Index as Index

Length.five
    |> last
    |> Index.toInt
    --> 4

fromModBy : StaticArray.Internal.Length n -> Basics.Int -> Index n

Construct an Index by wrapping higher values

import StaticArray.Length as Length

fromModBy (Length.one |> Length.plus2) 3
    --> first --0

fromLessThen : StaticArray.Internal.Length n -> Basics.Int -> Index n

Construct an Index by cutting off higher values

import StaticArray.Length as Length exposing (Length)
import StaticArray.Index exposing (OnePlus,Two)

length : Length (OnePlus Two)
length =
    (Length.one |> Length.plus2)

fromLessThen length 3
    --> last length --2

Operations

increase : StaticArray.Internal.Length n -> Index n -> Maybe (Index n)

Increases the index by one. You can not increase the last index.

decrease : Index n -> Maybe (Index n)

Decreases the index by one. You can not decrease the first index.

toInt : Index n -> Basics.Int

Convert to Integer. You can convert it back by using either fromModBy or fromLessThen

fromInt : StaticArray.Internal.Length n -> Basics.Int -> Maybe (Index n)

Turn a number into an Index and returns Nothing if the number is not in the range.

import StaticArray.Length as Length

fromInt Length.one -1 --> Nothing

fromInt Length.one 0 --> Just (fromLessThen Length.one 0)

fromInt Length.one 1 --> Nothing

setLength : StaticArray.Internal.Length m -> Index n -> Maybe (Index m)

Change the range of the index. If the length is smaller then the index, return Nothing.

Predefined Number Types


type alias Two =
OnePlus One


type alias Three =
OnePlus Two


type alias Four =
OnePlus Three


type alias Five =
OnePlus Four


type alias Six =
OnePlus Five


type alias Seven =
OnePlus Six


type alias Eight =
OnePlus Seven


type alias Nine =
OnePlus Eight


type alias Ten =
OnePlus Nine


type alias Twenty =
TenPlus Ten

Type Level Addition


type alias TwoPlus a =
OnePlus (OnePlus a)


type alias FourPlus a =
TwoPlus (TwoPlus a)


type alias FivePlus a =
OnePlus (FourPlus a)


type alias EightPlus a =
FourPlus (FourPlus a)


type alias TenPlus a =
FivePlus (FivePlus a)


type alias TwentyPlus a =
TenPlus (TenPlus a)