← index

Generator

inherits Iterate · defined at core/02-iterate.qn:535

An iterable made from a yielding block: each ^> in the block becomes an element.

each: runs the block in a fiber and forwards every ^> to the consumer. Consumed lazily through an Iterator (e.g. .take:), so even infinite generators are usable.

var naturals = Generator.from:{ var i = 0; { true }.whileDo:{ ^>i; i = i + 1 } };
(naturals.lazyCollect:{ |x| x * x }).take:4    "* -> #(0 1 4 9)

Class methods

from:Block

A Generator over the block's ^> yields.

(Generator.from:{ ^>1; ^>2 }).list    "* -> #(1 2)

core/02-iterate.qn:542

Instance methods

each:

Call the consumer once per ^> yield, running the generator block in a fresh fiber - so every each: restarts the generator from the top.

core/02-iterate.qn:547