nish/array

The array methods that take a callback, and slice, in Nish: each a plain loop, compiled once per callback.

import {
  every,
  everyIndexed,
  filter,
  filterIndexed,
  findIndex,
  findIndexIndexed,
  forEach,
  forEachIndexed,
  map,
  mapIndexed,
  slice,
  sliceAll,
  sliceFrom,
  some,
  someIndexed,
} from "nish/array";
export const main = (): i32 => {
  const xs: i32[] = [3, 1, 2];
  const doubled = xs.map((x) => x * 2); // [6, 2, 4]
  const big = xs.filter((x) => x > 1); // [3, 2]
  return doubled[0] + big.length + xs.findIndex((x) => x === 2); // 6 + 2 + 2
};
A program does not need to import this module

A .map(...), .filter(...), .some(...), .every(...), .findIndex(...), .forEach(...) or .slice(...) call on an array is what loads it (the checker's importArrayMethods), and the checker routes the call to the template here that matches it: xs.map(f) is map(xs, f) when f takes one parameter and mapIndexed(xs, f) when it takes two. f is a compile-time function parameter, so each callback is an instantiation of its own whose body calls it directly — there is no function pointer, and importing a template here by name and calling it gives the same code as the method. Under Node, --emit-ts leaves map, filter, some, every, findIndex and forEach as the platform's own Array.prototype methods, writes slice as the runtime's checked sliceArray, and copies an answer that holds records inline with copyRecords, so nothing here runs there.

Same answers as Node

Each walks the array from index 0 upward and calls the callback once per element, in order, until the length it had when the call began, as Array.prototype does; some, every and findIndex stop at the first element that decides the answer. A callback that shrinks the array ends the walk at the new length, where JavaScript goes on and skips the missing indices.

Functions

every

Whether pred answers true for every element of xs, stopping at the first that does not: what xs.every(pred) compiles to when pred takes one parameter. An empty array answers true.

const every: <T>(
  xs: readonly T[],
  pred: (x: T) => boolean,
) => boolean
xsreadonly T[]required

the array read; it is not changed.

pred(x: T) => booleanrequired

called in order until it answers false.

returnsboolean

true when no element fails pred.

everyIndexed

Whether pred answers true for every element of xs, given each element and its index: what xs.every(pred) compiles to when pred takes two parameters.

const everyIndexed: <T>(
  xs: readonly T[],
  pred: (x: T, i: number) => boolean,
) => boolean
xsreadonly T[]required

the array read; it is not changed.

pred(x: T, i: number) => booleanrequired

called in order, with the element and its index, until it answers false.

returnsboolean

true when no element fails pred.

filter

The elements of xs for which pred answers true, in order, in a new array: what xs.filter(pred) compiles to when pred takes one parameter.

const filter: <T>(
  xs: readonly T[],
  pred: (x: T) => boolean,
) => T[]
xsreadonly T[]required

the array read; it is not changed.

pred(x: T) => booleanrequired

called once per element, in order.

returnsT[]

a new array of the elements kept.

filterIndexed

The elements of xs for which pred answers true, given each element and its index: what xs.filter(pred) compiles to when pred takes two parameters.

const filterIndexed: <T>(
  xs: readonly T[],
  pred: (x: T, i: number) => boolean,
) => T[]
xsreadonly T[]required

the array read; it is not changed.

pred(x: T, i: number) => booleanrequired

called once per element, in order, with the element and its index.

returnsT[]

a new array of the elements kept.

findIndex

The index of the first element of xs for which pred answers true: what xs.findIndex(pred) compiles to when pred takes one parameter.

const findIndex: <T>(
  xs: readonly T[],
  pred: (x: T) => boolean,
) => number
xsreadonly T[]required

the array read; it is not changed.

pred(x: T) => booleanrequired

called in order until it answers true.

returnsnumber

the first such index, or -1 when there is none.

findIndexIndexed

The index of the first element of xs for which pred answers true, given each element and its index: what xs.findIndex(pred) compiles to when pred takes two parameters.

const findIndexIndexed: <T>(
  xs: readonly T[],
  pred: (x: T, i: number) => boolean,
) => number
xsreadonly T[]required

the array read; it is not changed.

pred(x: T, i: number) => booleanrequired

called in order, with the element and its index, until it answers true.

returnsnumber

the first such index, or -1 when there is none.

forEach

Calls f with each element of xs, in order: what xs.forEach(f) compiles to when f takes one parameter. The walk stops at the length the array had when the call began, so an element the callback pushes is not visited, and at the current length, so one it pops is not either, as Array.prototype.forEach does.

const forEach: <T>(
  xs: readonly T[],
  f: (x: T) => void,
) => void
xsreadonly T[]required

the array read; the callback may change it.

f(x: T) => voidrequired

called once per element, in order.

forEachIndexed

Calls f with each element of xs and its index, in order: what xs.forEach(f) compiles to when f takes two parameters. The walk stops where forEach's does.

const forEachIndexed: <T>(
  xs: readonly T[],
  f: (x: T, i: number) => void,
) => void
xsreadonly T[]required

the array read; the callback may change it.

f(x: T, i: number) => voidrequired

called once per element, in order, with the element and its index.

map

The elements of xs, each passed through f, in a new array of the same length: what xs.map(f) compiles to when f takes one parameter.

const map: <T, U>(xs: readonly T[], f: (x: T) => U) => U[]
xsreadonly T[]required

the array read; it is not changed.

f(x: T) => Urequired

called once per element, in order.

returnsU[]

a new array whose element i is f(xs[i]).

mapIndexed

The elements of xs, each passed through f with its index: what xs.map(f) compiles to when f takes two parameters.

const mapIndexed: <T, U>(
  xs: readonly T[],
  f: (x: T, i: number) => U,
) => U[]
xsreadonly T[]required

the array read; it is not changed.

f(x: T, i: number) => Urequired

called once per element, in order, with the element and its index.

returnsU[]

a new array whose element i is f(xs[i], i).

slice

A copy of xs[start..end) in a new array: what xs.slice(start, end) compiles to, with string slice's rule. The range must satisfy 0 <= start <= end <= xs.length, and anything else panics with slice out of range: [start, end) of length n and exits 1, where JavaScript counts a negative end back from the length and clamps the rest. A float bound is truncated toward zero and saturates (NaN to 0, an infinity to the i64 limit), natively and under Node alike.

const slice: <T>(
  xs: readonly T[],
  start: number,
  end: number,
) => T[]
xsreadonly T[]required

the array copied from; it is not changed.

startnumberrequired

the first index copied.

endnumberrequired

the index after the last copied.

returnsT[]

a new array of end - start elements.

sliceAll

A copy of the whole of xs: what xs.slice() compiles to.

const sliceAll: <T>(xs: readonly T[]) => T[]
xsreadonly T[]required

the array copied; it is not changed.

returnsT[]

a new array with the same elements.

sliceFrom

A copy of xs[start..): what xs.slice(start) compiles to. start must satisfy 0 <= start <= xs.length, or the call panics as slice does.

const sliceFrom: <T>(xs: readonly T[], start: number) => T[]
xsreadonly T[]required

the array copied from; it is not changed.

startnumberrequired

the first index copied.

returnsT[]

a new array of the elements from start on.

some

Whether pred answers true for at least one element of xs, stopping at the first that does: what xs.some(pred) compiles to when pred takes one parameter. An empty array answers false.

const some: <T>(
  xs: readonly T[],
  pred: (x: T) => boolean,
) => boolean
xsreadonly T[]required

the array read; it is not changed.

pred(x: T) => booleanrequired

called in order until it answers true.

returnsboolean

true when some element satisfies pred.

someIndexed

Whether pred answers true for at least one element of xs, given each element and its index: what xs.some(pred) compiles to when pred takes two parameters.

const someIndexed: <T>(
  xs: readonly T[],
  pred: (x: T, i: number) => boolean,
) => boolean
xsreadonly T[]required

the array read; it is not changed.

pred(x: T, i: number) => booleanrequired

called in order, with the element and its index, until it answers true.

returnsboolean

true when some element satisfies pred.