@thi.ng/iterators
Advanced tools
Comparing version 6.1.102 to 6.1.103
# Change Log | ||
- **Last updated**: 2024-03-01T15:22:50Z | ||
- **Last updated**: 2024-03-02T14:05:53Z | ||
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub) | ||
@@ -5,0 +5,0 @@ |
{ | ||
"name": "@thi.ng/iterators", | ||
"version": "6.1.102", | ||
"version": "6.1.103", | ||
"description": "Clojure inspired, composable ES6 iterators & generators", | ||
@@ -39,3 +39,3 @@ "type": "module", | ||
"@thi.ng/api": "^8.9.27", | ||
"@thi.ng/dcons": "^3.2.97", | ||
"@thi.ng/dcons": "^3.2.98", | ||
"@thi.ng/errors": "^2.4.19" | ||
@@ -233,3 +233,3 @@ }, | ||
}, | ||
"gitHead": "d660ae8fd1bf64d919b4334f19509f1f539d70f6\n" | ||
"gitHead": "df9e312af741d87e6b450afcfea6a6e381662b1e\n" | ||
} |
344
README.md
@@ -137,13 +137,15 @@ <!-- This file is generated - DO NOT EDIT! --> | ||
```ts | ||
[...ti.butLast([])] | ||
import { butLast, range } from "@thi.ng/iterators"; | ||
[...butLast([])] | ||
// [] | ||
[...ti.butLast([1])] | ||
[...butLast([1])] | ||
// [] | ||
[...ti.butLast([1,2])] | ||
[...butLast([1,2])] | ||
// [ 1 ] | ||
[...ti.butLast([1,2,3])] | ||
[...butLast([1,2,3])] | ||
// [ 1, 2 ] | ||
[...ti.butLast("hello")] | ||
[...butLast("hello")] | ||
// [ "h", "e", "l", "l" ] | ||
[...ti.butLast(ti.range(10))] | ||
[...butLast(range(10))] | ||
// [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ] | ||
@@ -164,5 +166,7 @@ ``` | ||
```ts | ||
c = ti.cached(ti.range(10)); | ||
a = c(); | ||
b = c(); | ||
import { cached, range } from "@thi.ng/iterators"; | ||
const c = cached(range(10)); | ||
const a = c(); | ||
const b = c(); | ||
a.next(); | ||
@@ -177,11 +181,13 @@ // { done: false, value: 0 } -> from original | ||
angles = ti.cached(ti.range(0, 360, 45)); | ||
const angles = cached(range(0, 360, 45)); | ||
[...angles()] | ||
// [ 0, 45, 90, 135, 180, 225, 270, 315 ] | ||
ti.zip( | ||
import { juxt, map, zip } from "@thi.ng/iterators"; | ||
zip( | ||
angles(), | ||
ti.map( | ||
ti.juxt(Math.sin, Math.cos), | ||
ti.map( | ||
map( | ||
juxt(Math.sin, Math.cos), | ||
map( | ||
(x)=> x * 180 / Math.PI, | ||
@@ -211,6 +217,8 @@ angles() | ||
```ts | ||
[... ti.concat([1, 2, 3], [10, 20, 30], [100, 200, 300])] | ||
import { concat } from "@thi.ng/iterators"; | ||
[...concat([1, 2, 3], [10, 20, 30], [100, 200, 300])] | ||
// [ 1, 2, 3, 10, 20, 30, 100, 200, 300 ] | ||
[...ti.concat.apply(null, ["abc", null, [1, 2, 3]])] | ||
[...concat.apply(null, ["abc", null, [1, 2, 3]])] | ||
// [ "a", "b", "c", 1, 2, 3 ] | ||
@@ -227,4 +235,8 @@ ``` | ||
```ts | ||
import { | ||
constantly, filter, map, reduce, repeatedly, takeWhile | ||
} from "@thi.ng/iterators"; | ||
// define an iterable of unknown size | ||
iter = ti.takeWhile(x => x < 0.98, ti.repeatedly(()=> Math.random())); | ||
iter = takeWhile(x => x < 0.98, repeatedly(()=> Math.random())); | ||
@@ -234,7 +246,7 @@ // use map & reduce to determine size of iterable: | ||
// then reduce is used to sum | ||
ti.reduce((a, b)=> a + b, 0, ti.map(ti.constantly(1), iter)) | ||
reduce((a, b)=> a + b, 0, map(constantly(1), iter)) | ||
// 241 (varying) | ||
// some complex data transformation | ||
times10 = (flt, coll) => [...ti.map(x => x * 10, ti.filter(flt, coll))]; | ||
times10 = (flt, coll) => [...map(x => x * 10, filter(flt, coll))]; | ||
@@ -245,3 +257,3 @@ // some user selectable config | ||
even: x => (x % 2) == 0, | ||
all: ti.constantly(true) | ||
all: constantly(true) | ||
}; | ||
@@ -272,3 +284,5 @@ | ||
```ts | ||
[... ti.take(10, ti.cycle(ti.range(3)))] | ||
import { cycle, range, take } from "@thi.ng/iterators"; | ||
[...take(10, cycle(range(3)))] | ||
// [0, 1, 2, 0, 1, 2, 0, 1, 2, 0] | ||
@@ -285,3 +299,5 @@ ``` | ||
```ts | ||
[... ti.dedupe([1, 2, 2, 3, 4, 4, 4, 3])] | ||
import { dedupe } from "@thi.ng/iterators"; | ||
[...dedupe([1, 2, 2, 3, 4, 4, 4, 3])] | ||
// [1, 2, 3, 4, 3] | ||
@@ -298,6 +314,8 @@ ``` | ||
```ts | ||
import { dedupeWith } from "@thi.ng/iterators"; | ||
var coll = [{ a: 1 }, { a: 1, b: 2 }, { a: 2, b: 2 }, { a: 2, b: 2 }, { a: 3 }]; | ||
var eq = (a, b) => a.a === b.a; | ||
[...ti.dedupeWith(eq, coll)] | ||
[...dedupeWith(eq, coll)] | ||
// [ { a: 1 }, { a: 2, b: 2 }, { a: 3 } ] | ||
@@ -314,2 +332,4 @@ ``` | ||
```ts | ||
import { dense } from "@thi.ng/iterators"; | ||
var a = [] | ||
@@ -319,3 +339,3 @@ a[10] = 1; | ||
[...ti.dense(a)] | ||
[...dense(a)] | ||
// [1, 2] | ||
@@ -332,7 +352,11 @@ ``` | ||
```ts | ||
[... ti.drop(5, ti.range(10))] | ||
import { drop, range, take } from "@thi.ng/iterators"; | ||
[...drop(5, range(10))] | ||
// [5, 6, 7, 8, 9] | ||
[... ti.drop(5, ti.range(3))] | ||
[...drop(5, range(3))] | ||
// [] | ||
[... ti.take(5, ti.drop(5, ti.range()))] | ||
[...take(5, drop(5, range()))] | ||
// [ 5, 6, 7, 8, 9 ] | ||
@@ -348,5 +372,7 @@ ``` | ||
```ts | ||
[... ti.dropNth(2, ti.range(10))] | ||
import { dropNth, range } from "@thi.ng/iterators"; | ||
[...dropNth(2, range(10))] | ||
// [0, 2, 4, 6, 8] | ||
[... ti.dropNth(3, ti.range(10))] | ||
[...dropNth(3, range(10))] | ||
// [ 0, 1, 3, 4, 6, 7, 9 ] | ||
@@ -364,3 +390,5 @@ ``` | ||
```ts | ||
[... ti.dropWhile((x) => x < 5, ti.range(10))] | ||
import { dropWhile, range } from "@thi.ng/iterators"; | ||
[...dropWhile((x) => x < 5, range(10))] | ||
// [5, 6, 7, 8, 9] | ||
@@ -381,5 +409,7 @@ ``` | ||
```ts | ||
var nums = ti.iterator([2, 4, 6, 8, 10]); | ||
import { every, iterator } from "@thi.ng/iterators"; | ||
ti.every((x) => (x % 2) === 0, nums); | ||
var nums = iterator([2, 4, 6, 8, 10]); | ||
every((x) => (x % 2) === 0, nums); | ||
// true, all passed | ||
@@ -389,4 +419,4 @@ nums.next() | ||
nums = ti.iterator([2, 3, 4]); | ||
ti.every((x) => (x % 2) === 0, nums); | ||
nums = iterator([2, 3, 4]); | ||
every((x) => (x % 2) === 0, nums); | ||
// false, `every` stopped at `3` | ||
@@ -396,3 +426,3 @@ nums.next() | ||
ti.every((x) => true, []) | ||
every((x) => true, []) | ||
// false (empty input) | ||
@@ -409,4 +439,7 @@ ``` | ||
```ts | ||
var multOf3 = (x) => (x % 3) === 0; | ||
[... ti.filter(multOf3, ti.range(10))]; | ||
import { filter, range } from "@thi.ng/iterators"; | ||
const multOf3 = (x: number) => (x % 3) === 0; | ||
[...filter(multOf3, range(10))]; | ||
// [ 0, 3, 6, 9 ] | ||
@@ -425,10 +458,12 @@ ``` | ||
```ts | ||
[... ti.flatten([1, [2, 3], [4, [5, ["abc"]]]])] | ||
import { flatten } from "@thi.ng/iterators"; | ||
[...flatten([1, [2, 3], [4, [5, ["abc"]]]])] | ||
// [ 1, 2, 3, 4, 5, "abc" ] | ||
[...ti.flatten([{ a: 1 }, { a: 23, b: 42, c: [1, 2, 3] }])] | ||
[...flatten([{ a: 1 }, { a: 23, b: 42, c: [1, 2, 3] }])] | ||
// ["a", 1, "a", 23, "b", 42, "c", 1, 2, 3] | ||
// don't flatten objects | ||
[...ti.flatten([{ a: 1 }, [1, 2, 3], { a: 23, b: 42, c: [1, 2, 3] }], false)] | ||
[...flatten([{ a: 1 }, [1, 2, 3], { a: 23, b: 42, c: [1, 2, 3] }], false)] | ||
// [ { a: 1 }, 1, 2, 3, { a: 23, b: 42, c: [ 1, 2, 3 ] } ] | ||
@@ -451,3 +486,5 @@ ``` | ||
```ts | ||
let defaultTx = x => | ||
import { flattenWith, map, maybeIterator, maybeObjectIterator } from "@thi.ng/iterators"; | ||
const defaultTx = x => | ||
(typeof x !== "string" && (maybeIterator(x) || maybeObjectIterator(x))) || | ||
@@ -458,5 +495,5 @@ undefined; | ||
// if `x` is a string, return its numeric charcode sequence for flattening | ||
let tx = x => typeof x == "string" ? ti.map(x => x.charCodeAt(0), x) : ti.maybeIterator(x); | ||
const tx = x => typeof x == "string" ? map(x => x.charCodeAt(0), x) : maybeIterator(x); | ||
[...ti.flattenWith(tx, ["ROOT", undefined, ["CHILD_1", null, ["CHILD_2"]]])] | ||
[...flattenWith(tx, ["ROOT", undefined, ["CHILD_1", null, ["CHILD_2"]]])] | ||
// [ 82, 79, 79, 84, undefined, 67, 72, 73, 76, 68, 95, 49, null, 67, 72, 73, 76, 68, 95, 50 ] | ||
@@ -479,6 +516,8 @@ ``` | ||
```ts | ||
import { fnil, reduce } from "@thi.ng/iterators"; | ||
hello = (greet, name) => `${greet}, ${name}!`; | ||
helloEN = ti.fnil(hello, () => "Hi", () => "user"); | ||
helloDE = ti.fnil(hello, () => "Hallo", () => `Teilnehmer #${(Math.random()*100)|0}`); | ||
helloEN = fnil(hello, () => "Hi", () => "user"); | ||
helloDE = fnil(hello, () => "Hallo", () => `Teilnehmer #${(Math.random()*100)|0}`); | ||
@@ -493,3 +532,3 @@ helloEN(); // "Hi, user!" | ||
// build new fn which calls ctor to supply 0 if arg is null or undefined | ||
adder = ti.fnil(inc, () => 0); | ||
adder = fnil(inc, () => 0); | ||
@@ -508,3 +547,3 @@ adder(); | ||
// fnil is used here to avoid `NaN` each time an yet unknown letter is encountered | ||
ti.reduce(updateWith(adder), {}, "abracadabra"); | ||
reduce(updateWith(adder), {}, "abracadabra"); | ||
// { a: 5, b: 2, r: 2, c: 1, d: 1 } | ||
@@ -534,7 +573,9 @@ ``` | ||
```ts | ||
import { fork, map, partition, reduce, repeatedly } from "@thi.ng/iterators"; | ||
// stream of random numbers, as sliding partitions of 5 values | ||
src = ti.partition(5, 1, ti.repeatedly(()=> (Math.random() * 100) | 0, 10)); | ||
src = partition(5, 1, repeatedly(()=> (Math.random() * 100) | 0, 10)); | ||
// setup forking, only caching single value | ||
f = ti.fork(src, 1); | ||
f = fork(src, 1); | ||
@@ -544,7 +585,7 @@ // create 4 forks (by calling f()), each with their own transformer: | ||
// simple moving average | ||
sma = ti.map((part)=> ti.reduce((a, b) => a + b, 0, part) / part.length, f()); | ||
sma = map((part)=> reduce((a, b) => a + b, 0, part) / part.length, f()); | ||
// minimum | ||
min = ti.map((part)=> ti.reduce((a, b) => Math.min(a, b), 100, part), f()); | ||
min = map((part)=> reduce((a, b) => Math.min(a, b), 100, part), f()); | ||
// maximum | ||
max = ti.map((part)=> ti.reduce((a, b) => Math.max(a, b), -1, part), f()); | ||
max = map((part)=> reduce((a, b) => Math.max(a, b), -1, part), f()); | ||
@@ -575,4 +616,6 @@ // consume the forks in synchronized manner | ||
```ts | ||
import { frequencies, filter } from "@thi.ng/iterators"; | ||
// without key fn | ||
[... ti.frequencies([[1,2], [2,3], [1,2], [2,4]])] | ||
[...frequencies([[1,2], [2,3], [1,2], [2,4]])] | ||
// [ [[1, 2], 2], | ||
@@ -583,3 +626,3 @@ // [[2, 3], 1], | ||
// with key fn | ||
[... ti.frequencies([1, 2, 3, 4, 5, 9, 3], (x) => x & ~1)] | ||
[...frequencies([1, 2, 3, 4, 5, 9, 3], (x) => x & ~1)] | ||
// [ [ 0, 1 ], [ 2, 3 ], [ 4, 2 ], [ 8, 1 ] ] | ||
@@ -589,3 +632,3 @@ | ||
var isLetter = (x) => /[a-z]/i.test(x); | ||
[... ti.frequencies(ti.filter(isLetter, "hello world!"))].sort((a, b) => b[1] - a[1]) | ||
[...frequencies(filter(isLetter, "hello world!"))].sort((a, b) => b[1] - a[1]) | ||
// [ ["l", 3], ["o", 2], ["h", 1], ["e", 1], ["w", 1], ["r", 1], ["d", 1] ] | ||
@@ -603,2 +646,4 @@ ``` | ||
```ts | ||
import { groupBy } from "@thi.ng/iterators"; | ||
// group into multiples of 2 | ||
@@ -623,14 +668,16 @@ groupBy((x) => x & ~1, [1, 2, 3, 4, 5, 9, 3]) | ||
```ts | ||
ti.identity() // undefined | ||
ti.identity(null) // null | ||
ti.identity(42) // 42 | ||
import { identity, every, fnil } from "@thi.ng/iterators"; | ||
identity() // undefined | ||
identity(null) // null | ||
identity(42) // 42 | ||
tests = [true, true, undefined, true] | ||
// all tests succeeded? | ||
ti.every(ti.identity, tests); | ||
every(identity, tests); | ||
// false | ||
// mark missing test results as success | ||
ti.every(ti.fnil(ti.identity, () => true), tests); | ||
every(fnil(identity, () => true), tests); | ||
// true | ||
@@ -646,3 +693,4 @@ ``` | ||
```ts | ||
[...ti.indexed("hello")] | ||
import { indexed } from "@thi.ng/iterators"; | ||
[...indexed("hello")] | ||
// [ [ 0, "h" ], [ 1, "e" ], [ 2, "l" ], [ 3, "l" ], [ 4, "o" ] ] | ||
@@ -660,3 +708,5 @@ ``` | ||
```ts | ||
[... ti.interleave(ti.range(), ti.range(100, 200), ti.range(200, 205))] | ||
import { interleave, range } from "@thi.ng/iterators"; | ||
[...interleave(range(), range(100, 200), range(200, 205))] | ||
// [ 0, 100, 200, 1, 101, 201, 2, 102, 202, 3, 103, 203, 4, 104, 204 ] | ||
@@ -673,3 +723,5 @@ ``` | ||
```ts | ||
[... ti.interpose("/", ti.range(5))] | ||
import { interpose, range } from "@thi.ng/iterators"; | ||
[...interpose("/", range(5))] | ||
// [ 0, "/", 1, "/", 2, "/", 3, "/", 4 ] | ||
@@ -688,3 +740,5 @@ ``` | ||
```ts | ||
[... ti.take(10, ti.iterate((x) => x * 2, 1))] | ||
import { iterate, take } from "@thi.ng/iterators"; | ||
[...take(10, iterate((x) => x * 2, 1))] | ||
// [ 1, 2, 4, 8, 16, 32, 64, 128, 256, 512 ] | ||
@@ -710,3 +764,5 @@ ``` | ||
```ts | ||
var kernel = ti.juxt( | ||
import { juxt, map, range } from "@thi.ng/iterators"; | ||
var kernel = juxt( | ||
(x) => x - 1, | ||
@@ -719,3 +775,3 @@ (x) => x, | ||
[... ti.map(kernel, ti.range(3))] | ||
[...map(kernel, range(3))] | ||
// [ [-1, 0, 1], [0, 1, 2], [1, 2, 3] ] | ||
@@ -733,7 +789,9 @@ ``` | ||
```ts | ||
ti.last(ti.range(10)) | ||
import { last, range, take } from "@thi.ng/iterators"; | ||
last(range(10)) | ||
// 9 | ||
// last item of truncated infinite input | ||
ti.last(ti.take(10, ti.range())) | ||
last(take(10, range())) | ||
// 9 | ||
@@ -753,6 +811,8 @@ ``` | ||
```ts | ||
[... ti.map((x)=> x*10, ti.range(10))] | ||
import { map, range } from "@thi.ng/iterators"; | ||
[...map((x)=> x*10, range(10))] | ||
// [0, 10, 20, 30, 40, 50, 60, 70, 80, 90] | ||
[... ti.map((x, y, z) => [x, y, z], ti.range(5), ti.range(0, 100, 10), ti.range(0, 1000, 100))] | ||
[...map((x, y, z) => [x, y, z], range(5), range(0, 100, 10), range(0, 1000, 100))] | ||
// [ [0, 0, 0], [1, 10, 100], [2, 20, 200], [3, 30, 300], [4, 40, 400] ] | ||
@@ -771,6 +831,8 @@ ``` | ||
```ts | ||
[... ti.mapcat((x) => ti.repeat(x, 3), "hello")] | ||
import { mapcat, range, repeat } from "@thi.ng/iterators"; | ||
[...mapcat((x) => repeat(x, 3), "hello")] | ||
// [ "h", "h", "h", "e", "e", "e", "l", "l", "l", "l", "l", "l", "o", "o", "o" ] | ||
[...ti.mapcat((x) => x < 5 ? ti.repeat(x,x) : null, ti.range(10))] | ||
[...mapcat((x) => x < 5 ? repeat(x,x) : null, range(10))] | ||
// [ 1, 2, 2, 3, 3, 3, 4, 4, 4, 4 ] | ||
@@ -787,3 +849,5 @@ ``` | ||
```ts | ||
[... ti.mapIndexed((i, a, b) => [i, a, b], "hello", "there")] | ||
import { mapIndexed } from "@thi.ng/iterators"; | ||
[...mapIndexed((i, a, b) => [i, a, b], "hello", "there")] | ||
// [ [0, "h", "t"], | ||
@@ -817,3 +881,5 @@ // [1, "e", "h"], | ||
```ts | ||
[... ti.objectIterator({a: 23, b: 42, c: [1, 2, 3]})] | ||
import { objectIterator } from "@thi.ng/iterators"; | ||
[...objectIterator({a: 23, b: 42, c: [1, 2, 3]})] | ||
// [ ["a", 23], ["b", 42], ["c", [1, 2, 3]] ] | ||
@@ -833,13 +899,15 @@ ``` | ||
```ts | ||
[... ti.partition(3, 3, ti.range(10))] | ||
import { partition, range } from "@thi.ng/iterators"; | ||
[...partition(3, 3, range(10))] | ||
// [ [0, 1, 2], [3, 4, 5], [6, 7, 8] ] | ||
[... ti.partition(3, 3, ti.range(10), true)] | ||
[...partition(3, 3, range(10), true)] | ||
// [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [9] ] | ||
[... ti.partition(3, 1, ti.range(10))] | ||
[...partition(3, 1, range(10))] | ||
// [ [0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5], | ||
// [4, 5, 6], [5, 6, 7], [6, 7, 8], [7, 8, 9] ] | ||
[... ti.partition(3, 5, ti.range(10))] | ||
[...partition(3, 5, range(10))] | ||
// [ [0, 1, 2], [5, 6, 7] ] | ||
@@ -856,3 +924,5 @@ ``` | ||
```ts | ||
[... ti.partitionBy((x) => x / 5 | 0, ti.range(11))] | ||
import { partitionBy, range } from "@thi.ng/iterators"; | ||
[...partitionBy((x) => x / 5 | 0, range(11))] | ||
// [ [0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10] ] | ||
@@ -869,3 +939,5 @@ ``` | ||
```ts | ||
[... ti.randomSample(0.1, ti.range(100))] | ||
import { randomSample, range } from "@thi.ng/iterators"; | ||
[...randomSample(0.1, range(100))] | ||
// [ 10, 13, 16, 21, 22, 24, 32, 35, 37, 81, 93 ] | ||
@@ -882,3 +954,3 @@ ``` | ||
- If called without arguments, produces values from 0 .. +∞. | ||
- If called with 1 arg: 0 ... n (exclusive) | ||
- If called with 1 arg: 0 ...n (exclusive) | ||
- If called with 2 arg: `from` ... `to` (exclusive) | ||
@@ -889,18 +961,20 @@ | ||
```ts | ||
[... ti.take(5, ti.range())] | ||
import { take, range } from "@thi.ng/iterators"; | ||
[...take(5, range())] | ||
// [0, 1, 2, 3, 4] | ||
[... ti.range(5)] | ||
[...range(5)] | ||
// [0, 1, 2, 3, 4] | ||
[... ti.range(100, 105)] | ||
[...range(100, 105)] | ||
// [100, 101, 102, 103, 104] | ||
[... ti.range(5,0)] | ||
[...range(5,0)] | ||
// [5, 4, 3, 2, 1] | ||
[... ti.range(0, 50, 10)] | ||
[...range(0, 50, 10)] | ||
// [0, 10, 20, 30, 40] | ||
[... ti.range(50, -1, -10)] | ||
[...range(50, -1, -10)] | ||
// [50, 40, 30, 20, 10, 0] | ||
@@ -921,7 +995,16 @@ ``` | ||
```ts | ||
ti.reduce((acc, x) => acc + x, 0, ti.range(10)) | ||
import { reduce, reduced, range } from "@thi.ng/iterators"; | ||
reduce((acc, x) => acc + x, 0, range(10)) | ||
// 45 | ||
// infinite input with early termination | ||
ti.reduce((acc, x) => { return acc += x, acc >= 15 ? ti.reduced(acc) : acc }, 0, ti.range()) | ||
reduce( | ||
(acc, x) => { | ||
acc += x; | ||
return acc >= 15 ? reduced(acc) : acc; | ||
}, | ||
0, | ||
range() | ||
) | ||
// 15 | ||
@@ -943,7 +1026,16 @@ ``` | ||
```ts | ||
[... ti.reductions((acc, x) => acc + x, 0, ti.range(10))] | ||
import { reductions, reduced, range } from "@thi.ng/iterators"; | ||
[...reductions((acc, x) => acc + x, 0, range(10))] | ||
// [ 0, 1, 3, 6, 10, 15, 21, 28, 36, 45 ] | ||
// with early termination | ||
[... ti.reductions((acc, x) => { return acc += x, acc >= 15 ? ti.reduced(acc) : acc }, 0, ti.range())] | ||
[...reductions( | ||
(acc, x) => { | ||
acc += x; | ||
return acc >= 15 ? reduced(acc) : acc | ||
}, | ||
0, | ||
range() | ||
)] | ||
// [ 0, 1, 3, 6, 10, 15 ] | ||
@@ -967,6 +1059,7 @@ ``` | ||
```ts | ||
[... ti.take(5, ti.repeat(42))] | ||
import { repeat, take } from "@thi.ng/iterators"; | ||
[...take(5, repeat(42))] | ||
// [42, 42, 42, 42, 42] | ||
[... ti.repeat(42, 5)] | ||
[...repeat(42, 5)] | ||
// [42, 42, 42, 42, 42] | ||
@@ -983,6 +1076,8 @@ ``` | ||
```ts | ||
[... ti.take(3, ti.repeatedly(() => Math.random()))] | ||
import { repeatedly, take } from "@thi.ng/iterators"; | ||
[...take(3, repeatedly(() => Math.random()))] | ||
// [ 0.9620186971807614, 0.8191901643942394, 0.5964328949163533 ] | ||
[... ti.repeatedly(() => Math.random(), 3)] | ||
[...repeatedly(() => Math.random(), 3)] | ||
// [ 0.46381477224416057, 0.22568030685532992, 0.5494769470662977 ] | ||
@@ -1000,9 +1095,11 @@ ``` | ||
```ts | ||
[...ti.reverse([1, 2, 3])] | ||
import { iterate, reverse, take } from "@thi.ng/iterators"; | ||
[...reverse([1, 2, 3])] | ||
// [3, 2, 1] | ||
[...ti.reverse("hello")] | ||
[...reverse("hello")] | ||
// [ "o", "l", "l", "e", "h" ] | ||
[...ti.reverse(ti.take(10, ti.iterate(x => x * 2, 1)))] | ||
[...reverse(take(10, iterate(x => x * 2, 1)))] | ||
// [ 512, 256, 128, 64, 32, 16, 8, 4, 2, 1 ] | ||
@@ -1020,5 +1117,7 @@ ``` | ||
```ts | ||
var nums = ti.iterator([1, 2, 3]); | ||
import { iterator, some } from "@thi.ng/iterators"; | ||
ti.some((x) => (x % 2) === 0, nums); | ||
var nums = iterator([1, 2, 3]); | ||
some((x) => (x % 2) === 0, nums); | ||
// 2, the 1st value which passed | ||
@@ -1028,4 +1127,4 @@ nums.next() | ||
nums = ti.iterator([1, 2, 3]); | ||
ti.some((x) => x > 3, nums); | ||
nums = iterator([1, 2, 3]); | ||
some((x) => x > 3, nums); | ||
// undefined | ||
@@ -1044,3 +1143,5 @@ nums.next() | ||
```ts | ||
[... ti.take(3, ti.range())] | ||
import { range, take } from "@thi.ng/iterators"; | ||
[...take(3, range())] | ||
// [ 0, 1, 2 ] | ||
@@ -1056,3 +1157,5 @@ ``` | ||
```ts | ||
[... ti.takeNth(3, ti.range(10))] | ||
import { range, takeNth } from "@thi.ng/iterators"; | ||
[...takeNth(3, range(10))] | ||
// [ 0, 3, 6, 9 ] | ||
@@ -1073,6 +1176,9 @@ ``` | ||
```ts | ||
var input = ti.range(10); | ||
[... ti.takeWhile((x)=> x < 5, input)] | ||
import { range, takeWhile } from "@thi.ng/iterators"; | ||
var input = range(10); | ||
[...takeWhile((x)=> x < 5, input)] | ||
// [ 0, 1, 2, 3, 4 ] | ||
[... input] | ||
[...input] | ||
// note: `5` is missing (the value which failed takeWhile) | ||
@@ -1092,6 +1198,8 @@ // [ 6, 7, 8, 9 ] | ||
```ts | ||
[... ti.takeLast(5, ti.range(1000))] | ||
import { range, takeLast } from "@thi.ng/iterators"; | ||
[...takeLast(5, range(1000))] | ||
// [ 995, 996, 997, 998, 999 ] | ||
[... ti.takeLast(5, ti.range(3))] | ||
[...takeLast(5, range(3))] | ||
// [ 0, 1, 2 ] | ||
@@ -1120,2 +1228,4 @@ ``` | ||
```ts | ||
import { walk } from "@thi.ng/iterators"; | ||
// dummy SVG document | ||
@@ -1148,3 +1258,3 @@ let doc = { | ||
// transform doc | ||
ti.walk(circleTX, (x) => x.content, doc); | ||
walk(circleTX, (x) => x.content, doc); | ||
@@ -1177,4 +1287,6 @@ doc.content[0].content[1] | ||
```ts | ||
import { map, walkIterator } from "@thi.ng/iterators"; | ||
// pre-order traversal | ||
[...ti.map(JSON.stringify, ti.walkIterator([[[1, [2]], [3, [4]]], [5]], false))] | ||
[...map(JSON.stringify, walkIterator([[[1, [2]], [3, [4]]], [5]], false))] | ||
// [ "[[[1,[2]],[3,[4]]],[5]]", | ||
@@ -1194,3 +1306,3 @@ // "[[1,[2]],[3,[4]]]", | ||
// post-order traversal | ||
[...ti.map(JSON.stringify, ti.walkIterator([[[1, [2]], [3, [4]]], [5]], true))] | ||
[...map(JSON.stringify, walkIterator([[[1, [2]], [3, [4]]], [5]], true))] | ||
// [ "1", | ||
@@ -1220,6 +1332,8 @@ // "2", | ||
```ts | ||
ti.zip("abcdef", ti.range()) | ||
import { map, range, zip } from "@thi.ng/iterators"; | ||
zip("abcdef", range()) | ||
// { a: 0, b: 1, c: 2, d: 3, e: 4, f: 5 } | ||
ti.zip(ti.range(5,10), ti.range(100,200), new Uint8Array(16)) | ||
zip(range(5,10), range(100,200), new Uint8Array(16)) | ||
// [ 0, 0, 0, 0, 0, 100, 101, 102, 103, 104, 0, 0, 0, 0, 0, 0 ] | ||
@@ -1233,3 +1347,3 @@ | ||
ti.zip(ti.map((x)=> x.id, langs), langs) | ||
zip(map((x)=> x.id, langs), langs) | ||
// { js: { id: "js", name: "JavaScript" }, | ||
@@ -1236,0 +1350,0 @@ // clj: { id: "clj", name: "Clojure" }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
88532
1307
Updated@thi.ng/dcons@^3.2.98