Cindiary/elm-not-empty/1.2.0

NotEmpty.Array

type alias Array item =
( item, Array item )

Not empty version of Array

Create

singleton : a -> Array a

A not empty array containing only the given value.

toList ( singleton "a" ) == [ "a" ]
initialize : Int -> (Int -> a) -> Array a

Same as Array.initialize.

Will always include at least 1 value ( initialized with index 0 ).

initialize 4 identity    == fromListWithHead 0 [ 1, 2, 3 ]
initialize 4 ( \n -> n * n ) == fromListWithHead 0 [ 1, 4, 9 ]
initialize 4 ( always 0 ) == fromListWithHead 0 [ 0, 0, 0 ]
initialize 0 ( \n -> n + 4 ) == singleton 4
repeat : Int -> a -> Array a

Same as Array.repeat.

Will always include at least 1 value.

repeat 4 "Value" == fromListWithHead "Value" [ "Value", "Value", "Value" ]
repeat 0 "Test" == singleton "Test"
withHead : a -> Array a -> Array a

A not empty array list with the given head and tail

array = withHead 5 ( Array.fromList [ 4, 3 ] )
toArray array == Array.fromList [ 5, 4, 3 ]
head array == 5
tail array == Array.fromList [ 4, 3 ]
fromList : List a -> Array a

Same as Array.fromList except using a NotEmpty.List

fromListWithHead : a -> List a -> Array a

Same as withHead but with a list instead of a array

Transforming from and to regular arrays

fromArray : Array a -> Maybe (Array a)

Convert a regular array into a not empty array, or Nothing if the array is empty

fromArray ( Array.fromList [] ) == Nothing
fromArray ( Array.fromList [ 1, 2, 3 ] ) == Just ( fromListWithHead 1 [ 2, 3 ] )
toArray : Array a -> Array a

Convert a not empty array into a regular array

toArray ( fromListWithHead 1 [ 2, 3 ] ) == Array.fromList [ 1, 2, 3 ] toArray ( singleton 1 ) == Array.fromList [ 1 ]

New functions

last : Array a -> a

Extract the last element from a not empty array.

last ( fromListWithHead 1 [ 2, 3 ] ) == 3
last ( singleton 1 ) == 1
tail : Array a -> Array a

Returns the elements after the first element as a regular array

tail ( fromListWithHead 1 [ 2, 3 ] ) == Array.fromList [ 2, 3 ]
tail ( singleton 1 ) == Array.fromList []
hasTail : Array a -> Bool

Returns wether the array has a tail with elements

hasTail ( fromListWithHead 1 [ 2, 3 ] ) == True
hasTail ( singleton 1 ) == False
hasTail ( fromListWithHead 1 [] ) == False
appendArray : Array a -> Array a -> Array a

Append a regular array to a not empty array

appendArray ( repeat 2 42 ) ( Array.repeat 3 81 ) == fromListWithHead 42 [ 42, 81, 81, 81 ]
prependArray : Array a -> Array a -> Array a

Prepend a regular array to a not empty array

prependArray ( Array.repeat 2 42 ) ( repeat 3 81 ) == fromListWithHead 42 [ 42, 81, 81, 81 ]

Modified functions

filter : (a -> Bool) -> Array a -> Maybe (Array a)

Same as Array.filter but will return Nothing if no elements satisfy the test

filter isEven ( fromListWithHead 1 [ 2, 3, 4, 5, 6 ] ) == Just ( fromListWithHead 2 [ 4, 6 ] )
filter isEven ( fromListWithHead 1 [ 3, 5 ] ) == Nothing

Unchanged functions

get : Int -> Array a -> Maybe a

Same as Array.get

set : Int -> a -> Array a -> Array a

Same as Array.set

push : a -> Array a -> Array a

Same as Array.push

append : Array a -> Array a -> Array a

Same as Array.append

length : Array a -> Int

Same as Array.length

map : (a -> b) -> Array a -> Array b

Same as Array.map

indexedMap : (Int -> a -> b) -> Array a -> Array b

Same as Array.indexedMap

foldl : (a -> b -> b) -> b -> Array a -> b

Same as Array.foldl

foldr : (a -> b -> b) -> b -> Array a -> b

Same as Array.foldr

toList : Array a -> List a

Same as Array.toList

toIndexedList : Array a -> List ( Int, a )

Same as Array.toIndexedList