collections/array/module.protos

Repository
guillermomolina/protos
Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75
Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6
Blob
fff330317d1f31979bfa6ba4d06965dc1e3e47f8
Source
View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.

//! Sequential and isolated-parallel algorithms over ordinary Arrays.

// Dependencies retain ordinary public-member selection in the initializer context.
ArrayTraversal: import("../internal/ArrayTraversal.protos")

ArrayOrdering: import("../internal/ArrayOrdering.protos")

ArrayEdits: import("../internal/ArrayEdits.protos")

ArrayParallel: import("../internal/ArrayParallel.protos")

/// Answers a fresh Array whose element `i` is `transform(array[i])`, applied to a
/// snapshot of `array` in ascending index order.
this.map: ArrayTraversal.slotValue("map")

/// Answers a fresh Array of the elements of a snapshot of `array` for which
/// `predicate(element)` answers `true`, keeping source order. A result other
/// than canonical `true` or `false` signals `InvalidPredicateResult`.
this.filter: ArrayTraversal.slotValue("filter")

/// Answers the lowest index of a snapshot of `array` whose element satisfies
/// `predicate(element)`, or `null` when none does. Elements are tested in
/// ascending order; a result other than canonical `true` or `false` signals
/// `InvalidPredicateResult`.
this.findIndex: ArrayTraversal.slotValue("findIndex")

/// Folds a snapshot of `array` left to right with `reducer(accumulator, element)`.
/// An optional single `initial` value seeds the fold; without one, the first
/// element does, and an empty Array answers `null`. More than one initial
/// value signals an Error.
this.reduce: ArrayTraversal.slotValue("reduce")

/// Answers a fresh Array with the elements of a snapshot of `array` stably sorted
/// by `less(left, right)`. A comparator result other than canonical `true` or
/// `false` signals `InvalidComparatorResult`, and two elements each less than
/// the other signal `InvalidComparatorOrder`. The source is not mutated.
/// When `less` is omitted it defaults to `(left, right) => left < right`, the
/// ordinary `<` protocol of the elements; that default defines no total order
/// over arbitrary values, so elements whose `<` fails or answers a non-Boolean
/// signal exactly as with an explicit comparator. An explicit `null` is an
/// ordinary comparator argument and does not select the default.
this.sort: ArrayOrdering.slotValue("sort")

/// Answers a fresh open Array holding the elements of `array` followed by
/// `value` as one single element, even when `value` is itself an Array. The
/// copy is shallow and `array` is not mutated. A non-Array `array` (one that
/// `Array.recognizes` rejects) signals an Error.
this.append: ArrayEdits.slotValue("append")

/// Answers a fresh open Array holding the elements of `left` followed by the
/// elements of `right`, flattened exactly one level. The copy is shallow,
/// neither operand is mutated, and the result never shares indexed storage
/// with either operand, even when both are the same Array or both are empty.
/// An operand that `Array.recognizes` rejects signals an Error.
this.concat: ArrayEdits.slotValue("concat")

/// Answers a fresh open Array holding the elements of a shallow snapshot of
/// `array` at the half-open index range `[start, end)`. `start` and `end` must
/// be Integers with `0 <= start <= end <= size`; anything else, including a
/// Float, a negative bound, or an arbitrarily large Integer, signals an Error
/// without numeric conversion. The result is never a view or an alias of
/// `array`, even for the full or an empty range.
this.slice: ArrayEdits.slotValue("slice")

/// Answers a fresh open Array holding the elements of `array` with `value`
/// inserted as one single element at `index`, shifting later elements one
/// position up. `index` must be an Integer with `0 <= index <= size`; `size`
/// itself appends. Anything else signals an Error before any element is moved.
/// The copy is shallow and `array` is not mutated.
this.insertAt: ArrayEdits.slotValue("insertAt")

/// Answers a fresh open Array holding the elements of a shallow snapshot of
/// `array` without the element at `index`, keeping the order and identity of
/// the remaining elements. `index` must be an Integer with
/// `0 <= index < size`; anything else, including any index of an empty Array,
/// signals an Error. `array` is not mutated. Unlike `Bytes.removeAt`, this
/// answers a new Array rather than editing a receiver.
this.removeAt: ArrayEdits.slotValue("removeAt")

/// Answers a fresh open Array holding the elements of `array` in reverse
/// order. The copy is shallow, `array` is not mutated, and no comparator or
/// other callback is invoked.
this.reverse: ArrayEdits.slotValue("reverse")

/// Answers the first element of a shallow snapshot whose predicate answers
/// canonical true, or null when none matches. A matched null is ambiguous;
/// use findIndex when the distinction matters.
this.find: ArrayTraversal.slotValue("find")

/// Answers whether a snapshot contains value, using element == value in
/// ascending order. Equality is ordinary dispatch, not hashing or identity.
this.contains: ArrayTraversal.slotValue("contains")

/// Answers true at the first canonical true predicate result, otherwise false.
/// Invalid predicate results signal InvalidPredicateResult.
this.any: ArrayTraversal.slotValue("any")

/// Answers false at the first canonical false predicate result, otherwise true.
/// Invalid predicate results signal InvalidPredicateResult.
this.all: ArrayTraversal.slotValue("all")

/// Answers a fresh open Array with the first min(count, size) elements.
/// Count must be a nonnegative Integer; an oversized count saturates.
this.take: (array, count) => ArrayEdits.take(
    (array, start, end) => slice(array, start, end), array, count
)

/// Answers a fresh open Array after skipping min(count, size) elements.
/// Count must be a nonnegative Integer; an oversized count saturates.
this.drop: (array, count) => ArrayEdits.drop(
    (array, start, end) => slice(array, start, end), array, count
)

// D242: resolve the private SOURCE during MODULE initialization.
// This lexical binding is not a public MODULE export.
ArrayBulk: import("../internal/ArrayBulk.protos")
/// Maps each source element once; snapshots each Array chunk immediately and flattens one level.
this.flatMap: (array, transform) => ArrayBulk.flatMap(array, transform)
/// Answers fresh element pairs up to the shorter Array length, without padding.
this.zip: (left, right) => ArrayBulk.zip(left, right)
/// Splits a snapshot by a strict Boolean predicate into two fresh stable-order Arrays.
this.partition: (array, predicate) => ArrayBulk.partition(array, predicate)
/// Retains first occurrences in source order using ordinary Set hash and equality semantics.
this.distinct: (array) => ArrayBulk.distinct(array)

// Parallel algorithms (D160): Standard Library policy composed only from the
// public isolated-parallel substrate (`Closure.parallel`, `Future`,
// `Future.all`, `ensure`). Every callback runs in isolated P, so it cannot
// capture caller state; explicit trailing `arguments` are the supported way to
// pass data to it, and they cross P exactly like the elements do.
//
// A non-empty call first submits P snapshots of the source elements, the
// callback, and the arguments. That synchronous submission is the input
// snapshot point and signals `NonParallelValue` before any Future is returned.
// Each logical callback invocation then runs in its own isolated P child owned
// by the operation's result Future. Cancelling the result Future cancels
// unfinished children. Failure selection never depends on worker timing.
//
// Arrays, Errors, and other results are always assembled in the caller domain
// and only cross P: P children run nothing but the user callback, and their
// raw outcomes are validated in the caller in ascending index order.

/// Answers a Future of a fresh Array whose element `i` is
/// `worker(array[i], arguments...)`, evaluated in isolated parallel execution.
/// Results keep source order; the lowest failing index supplies the failure.
this.parallelMap: (array, worker, ...arguments) => ArrayParallel.parallelMap(
    (array, transform) => map(array, transform),
    array, worker, ...arguments
)

/// Answers a Future of a fresh Array of the elements for which
/// `predicate(element, arguments...)`, evaluated in isolated parallel
/// execution, answers `true`. Selection keeps source order. Outcomes are
/// examined in ascending index order, so the lowest index whose predicate
/// fails or answers something other than canonical `true` or `false`
/// (`InvalidPredicateResult`) supplies the failure.
this.parallelFilter: (array, predicate, ...arguments) => ArrayParallel.parallelFilter(
    (array, transform) => map(array, transform),
    (array, predicate) => filter(array, predicate),
    array, predicate, ...arguments
)

/// Answers a Future of the lowest index whose element satisfies
/// `predicate(element, arguments...)`, evaluated in isolated parallel
/// execution, or `null` when none does. Indexes are decided in ascending
/// logical order: a failure or non-Boolean result (`InvalidPredicateResult`)
/// at a lower index defeats a later match, and work at higher indexes is
/// cancelled once the outcome is decided.
this.parallelFindIndex: (array, predicate, ...arguments) => ArrayParallel.parallelFindIndex(
    (array, transform) => map(array, transform),
    (array, predicate) => findIndex(array, predicate),
    array, predicate, ...arguments
)

/// Answers a Future of the reduction of the elements with
/// `reducer(left, right, arguments...)`, or `null` for an empty Array. Each
/// round combines adjacent pairs (0,1), (2,3), ... left to right in isolated
/// parallel execution and carries an odd final value unchanged, so the
/// parenthesization is fixed and a non-associative reducer stays
/// deterministic. Within a round the leftmost failing pair supplies the
/// failure, and no later round starts after a failure.
this.parallelReduce: (array, reducer, ...arguments) => ArrayParallel.parallelReduce(
    (array, worker, ...arguments) => parallelMap(array, worker, ...arguments),
    array, reducer, ...arguments
)

/// Answers a Future of a fresh Array with the elements stably sorted by
/// `less(left, right, arguments...)`. The result, comparator invocation order,
/// and failures are exactly those of `sort` with the comparator
/// `(left, right) => less(left, right, arguments...)`, except that every
/// comparator invocation runs in its own isolated parallel execution. The
/// source Array is not mutated.
this.parallelSort: (array, less, ...arguments) => ArrayParallel.parallelSort(
    (array, less) => sort(array, less),
    array, less, ...arguments
)