TAKE (Array, Count) TAKE (Array; Count)

Array

The array whose elements should be returned.

Count

Number Number

The number of elements to return. A positive count returns elements from the beginning of the array and a negative count returns elements from the end.

Returns

The first or last elements of the given array.

Returns the first or last elements of the given array. TAKE ({ 10, 20, 30 }, 2) TAKE ({ 10; 20; 30 }; 2) returns the first two elements, { 10, 20 }{ 10; 20 }.

Taking elements from the end

A negative count returns elements from the end of the array instead of from the beginning. This formula returns the last two elements, { 20, 30 }{ 20; 30 }:

TAKE ({ 10, 20, 30 }, - 2)
TAKE ({ 10; 20; 30 }; - 2)

Naming the parameter often makes the intent clearer:

TAKE (SomeArray, Count: 2)
TAKE (SomeArray; Count: 2)

Counts of zero and counts which are too large

A count of zero returns an empty array.

A count larger than the number of elements in the array is not an error. Every element is returned. This formula returns all three elements:

TAKE ({ 10, 20, 30 }, 100)
TAKE ({ 10; 20; 30 }; 100)

TAKE, DROP and FILTER

DROP is the counterpart of this function and removes the elements which TAKE returns. Given the same array and the same count, the two functions divide the array between them.

TAKE and DROP both select elements by position . An author who needs to select elements by condition should instead use FILTER, which tests every element and keeps the ones matching the condition.

Examples

TAKE ({ 10, 20, 30 }, 2)
TAKE ({ 10; 20; 30 }; 2)

Returns { 10, 20 }{ 10; 20 }, the first two elements.

TAKE ({ 10, 20, 30 }, - 2)
TAKE ({ 10; 20; 30 }; - 2)

Returns { 20, 30 }{ 20; 30 }, the last two elements.

TAKE ({ 10, 20, 30 }, 0)
TAKE ({ 10; 20; 30 }; 0)

Returns an empty array.

TAKE ({ 10, 20, 30 }, 100)
TAKE ({ 10; 20; 30 }; 100)

Returns { 10, 20, 30 }{ 10; 20; 30 }, every element. A count larger than the number of elements is not an error.

TAKE (Matrix, RowCount, ColumnCount) TAKE (Matrix; RowCount; ColumnCount)

Matrix

The two-dimensional array whose rows and columns should be returned.

RowCount

Number Number

The number of rows to return. A positive count returns rows from the beginning and a negative count returns rows from the end.

ColumnCount

Number Number

The number of columns to return from every returned row. A positive count returns columns from the beginning and a negative count returns columns from the end.

Returns

The first or last rows and columns of the given two-dimensional array.

Returns the first or last rows and columns of the given two-dimensional array. TAKE ({ { 10, 20 }, { 30, 40 } }, 1, 2) TAKE ({ { 10; 20 }; { 30; 40 } }; 1; 2) returns the first row, { { 10, 20 } }{ { 10; 20 } }.

The two counts are independent of one another. Either may be negative, returning rows or columns from the end instead of from the beginning. This formula returns the first row and the last column:

TAKE ({ { 10, 20 }, { 30, 40 } }, 1, - 1)
TAKE ({ { 10; 20 }; { 30; 40 } }; 1; - 1)

As both counts must be given, an author who wants every row or every column on one axis passes a count large enough to cover it. Naming the parameters makes this easier to read:

TAKE (SomeMatrix, RowCount: 1, ColumnCount: 2)
TAKE (SomeMatrix; RowCount: 1; ColumnCount: 2)

If either count is zero, an empty array is returned.

Examples

TAKE ({ { 10, 20 }, { 30, 40 } }, 1, 2)
TAKE ({ { 10; 20 }; { 30; 40 } }; 1; 2)

Returns { { 10, 20 } }{ { 10; 20 } }, the first row with both of its columns.

TAKE ({ { 10, 20 }, { 30, 40 } }, 2, 1)
TAKE ({ { 10; 20 }; { 30; 40 } }; 2; 1)

Returns { { 10 }, { 30 } }{ { 10 }; { 30 } }, the first column of both rows.

TAKE ({ { 10, 20 }, { 30, 40 } }, 1, - 1)
TAKE ({ { 10; 20 }; { 30; 40 } }; 1; - 1)

Returns { { 20 } }{ { 20 } }, the first row and the last column.

fxEngine Shell Want this in your own terminal? A standalone version is coming. Get notified.
Loading fxEngine Shell