std.list

A linked list type.

Types

#[derive(Eq, Show)]
type List a = 
    | Nil
    | Cons a (List a)

A linked list type

let list @ { List, ? } = import! std.list
let { assert_neq } = import! std.test

assert_neq (Cons 1 Nil) Nil

Values

let of xs : forall a . Array a -> List a

Constructs a list from an array. Useful to emulate list literals

let { ? } = import! std.effect
let list @ { List, ? } = import! std.list
let { assert_eq, ? } = import! std.test

seq assert_eq (list.of [1, 2]) (Cons 1 (Cons 2 Nil))
let xs : List String = list.of []
assert_eq xs Nil

let many ?alt x : forall a f . [Alternative f] -> f a -> f (List a)

let some ?alt x : forall a f . [Alternative f] -> f a -> f (List a)

let filter predicate xs : forall a . (a -> Bool) -> List a -> List a

Applies predicate to each element in the list and returns a new list containing only of the elements where predicate returns True

let { ? } = import! std.effect
let list @ { List, ? } = import! std.list
let { assert_eq, ? } = import! std.test
seq assert_eq (list.filter (\x -> x /= 2) (list.of [1, 2, 3])) (list.of [1, 3])
assert_eq (list.filter (\x -> False) (list.of [1, 2, 3])) Nil

let sort xs : forall a . [Ord a] -> List a -> List a

Sorts the list using ord.

let list @ { List, ? } = import! std.list
let { assert_eq, ? } = import! std.test
assert_eq (list.sort (list.of [2, 1, 3])) (list.of [1, 2, 3])

#[implicit]
let eq : forall a . [Eq a] -> Eq (List a)

Eq a defines equality (==) on a

#[implicit]
let ord ?ord : forall a . [Ord a] -> Ord (List a)

Ord a defines an ordering on a

#[implicit]
let semigroup : forall a . Semigroup (List a)

Semigroup a represents an associative operation on a. This means the following laws must hold:

  • forall x . append x (append y z) == append (append x y) z

#[implicit]
let monoid : forall a . Monoid (List a)

Monoid a represents an semigroup an which has an identity. This means the following additional laws must hold:

  • forall x . append x empty == x
  • forall x . append empty x == x

#[implicit]
let functor : Functor List

A Functor represents an action on a parameterized type which does not change the structure with the mapped type.

The following laws should hold:

  • map id == id
  • map (f << g) == map f << map g

#[implicit]
let applicative : Applicative List

A Functor with application.

The following laws should hold:

  • wrap id <*> v = v
  • wrap (<<) <*> u <*> v <*> w = u <*> (v <*> w)
  • wrap f <*> wrap x = wrap (f x)
  • u <*> wrap y = wrap (\g -> g x) <*> u

#[implicit]
let alternative : Alternative List

A monoid on applicative functors.

#[implicit]
let monad : Monad List

A generalised interface for imperatively sequencing actions

#[implicit]
let foldable : Foldable List

Operations over a data structure that can be folded which means that a functions gets called on each element to reduce the structure to a single value (Array, List and Map are all Foldable)

#[implicit]
let traversable : Traversable List

#[implicit]
let show ?d : forall a . [Show a] -> Show (List a)

Show a represents a conversion function from a to a readable string.