Generator/Exponential: Difference between revisions
J
(Added PicoLisp) |
(J) |
||
Line 23:
'''Sample output'''
<pre>[529,576,625,676,784,841,900,961,1024,1089]</pre>
=={{header|J}}==
The most natural way to accomplish this task in J is by not using a generator:
<lang j> 10{.20}.(^&2 -. ^&3) i.100
529 576 625 676 784 841 900 961 1024 1089</lang>
This is considerably simpler, and faster, and arguably easier to reason about, than the generator implementation.
That said, here is an implementation of a generator:
<lang j>coclass 'generator'
n=:0
next=:3 :0
while.-. ((-: <.&.(^&1r2)) *. -.@(-: <.&.(^&1r3)))n do.
n=: n+1
end.
_1 + n=:n+1
)</lang>
And, here is an example of using that generator to accomplish this task:
<lang j> 20 }.next__g"0 i.30 [ g=: conew'generator'
529 576 625 676 784 841 900 961 1024 1089</lang>
=={{header|PicoLisp}}==
|