Orasund/elm-static-array/3.0.0

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 =
Index n

An Index is integer between 0 and n-1

range : 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 : 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 : Length n -> Int -> Index n

Construct an Index by wrapping higher values

import StaticArray.Length as Length


fromModBy (Length.one |> Length.plus2) 3
    --> first --0
fromLessThen : Length n -> 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 : 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 -> Int

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

fromInt : Length n -> 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 : 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 =
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 =
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)