Ludwig.List

Module Members

all

(Function)

Test if all elements of a Ludwig.List have the specified property.

Example:

allEvens: all(fun(x): x % 2 == 0, [0, 2, 4, 6]) # => True
allOdds:  all(fun(x): x % 2 == 1, [1, 3, 4, 5]) # => False

Type Signature

 fun (fun (a) -> Bool, List<a>) -> Bool
Argument: test
Type: fun ( a) -> Bool
Argument: list
Type: List<a>
Returns:

True if the test function returns True for all elements of list.

Type: Bool

any

(Function)

Test if any elements of a Ludwig.List have the specified property.

Example:

anyOdds:  any(fun(x): x % 2 == 1, [0, 2, 4, 6]) # => False
anyEvens: any(fun(x): x % 2 == 0, [1, 3, 4, 5]) # => True

Type Signature

 fun (fun (a) -> Bool, List<a>) -> Bool
Argument: test
Type: fun ( a) -> Bool
Argument: list
Type: List<a>
Returns:

True if the test function returns True for any elements of list.

Type: Bool

cartesian-product

(Function)

DEPRECATED: Use cartesianProduct instead.

Return the Cartesian Product of two Ludwig.Lists.

Example:

my-product: List.cartesian-product([1, 2], ["a", "b"])   # => [(1, "a"), (1, "b"), (2, "a"), (2, "b")]

Type Signature

 fun (List<a>, List<b>) -> List<(a, b)>
Argument: xs
Type: List<a>
Argument: ys
Type: List<b>
Returns:
Type: List<(a, b)>

cartesianProduct

(Function)

Return the Cartesian Product of two Ludwig.Lists.

Example:

my-product: List.cartesianProduct([1, 2], ["a", "b"])   # => [(1, "a"), (1, "b"), (2, "a"), (2, "b")]

Type Signature

 fun (List<a>, List<b>) -> List<(a, b)>
Argument: xs
Type: List<a>
Argument: ys
Type: List<b>
Returns:
Type: List<(a, b)>

concat

(Function)

Concatenate two Ludwig.Lists and return the resulting Ludwig.List.

Example:

my-list: List.concat([1, 2], [3, 4])   # => [1, 2, 3, 4]

Type Signature

 fun (List<a>, List<a>) -> List<a>
Argument: x
Type: List<a>
Argument: y
Type: List<a>
Returns:
Type: List<a>

cons

(Function)

Append an element to the front of the specified Ludwig.List.

Example:

list1: [2, 3]
list2: List.cons(1, list1) # => [1, 2, 3]
list3: List.cons(1, [])   # => [1]

Type Signature

 fun (a, List<a>) -> List<a>
Argument: x
Type: a
Argument: xs
Type: List<a>
Returns:
Type: List<a>

drop

(Function)

Return the original Ludwig.List without the first n elements. Returns the original Ludwig.List if n is negative and the empty Ludwig.List if length(xs) <= n.

Example:

drop-1: List.drop(2, [1, 2, 3, 4, 5])   # => [3, 4, 5]
drop-2: List.drop(2, [1])               # => []

Using List.drop(n, l) is equivalent to using List.slice {start: n, list: l}.

Type Signature

 fun (Int, List<a>) -> List<a>
Argument: n
Type: Int
Argument: xs
Type: List<a>
Returns:
Type: List<a>

dropNones

(Function)

Takes a Ludwig.List of Ludwig.Optionals and returns a list of just the Ludwig.Optional values (removing the Nones).

Example:

xs: List.dropNones[Optional(1), None, Optional(2)]  # => [1,2]

Type Signature

 fun (List<Optional<a>>) -> List<a>
Argument: xs

The list of optionals.

Type: List<Optional<a>>

Returns:

The list of optional values.

Type: List<a>

elementAt

(Function)

DEPRECATED: Use index instead.

Version of index with the arguments flipped.

Type Signature

 fun (Int, List<a>) -> a
Argument: i

The element index. The Ludwig.List is indexed from 0.

Type: Int

Argument: list

The list to index in.

Type: List<a>

Returns:

The element. An out of bounds index causes a compilation error.

Type: a

equal

(Function)

Test if two List values are equal.

In most cases, you can use the == operator. This function is useful if you want to use a custom equality function for the elements, though.

Example:

x1: List.equal([], [], String.equal)                       # => True
x2: List.equal([1,2], [1,2], fun(x, y): x == y)            # => True
x3: List.equal([42], [1, 2], fun(x, y): x == y)            # => False
x4: List.equal(["foo", "bar"], [], String.equal)           # => False

Type Signature

 fun (List<a>, List<a>, fun (a, a) -> Bool) -> Bool
Argument: this

The first list to compare.

Type: List<a>

Argument: that

The list to compare to this.

Type: List<a>

Argument: test

The function used as the equality test for List elements.

Type: fun ( a, a) -> Bool

Returns:

True if the lists are equal, otherwise False.

Type: Bool

filter

(Function)

Returns a new Ludwig.List containing the elements of the specified Ludwig.List where the provided predicate evaluates to True.

Example:

my-filtered: List.filter(fun(x): x % 2 == 0, [1, 2, 3, 4, 5])   # => [2, 4]

Type Signature

 fun (fun (a) -> Bool, List<a>) -> List<a>
Argument: f
Type: fun ( a) -> Bool
Argument: xs
Type: List<a>
Returns:
Type: List<a>

flatten

(Function)

Flatten a list of lists into one list.

Example:

my-list: List.flatten([[1, 2], [3, 4], [5, 6])  # => [1, 2, 3, 4, 5, 6]

Type Signature

 fun (List<List<a>>) -> List<a>
Argument: lists
Type: List<List<a>>
Returns:
Type: List<a>

fold

(Function)

DEPRECATED: Use reduce instead.

Reduce a Ludwig.List by applying the given binary operation to an initial value and each element of the Ludwig.List from left to right.

Example:

sum: fold(fun(x, y): x + y, 0, [1, 2, 3, 4, 5])   # => 15

Type Signature

 fun (fun (b, a) -> b, b, List<a>) -> b
Argument: f
Type: fun ( b, a) -> b
Argument: z
Type: b
Argument: xs
Type: List<a>
Returns:
Type: b

fold1

(Function)

DEPRECATED: Use reduce instead.

Reduce a Ludwig.List by applying the given binary operation to each element of the Ludwig.List from left to right. The list must contain at least one element.

Example:

sum: fold(fun(x, y): x + y, [1, 2, 3, 4, 5])   # => 15

Type Signature

 fun (fun (b, b) -> b, List<b>) -> b
Argument: f
Type: fun ( b, b) -> b
Argument: xs
Type: List<b>
Returns:
Type: b

index

(Function)

Retrieve a Ludwig.List element by index.

Example:

x: List.index(["the", "quick", "brown", "fox"], 2) # => "brown"

Type Signature

 fun (List<a>, Int) -> a
Argument: list

The list to index in.

Type: List<a>

Argument: i

The element index. The Ludwig.List is indexed from 0.

Type: Int

Returns:

The element. An out of bounds index causes a error.

Type: a

intersperse

(Function)

Insert a separator between elements in the list.

Example: ex1: List.intersperse(”:”, [“a”, “b”, “c”]) # => [“a”, ”:”, “b”, ”:”, “c”]

Type Signature

 fun (a, List<a>) -> List<a>
Argument: sep

The separator.

Type: a

Argument: xs

The list of input elements. @result The interspersed list.

Type: List<a>

Returns:
Type: List<a>

isEmpty

(Function)

Returns True if the Ludwig.List is empty, False otherwise.

Example:

List.isEmpty([])     # => True
List.isEmpty([1, 2]) # => False

Type Signature

 fun (List<a>) -> Bool
Argument: x
Type: List<a>
Returns:
Type: Bool

length

(Function)

Return the length of the specified Ludwig.List.

Example:

my-len: List.length([1, 2, 3])   # => 3

Type Signature

 fun (List<a>) -> Int
Argument: xs
Type: List<a>
Returns:
Type: Int

map

(Function)

Returns a new Ludwig.List constructed by applying the specified function to each element in the provided Ludwig.List.

Example:

my-list: List.map(fun(x): x * x, [1, 2, 3, 4, 5]   # => [1, 4, 9, 16, 25]

Type Signature

 fun (fun (b) -> a, List<b>) -> List<a>
Argument: f
Type: fun ( b) -> a
Argument: xs
Type: List<b>
Returns:
Type: List<a>

null

(Function)

DEPRECATED: Use isEmpty instead.

Returns True if the Ludwig.List is empty, False otherwise.

Example:

List.null([])     # => True
List.null([1, 2]) # => False

Type Signature

 fun (List<a>) -> Bool
Argument: x
Type: List<a>
Returns:
Type: Bool

reduce

(Function)

Reduce a Ludwig.List by applying the given binary operation to an initial value and each element of the Ludwig.List from left to right.

Example:

sum: List.reduce(fun(x, y): x + y, 0, [1, 2, 3, 4, 5])   # => 15

Type Signature

 fun (fun (b, a) -> b, b, List<a>) -> b
Argument: f
Type: fun ( b, a) -> b
Argument: z
Type: b
Argument: xs
Type: List<a>
Returns:
Type: b

safeElementAt

(Function)

DEPRECATED: Use safeIndex instead.

Version of safeIndex with arguments flipped.

Type Signature

 fun (Int, List<a>) -> Optional<a>
Argument: i

The element index. The Ludwig.List is indexed from 0.

Type: Int

Argument: list

The list to index in.

Type: List<a>

Returns:

The element wrapped in an Ludwig.Optional. Returns None on an out of bounds index.

Type: Optional<a>

safeIndex

(Function)

Retrieve a Ludwig.List element by index.

Example:

x: List.safeIndex(["the", "quick", "brown", "fox"], 2)        # => Optional("brown")
y: List.safeIndex(["the", "quick", "brown", "fox", negate(1)] # => None
z: List.safeIndex([], 2)                                      # => None

Type Signature

 fun (List<a>, Int) -> Optional<a>
Argument: list

The list to index in.

Type: List<a>

Argument: i

The element index. The Ludwig.List is indexed from 0.

Type: Int

Returns:

The element wrapped in an Ludwig.Optional. Returns None on an out of bounds index.

Type: Optional<a>

slice

(Function)

Take a sub-list of a list.

Example:

x01: List.slice {start: 2, list: [1, 2, 3, 4]} # => [3, 4]

This function will never throw an error. If start is beyond the length of the original list, the empty list will be returned. If length is too long, the list will be trimmed so it still returns as many elements as possible.

Type Signature

 fun { start: Optional<Int>,
       length: Optional<Int>,
       list: List<a> } -> List<a>
Argument: start
Type: Optional<Int>
Argument: length
Type: Optional<Int>
Argument: list

The list to slice. @result The requested sublist.

Type: List<a>

Returns:
Type: List<a>

tail

(Function)

Returns the specified Ludwig.List sans the head element wrapped in an Ludwig.Optional. Returns None if the Ludwig.List is empty.

Example:

my-tail: List.tail([1, 2, 3])   # => Optional([2, 3])
singleton-tail: List.tail([1])  # => []
empty-tail: List.tail([])       # => None

Using List.tail(l) is equivalent to using List.slice {start: 1, list: l}.

Type Signature

 fun (List<a>) -> Optional<List<a>>
Argument: xs
Type: List<a>
Returns:
Type: Optional<List<a>>

take

(Function)

Return a Ludwig.List of the first n elements of the specified Ludwig.List. Returns the original Ludwig.List if length(xs) <= n.

Example:

take1: List.take(2, [1, 2, 3, 4, 5])  # => [1, 2]
take2: List.take(2, [1])              # => [1]

Using List.take(n, l) is equivalent to using List.slice {length: n, list: l}.

Type Signature

 fun (Int, List<a>) -> List<a>
Argument: n
Type: Int
Argument: xs
Type: List<a>
Returns:
Type: List<a>

uncons

(Function)

Split the specified Ludwig.List into a tuple containing the head and tail of the Ludwig.List wrapped in an Ludwig.Optional. Returns None if the Ludwig.List is empty.

Example:

uncons1: List.uncons([1, 2, 3])   # => Optional((1, [2, 3]))
uncons2: List.uncons([1])         # => Optional((1, []))
uncons3: List.uncons([])          # => None

Type Signature

 fun (List<a>) -> Optional<(a, List<a>)>
Argument: xs
Type: List<a>
Returns:
Type: Optional<(a, List<a>)>

zip

(Function)

Join together two lists as a list of tuples. Unpaired elements are discarded.

Example: ex1: List.zip([1, 2, 3], [“a”, “b”]) # => [(1, “a”), (2, “b”)]

Type Signature

 fun (List<a>, List<b>) -> List<(a, b)>
Argument: xs

The first list of input elements.

Type: List<a>

Argument: ys

The second list of input elements. @result A list with tuples.

Type: List<b>

Returns:
Type: List<(a, b)>