/Pony

# Cons[A: A]

[Source]

A list with a head and a tail, where the tail can be empty.

```class val Cons[A: A] is
```

## Constructors

### create

[Source]

```new val create(
a: val->A,
t: (Cons[A] val | Nil[A] val))
: Cons[A] val^
```

#### Parameters

• a: val->A
• t: (Cons[A] val | Nil[A] val)

## Public Functions

### size

[Source]

Returns the size of the list.

```fun box size()
: USize val
```

### apply

[Source]

Returns the i-th element of the list. Errors if the index is out of bounds.

```fun box apply(
i: USize val)
: val->A ?
```

• val->A ?

### values

[Source]

Returns an iterator over the elements of the list.

```fun box values()
: Iterator[val->A] ref^
```

### is_empty

[Source]

Returns a Bool indicating if the list is empty.

```fun box is_empty()
: Bool val
```

### is_non_empty

[Source]

Returns a Bool indicating if the list is non-empty.

```fun box is_non_empty()
: Bool val
```

#### Returns

[Source]

Returns the head of the list.

```fun box head()
: val->A
```

• val->A

### tail

[Source]

Returns the tail of the list.

```fun box tail()
: (Cons[A] val | Nil[A] val)
```

### reverse

[Source]

Builds a new list by reversing the elements in the list.

```fun val reverse()
: (Cons[A] val | Nil[A] val)
```

### prepend

[Source]

Builds a new list with an element added to the front of this list.

```fun val prepend(
a: val->A!)
: Cons[A] val
```

• a: val->A!

### concat

[Source]

Builds a new list that is the concatenation of this list and the provided list.

```fun val concat(
l: (Cons[A] val | Nil[A] val))
: (Cons[A] val | Nil[A] val)
```

### map[B: B]

[Source]

Builds a new list by applying a function to every member of the list.

```fun val map[B: B](
f: {(val->A): val->B}[A, B] box)
: (Cons[B] val | Nil[B] val)
```

#### Parameters

• f: {(val->A): val->B}[A, B] box

### flat_map[B: B]

[Source]

Builds a new list by applying a function to every member of the list and using the elements of the resulting lists.

```fun val flat_map[B: B](
f: {(val->A): List[B]}[A, B] box)
: (Cons[B] val | Nil[B] val)
```

#### Parameters

• f: {(val->A): List[B]}[A, B] box

### for_each

[Source]

Applies the supplied function to every element of the list in order.

```fun val for_each(
f: {(val->A)}[A] box)
: None val
```

#### Parameters

• f: {(val->A)}[A] box

### filter

[Source]

Builds a new list with those elements that satisfy a provided predicate.

```fun val filter(
f: {(val->A): Bool}[A] box)
: (Cons[A] val | Nil[A] val)
```

#### Parameters

• f: {(val->A): Bool}[A] box

### fold[B: B]

[Source]

Folds the elements of the list using the supplied function.

```fun val fold[B: B](
f: {(B, val->A): B^}[A, B] box,
acc: B)
: B
```

#### Parameters

• f: {(B, val->A): B^}[A, B] box
• acc: B

• B

### every

[Source]

Returns true if every element satisfies the provided predicate, false otherwise.

```fun val every(
f: {(val->A): Bool}[A] box)
: Bool val
```

#### Parameters

• f: {(val->A): Bool}[A] box

### exists

[Source]

Returns true if at least one element satisfies the provided predicate, false otherwise.

```fun val exists(
f: {(val->A): Bool}[A] box)
: Bool val
```

#### Parameters

• f: {(val->A): Bool}[A] box

### partition

[Source]

Builds a pair of lists, the first of which is made up of the elements satisfying the supplied predicate and the second of which is made up of those that do not.

```fun val partition(
f: {(val->A): Bool}[A] box)
: ((Cons[A] val | Nil[A] val) , (Cons[A] val | Nil[A] val))
```

#### Parameters

• f: {(val->A): Bool}[A] box

### drop

[Source]

Builds a list by dropping the first n elements.

```fun val drop(
n: USize val)
: (Cons[A] val | Nil[A] val)
```

### drop_while

[Source]

Builds a list by dropping elements from the front of the list until one fails to satisfy the provided predicate.

```fun val drop_while(
f: {(val->A): Bool}[A] box)
: (Cons[A] val | Nil[A] val)
```

#### Parameters

• f: {(val->A): Bool}[A] box

### take

[Source]

Builds a list of the first n elements.

```fun val take(
n: USize val)
: (Cons[A] val | Nil[A] val)
```

### take_while

[Source]

Builds a list of elements satisfying the provided predicate until one does not.

```fun val take_while(
f: {(val->A): Bool}[A] box)
: (Cons[A] val | Nil[A] val)
```

#### Parameters

• f: {(val->A): Bool}[A] box

## Private Functions

### _reverse

[Source]

Private helper for reverse, recursively working on elements.

```fun val _reverse(
l: (Cons[A] val | Nil[A] val),
acc: (Cons[A] val | Nil[A] val))
: (Cons[A] val | Nil[A] val)
```

### _concat

[Source]

Private helper for concat that recursively builds the new list.

```fun val _concat(
l: (Cons[A] val | Nil[A] val),
acc: (Cons[A] val | Nil[A] val))
: (Cons[A] val | Nil[A] val)
```

### _map[B: B]

[Source]

Private helper for map, recursively applying function to elements.

```fun box _map[B: B](
l: (Cons[A] val | Nil[A] val),
f: {(val->A): val->B}[A, B] box,
acc: (Cons[B] val | Nil[B] val))
: (Cons[B] val | Nil[B] val)
```

#### Parameters

• l: (Cons[A] val | Nil[A] val)
• f: {(val->A): val->B}[A, B] box
• acc: (Cons[B] val | Nil[B] val)

### _flat_map[B: B]

[Source]

Private helper for flat_map, recursively working on elements.

```fun box _flat_map[B: B](
l: (Cons[A] val | Nil[A] val),
f: {(val->A): List[B]}[A, B] box,
acc: (Cons[B] val | Nil[B] val))
: (Cons[B] val | Nil[B] val)
```

#### Parameters

• l: (Cons[A] val | Nil[A] val)
• f: {(val->A): List[B]}[A, B] box
• acc: (Cons[B] val | Nil[B] val)

### _rev_prepend[B: B]

[Source]

Prepends l in reverse order onto target

```fun tag _rev_prepend[B: B](
l: (Cons[B] val | Nil[B] val),
target: (Cons[B] val | Nil[B] val))
: (Cons[B] val | Nil[B] val)
```

### _for_each

[Source]

Private helper for for_each, recursively working on elements.

```fun box _for_each(
l: (Cons[A] val | Nil[A] val),
f: {(val->A)}[A] box)
: None val
```

#### Parameters

• l: (Cons[A] val | Nil[A] val)
• f: {(val->A)}[A] box

### _filter

[Source]

Private helper for filter, recursively working on elements, keeping those that match the predicate and discarding those that don't.

```fun box _filter(
l: (Cons[A] val | Nil[A] val),
f: {(val->A): Bool}[A] box,
acc: (Cons[A] val | Nil[A] val))
: (Cons[A] val | Nil[A] val)
```

#### Parameters

• l: (Cons[A] val | Nil[A] val)
• f: {(val->A): Bool}[A] box
• acc: (Cons[A] val | Nil[A] val)

### _fold[B: B]

[Source]

Private helper for fold, recursively working on elements.

```fun val _fold[B: B](
l: (Cons[A] val | Nil[A] val),
f: {(B, val->A): B^}[A, B] box,
acc: B)
: B
```

#### Parameters

• l: (Cons[A] val | Nil[A] val)
• f: {(B, val->A): B^}[A, B] box
• acc: B

• B

### _every

[Source]

Private helper for every, recursively testing predicate on elements, returning false immediately on an element that fails to satisfy the predicate.

```fun box _every(
l: (Cons[A] val | Nil[A] val),
f: {(val->A): Bool}[A] box)
: Bool val
```

#### Parameters

• l: (Cons[A] val | Nil[A] val)
• f: {(val->A): Bool}[A] box

### _exists

[Source]

Private helper for exists, recursively testing predicate on elements, returning true immediately on an element satisfying the predicate.

```fun box _exists(
l: (Cons[A] val | Nil[A] val),
f: {(val->A): Bool}[A] box)
: Bool val
```

#### Parameters

• l: (Cons[A] val | Nil[A] val)
• f: {(val->A): Bool}[A] box