Cindiary / elm-not-empty / NotEmpty.List


type alias List item =
( item
, Internal.CoreTypes.CoreList item 
)

Not empty version of List

Create

singleton : a -> List a

A not empty list containing only the given value.

toList ( singleton "a" ) == [ "a" ]

repeat : Basics.Int -> a -> List a

Create a not empty list with n copies of a value.

Will always include at least 1 value.

toList ( repeat 4 "Value" ) == [ "Value", "Value", "Value", "Value" ]
toList ( repeat 0 "Test" ) == [ "Test" ]

range : Basics.Int -> Basics.Int -> List Basics.Int

Create a list of numbers, every element increasing by one. You give the lowest and highest number that should be in the list.

Will always include the low value.

toList ( range 3 6 ) == [ 3, 4, 5, 6 ]
toList ( range 3 3 ) == [ 3 ]
toList ( range 6 3 ) == [ 6 ]

withHead : a -> Internal.CoreTypes.CoreList a -> List a

A not empty list with the given head and tail

ls = withHead 5 [ 4, 3 ]
toList ls == [ 5, 4, 3 ]
head ls == 5
tail ls == [ 4, 3 ]

cons : a -> List a -> List a

Add an element to the front of a not empty list.

cons 1 ( singleton 2 ) == withHead 1 [ 2 ]

Transforming from and to regular lists

fromList : Internal.CoreTypes.CoreList a -> Maybe (List a)

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

fromList [] == Nothing
fromList [ 1, 2, 3 ] == Just ( withHead 1 [ 2, 3 ] )

toList : List a -> Internal.CoreTypes.CoreList a

Convert a not empty list into a regular list

toList ( withHead 1 [ 2, 3 ] ) == [ 1, 2, 3 ] toList ( singleton 1 ) == [ 1 ]

New functions

unCons : List a -> ( a, Internal.CoreTypes.CoreList a )

Deconstruct a not empty list into it's head and tail

unCons ( singleton 5 ) = ( 5, [] )
unCons ( withHead 1 [ 2, 3 ] ) = ( 1, [ 2, 3 ] )

last : List a -> a

Extract the last element from a not empty list.

last ( withHead 1 [ 2, 3 ] ) == 3
last ( singleton 1 ) == 1

hasTail : List a -> Basics.Bool

Returns wether the list has a tail with elements

hasTail ( withHead 1 [ 2, 3 ] ) == True
hasTail ( singleton 1 ) == False
hasTail ( withHead 1 [] ) == False

appendList : List a -> Internal.CoreTypes.CoreList a -> List a

Appends a regular list to a not empty list

appendList ( singleton 1 ) [ 2, 3 ] == withHead 1 [ 2, 3 ]
appendList ( withHead 1 [ 2, 3 ] ) [ 4, 5 ] == withHead 1 [ 2, 3, 4, 5 ]

appendToList : Internal.CoreTypes.CoreList a -> List a -> List a

Appends a not empty list to a regular list

appendToList [ 1, 2 ] ( singleton 3 ) == withHead 1 [ 2, 3 ]
appendToList [ 1, 2, 3 ] ( withHead 4 [ 5 ] ) == withHead 1 [ 2, 3, 4, 5 ]
appendToList [] ( withHead 1 [ 2, 3 ] ) == withHead 1 [ 2, 3 ]

Modified functions

head : List a -> a

Same as List.head but will always always return a value

tail : List a -> Internal.CoreTypes.CoreList a

Same as List.tail but will always return a value

tail ( withHead 1 [ 2, 3 ] ) == [ 2, 3 ]
tail ( singleton 1 ) == []

maximum : List comparable -> comparable

Same as List.maximum but will always return a value

minimum : List comparable -> comparable

Same as List.minimum but will always return a value

take : Basics.Int -> List a -> List a

Same as List.take but will always include the first element

take 2 ( withHead 1 [ 2, 3 ] ) == withHead 1 [ 2 ]
take 0 ( withHead 1 [ 2, 3 ] ) == singleton 1

drop : Basics.Int -> List a -> List a

Same as List.drop but will always include the last element

drop 2 ( withHead 1 [ 2, 3 ] ) == withHead 2 [ 3 ]
drop 0 ( withHead 1 [ 2, 3 ] ) == singleton 3

filter : (a -> Basics.Bool) -> List a -> Maybe (List a)

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

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

filterMap : (a -> Maybe b) -> List a -> Maybe (List b)

Same as List.filterMap but will return Nothing if all elements return Nothing

numbers1 : Maybe ( List Int )
numbers1 =
  filterMap String.toInt ( withHead "3" [ "hi", "12", "4th", "May" ] )

numbers2 : Maybe ( List Int )
numbers2 =
  filterMap String.toInt ( withHead "hello" [ "world" ] )

-- numbers1 == Just [3, 12]
-- numbers2 == Nothing

partition : (a -> Basics.Bool) -> List a -> PartitionResult a

Same as List.partition but will return a PartitionResult since either but not both lists could be empty.


type PartitionResult a
    = NoMatches (List a)
    | OnlyMatches (List a)
    | Both (List a) (List a)

Return type for NotEmpty.List.partition

Will be NoMatches when no elements in the list satisfy the test.

Will be OnlyMatches when all elements in the list satisfy the test.

Will be Both if there a both elements that do and don't satisfy the test. The first list are the elements that satisfy the test and the second list are the elements that don't.

Unchanged functions

length : List a -> Basics.Int

Same as List.length

reverse : List a -> List a

Same as List.reverse

member : a -> List a -> Basics.Bool

Same as List.member

all : (a -> Basics.Bool) -> List a -> Basics.Bool

Same as List.all

any : (a -> Basics.Bool) -> List a -> Basics.Bool

Same as List.any

sum : List number -> number

Same as List.sum

product : List number -> number

Same as List.product

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

Same as List.map

indexedMap : (Basics.Int -> a -> b) -> List a -> List b

Same as List.indexedMap

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

Same as List.foldl

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

Same as List.foldr

foldl1 : (a -> acc -> acc) -> (a -> acc) -> List a -> acc

Same as List.foldl but instead of supplying a start value you provide a function that produces the start value based on the first element

foldl1 (+) negate ( withHead 1 [ 2, 3 ] ) == 4 foldl1 (::) singleton ( withHead 1 [ 2, 3 ] ) == withHead 3 [ 2, 1 ]

foldr1 : (a -> acc -> acc) -> (a -> acc) -> List a -> acc

Same as List.foldl but instead of supplying a start value you provide a function that produces the start value based on the last element

foldr1 (+) negate ( withHead 1 [ 2, 3 ] ) == 0 foldr1 (::) singleton ( withHead 1 [ 2, 3 ] ) == withHead 1 [ 2, 3 ]

append : List a -> List a -> List a

Same as List.append

concat : List (List a) -> List a

Same as List.concat

concatMap : (a -> List b) -> List a -> List b

Same as List.concatMap

intersperse : a -> List a -> List a

Same as List.intersperse

unzip : List ( a, b ) -> ( List a, List b )

Same as List.unzip

sort : List comparable -> List comparable

Same as List.sort

sortBy : (a -> comparable) -> List a -> List a

Same as List.sortBy

sortWith : (a -> a -> Basics.Order) -> List a -> List a

Same as List.sortWith

map2 : (a -> b -> result) -> List a -> List b -> List result

Same as List.map2

map3 : (a -> b -> c -> result) -> List a -> List b -> List c -> List result

Same as List.map3

map4 : (a -> b -> c -> d -> result) -> List a -> List b -> List c -> List d -> List result

Same as List.map4

map5 : (a -> b -> c -> d -> e -> result) -> List a -> List b -> List c -> List d -> List e -> List result

Same as List.map5