UNPKG

@thi.ng/grid-iterators

Version:

2D grid and shape iterators w/ multiple orderings

19 lines (18 loc) 660 B
import { map } from "@thi.ng/transducers/map"; import { range2d } from "@thi.ng/transducers/range2d"; import { __opts } from "./utils.js"; const interleaveColumns2d = (opts) => __interleave(opts, [0, 1], [1, 0]); const interleaveRows2d = (opts) => __interleave(opts, [1, 0], [0, 1]); function* __interleave(opts, order, [x, y]) { const { cols, rows, tx } = __opts(opts); const step = (opts.step ?? 2) | 0; const [outer, inner] = order.map((x2) => [cols, rows][x2]); for (let j = 0; j < step; j++) { yield* map((p) => tx(p[x], p[y]), range2d(0, inner, j, outer, 1, step)); } } export { __interleave, interleaveColumns2d, interleaveRows2d };