@extra-iterable/many

Converts a once iterable to many. :package: :smiley_cat: :running: :vhs: :moon: :scroll: :newspaper: :blue_book:

Usage no npm install needed!

<script type="module">
  import extraIterableMany from 'https://cdn.skypack.dev/@extra-iterable/many';
</script>

README

Converts a once iterable to many. :package: :smiley_cat: :running: :vhs: :moon: :scroll: :newspaper: :blue_book:

Similar: isOnce, isMany, many.

This is part of package extra-iterable.


iterable.many(x, [now]);
// x:   an iterable
// now: consume immediately? (false)
const iterable = require("extra-iterable");

var x = [1, 2, 3].values();
[...x, ...x];
// [ 1, 2, 3 ]
// ("x" cant be iterated multiple times)

function* from1ToN(n) {
  for(var i=1; i<=n; i++) {
    console.log("yielding", i);
    yield i;
  }
}
var x = from1ToN(3);
var y = iterable.many(x);
for(var v of y) console.log("read", v);
// yielding 1
// read 1
// yielding 2
// read 2
// yielding 3
// read 3
for(var v of y) console.log("read", v);
// read 1
// read 2
// read 3

var x = from1ToN(3);
var y = iterable.many(x, true);
// yielding 1
// yielding 2
// yielding 3
for(var v of y) console.log("read", v);
// read 1
// read 2
// read 3
for(var v of y) console.log("read", v);
// read 1
// read 2
// read 3