Module

Ch5

#flip

flip :: forall a b c. (a -> b -> c) -> b -> a -> c

Flips the order of the arguments to a function of two arguments.

flip const 1 2 = const 2 1 = 2

#const

const :: forall a b. a -> b -> a

Returns its first argument and ignores its second.

const 1 "hello" = 1

#apply

apply :: forall a b. (a -> b) -> a -> b

Applies a function to an argument.

apply (\f -> f + 1) 2 = 3

#($)

Operator alias for Ch5.apply (right-associative / precedence 0)

Applies a function to an argument: the reverse of (#).

length $ fromMaybe [] $ tail (1 .. 5) = 4

#applyFlipped

applyFlipped :: forall a b. a -> (a -> b) -> b

Applies an argument to a function.

applyFlipped 2 (\f -> f + 1) = 3

#(#)

Operator alias for Ch5.applyFlipped (left-associative / precedence 1)

Applies an argument to a function: the reverse of ($).

tail (1 .. 5) # fromMaybe [] # length = 4

#singleton

singleton :: forall a. a -> List a

Create a list with a single element.

singleton "xyz" = ("xyz" : Nil)
singleton 2 = (2 : Nil)

#null

null :: forall a. List a -> Boolean

Test whether a list is empty.

null Nil = true
null ("xyz" : Nil) = false

#snoc

snoc :: forall a. List a -> a -> List a

Append an element to the end of a list, creating a new list.

snoc (1 : 2 : Nil) 3 = (1 : 2 : 3 : Nil)

#length

length :: forall a. List a -> Int

Get the length of a list

length (1 : 2 : 3 : Nil) = 3

#head

head :: List ~> Maybe

Get the first element in a list, or Nothing if the list is empty.

head (1 : 2 : 3 : Nil) = 1

#tail

tail :: forall a. List a -> Maybe (List a)

Get all but the first element of a list, or Nothing if the list is empty.

tail (1 : 2 : 3 : Nil) = (2 : 3 : Nil)

#last

last :: List ~> Maybe

Get the last element in a list, or Nothing if the list is empty.

last (1 : 2 : 3 : Nil) = Just 3
last (Nil) = Nothing

#init

init :: forall a. List a -> Maybe (List a)

Get all but the last element of a list, or Nothing if the list is empty.

init (1 : 2 : 3 : Nil) = Just (1 : 2 : Nil)
init (Nil) = Nothing

#uncons

uncons :: forall a. List a -> Maybe { head :: a, tail :: List a }

Break a list into its first element, and the remaining elements, or Nothing if the list is empty.

uncons (1 : 2 : 3 : Nil) = Just { head: 1, tail: (2 : 3 : Nil)}

#index

index :: forall a. List a -> Int -> Maybe a

Get the element at the specified index, or Nothing if the index is out-of-bounds.

index (1 : Nil) 4 = Nothing
index (1 : 2 : 3 : Nil) 1 = Just 2
index (Nil :: List Unit) 0 = Nothing

#(!!)

Operator alias for Ch5.index (left-associative / precedence 8)

Operator alias for Data.List.index (left-associative / precedence 8)

(1 : Nil) !! 4 = Nothing
(1 : 2 : 3 : Nil) !! 1 = Just 2
(Nil :: List Unit) !! 0 = Nothing

#findIndex

findIndex :: forall a. (a -> Boolean) -> List a -> Maybe Int

Find the first index for which a predicate holds.

findIndex (_ >= 2) (1 : 2 : 3 : Nil) = Just 1
findIndex (_ >= 99) (1 : 2 : 3 : Nil) = Nothing
findIndex (10 /= _) (Nil :: List Int) = Nothing

#findLastIndex

findLastIndex :: forall a. (a -> Boolean) -> List a -> Maybe Int

Find the last index for which a predicate holds.

findIndex (_ == 10) (Nil :: List Int) = Nothing
findIndex (_ == 10) (10 : 5 : 10 : -1 : 2 : 10 : Nil) = Just 5
findIndex (_ == 10) (11 : 12 : Nil) = Nothing

#reverse

reverse :: List ~> List

Reverse a list.

reverse (1 : 2 : 3 : Nil) = (3 : 2 : 1 : Nil)

#concat

concat :: forall a. List (List a) -> List a

Flatten a list of lists.

concat ((1 : 2 : 3 : Nil) : (4 : 5 : Nil) : (6 : Nil) : (Nil) : Nil) = (1 : 2 : 3 : 4 : 5 : 6 : Nil)

#filter

filter :: forall a. (a -> Boolean) -> List a -> List a

Filter a list, keeping the elements which satisfy a predicate function.

filter (4 > _) (1 : 2 : 3 : 4 : 5 : 6 : Nil) = (1 : 2 : 3 : Nil)

#catMaybes

catMaybes :: forall a. List (Maybe a) -> List a

Filter a list of optional values, keeping only the elements which contain a value.

catMaybes (Just 1 : Nothing : Just 2 : Nothing : Nothing : Just 3 : Nil) = (1 : 2 : 3 : Nil)

#range

range :: Int -> Int -> List Int

Create a list containing a range of integers, including both endpoints.

range 1 5 = (1 : 2 : 3 : 4 : 5 : Nil)
range 3 (-3) = (3 : 2 : 1 : 0 : -1 : -2 : -3 : Nil)

#take

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

Take the specified number of elements from the front of a list.

take 5 (12 : 13 : 14 : Nil) = (12 : 13 : 14 : Nil)
take 5 (1 : 2 : 3 : 4 : 5 : 6 : Nil) = (1 : 2 : 3 : 4 : 5 : Nil)

#drop

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

Drop the specified number of elements from the front of a list.

drop 2 (1 : 2 : 3 : 4 : 5 : Nil) = (3 : 4 : 5 : Nil)

#takeWhile

takeWhile :: forall a. (a -> Boolean) -> List a -> List a

Take those elements from the front of a list which match a predicate.

takeWhile (_ > 3) (5 : 4 : 3 : 99 : 101 : Nil) = (5 : 4 : Nil)
takeWhile (_ == -17) (1 : 2 : 3 : Nil) = (Nil)

#dropWhile

dropWhile :: forall a. (a -> Boolean) -> List a -> List a

Drop those elements from the front of a list which match a predicate.

dropWhile (_ > 3) (5 : 4 : 3 : 99 : 101 : Nil) = (3 : 99 : 101 : Nil)
dropWhile (_ == -17) (1 : 2 : 3 : Nil) = (1 : 2 : 3 : Nil)

#takeEnd

takeEnd :: forall a. Int -> List a -> List a

Take the specified number of elements from the end of a list.

takeEnd 3 (1 : 2 : 3 : 4 : 5 : 6 : Nil) = (4 : 5 : 6 : Nil)
takeEnd 10 (1 : Nil) = (1 : Nil)

#dropEnd

dropEnd :: forall a. Int -> List a -> List a

Drop the specified number of elements from the end of a list.

dropEnd 3 (1 : 2 : 3 : 4 : 5 : 6 : Nil) = (1 : 2 : 3 : Nil)
dropEnd 10 (1 : Nil) = (Nil)

#zip

zip :: forall a b. List a -> List b -> List (Tuple a b)

Collect pairs of elements at the same positions in two lists.

zip (1 : 2 : 3 : Nil) ("a" : "b" : "c" : "d" : "e" : Nil) = ((Tuple 1 "a") : (Tuple 2 "b") : (Tuple 3 "c") : Nil)
zip ("a" : "b" : "c" : "d" : "e" : Nil) (1 : 2 : 3 : Nil) = ((Tuple "a" 1) : (Tuple "b" 2) : (Tuple "c" 3) : Nil)
zip (Nil :: List Unit) (1 : 2 : Nil) = Nil

#unzip

unzip :: forall a b. List (Tuple a b) -> Tuple (List a) (List b)

Transforms a list of pairs into a list of first components and a list of second components.

unzip ((Tuple 1 "a") : (Tuple 2 "b") : (Tuple 3 "c") : Nil) = Tuple (1 : 2 : 3 : Nil) ("a" : "b" : "c" : "d" : "e" : Nil)
unzip ((Tuple "a" 1) : (Tuple "b" 2) : (Tuple "c" 3) : Nil) = Tuple ("a" : "b" : "c" : "d" : "e" : Nil) (1 : 2 : 3 : Nil)
unzip (Nil :: List (Tuple Unit Unit)) = Tuple Nil Nil

#test

Modules