Collecting
A collector takes an iterable and returns a value, so it ends a pipeline.
Collectors that need configuration are called with it, like
reduce(fn, 0). The others go in a pipeline as is, like toArray.
toArray, toSet, toMap, and toObject
Section titled “toArray, toSet, toMap, and toObject”Collect the values into an array, a Set, a Map, or an object. toMap
and toObject take [key, value] pairs. toObject makes an object without
a prototype, so a key like __proto__ is just an ordinary property.
import * as Pact from "@hyperjump/pact";
Pact.toArray(new Set([1, 2])); // [1, 2]Pact.toSet([1, 1, 2]); // new Set([1, 2])
const pairs: [string, number][] = [["a", 1], ["b", 2]];Pact.toMap(pairs); // new Map([["a", 1], ["b", 2]])Pact.toObject(pairs); // { a: 1, b: 2 }reduce
Section titled “reduce”Calls a function with the accumulated value and each value in turn. It
returns the final accumulated value. Without an initial value, the first
value is used as the initial value. In that case an empty iterable throws a
TypeError, just like Array.prototype.reduce.
import * as Pact from "@hyperjump/pact";
const sum = (total: number, n: number) => total + n;
Pact.reduce(sum, 10)([1, 2, 3]); // 16Pact.reduce(sum)([1, 2, 3]); // 6every, some, and find
Section titled “every, some, and find”everychecks whether a function returnstruefor every value.somechecks whether it returnstruefor any value.findreturns the first value it returnstruefor, orundefined.
Each one stops reading and closes the iterable as soon as it knows the answer.
import * as Pact from "@hyperjump/pact";
Pact.every((n: number) => n > 0)([1, 2, 3]); // truePact.some((n: number) => n > 2)([1, 2, 3]); // truePact.find((n: number) => n > 1)([1, 2, 3]); // 2forEach
Section titled “forEach”Calls a function with each value and returns undefined. Use it for side
effects at the end of a pipeline. To look at values in the middle of a
pipeline, use tap.
import * as Pact from "@hyperjump/pact";
Pact.pipe( ["a.json", "b.txt"], Pact.filter((file) => file.endsWith(".json")), Pact.forEach((file) => console.log(file)));a.jsonhead and count
Section titled “head and count”head returns the first value and then closes the iterator. If there are
no values, it returns undefined. count returns how many values there
are.
import * as Pact from "@hyperjump/pact";
Pact.head(Pact.range(5)); // 5Pact.count(Pact.range(0, 5)); // 5Joins the values into a string with a separator between them.
import * as Pact from "@hyperjump/pact";
Pact.pipe( ["a", "b"], Pact.map((s) => s.toUpperCase()), Pact.join(", ")); // "A, B"