﻿ first – Algosim documentation
Algosim documentation: first

# first

Returns the first or the N first elements of an ordered container.

## Syntax

• `first(X)`

• `X` is an ordered container

The returned value is an lvalue.

• `first(X, n)`

• `X` is an ordered container

• `n` is a non-negative integer

## Description

• If `X` is any ordered container (string, vector, matrix, list, structure, pixmap, or binary data object), then `first(X)` returns the first element of `X`.

• `first(X, n)` returns the first `n` elements of `X`, in order. If `X` is a linear container, then the type of the result is the type of `X`. Otherwise (if `X` is a matrix or a pixmap) the result is a list.

## Examples

`sort(frequencies(compute(RandomInt(1000000)^2, n, 1, 1000000) @ digits @ first))`
```(1, 192164)
(2, 146925)
(3, 123873)
(4, 109158)
(5, 98564)
(6, 90439)
(7, 84566)
(8, 79092)
(9, 75219)
```
`Alice ≔ ExampleData("Alice in Wonderland") \ 50`
```ALICE’S ADVENTURES IN WONDERLAND

Lewis Carroll
…
```
`sort(frequencies(words(Alice) @ characters @ first @ UpperCase))`
```(A, 3358)
(B, 984)
(C, 903)
(D, 829)
(E, 378)
(F, 742)
(G, 574)
(H, 1575)
(I, 1968)
(J, 120)
(K, 262)
(L, 747)
(M, 897)
(N, 574)
(O, 1430)
(P, 493)
(Q, 186)
(R, 491)
(S, 2530)
(T, 4428)
(U, 262)
(V, 238)
(W, 1864)
(X, 3)
(Y, 550)
(Z, 2)
```
`first(ColorGradient(1, 10, "white", "black"), 3)`
```rgba(1.000, 1.000, 1.000, 1.000)
rgba(0.890, 0.890, 0.890, 1.000)
rgba(0.776, 0.776, 0.776, 1.000)
```