Generator/Exponential: Difference between revisions

Content added Content deleted
Line 1,753: Line 1,753:
Lingo neither supports coroutines nor first-class functions, and also misses syntactic sugar for implementing real generators. But in the context of traditional for-loops, a peculiarity of Lingo can be used to implement generator-like behavior: in Lingo, the halting condition (b) in a "repeat with i = a to b" loop is not pre-cached, but evaluated in each single step. This behavior can be used - in combination with simple pseudo-generator objects that store states internally - to implement generator like behavior in loops, and solve the given task.
Lingo neither supports coroutines nor first-class functions, and also misses syntactic sugar for implementing real generators. But in the context of traditional for-loops, a peculiarity of Lingo can be used to implement generator-like behavior: in Lingo, the halting condition (b) in a "repeat with i = a to b" loop is not pre-cached, but evaluated in each single step. This behavior can be used - in combination with simple pseudo-generator objects that store states internally - to implement generator like behavior in loops, and solve the given task.


<lang Lingo>
<lang Lingo>squares = script("generator.power").new(2)
squares = script("generator.power").new(2)
cubes = script("generator.power").new(3)
cubes = script("generator.power").new(3)
filter = script("generator.filter").new(squares, cubes)
filter = script("generator.filter").new(squares, cubes)
Line 1,764: Line 1,763:
if i>30 then exit repeat
if i>30 then exit repeat
if i>20 then put res[1]
if i>20 then put res[1]
end repeat
end repeat</lang>
</lang>


{{out}}
{{out}}
Line 1,787: Line 1,785:


----------------------------------------
----------------------------------------
-- @constructor
--
----------------------------------------
----------------------------------------
on new (me, e)
on new (me, e)