J
Johannes Ahl-mann
i am still in my early stages of ruby, so please forgive me any
blatantly lacking insight ;-))
i wonder how to divide responsibilities of block-driven methods in ruby.
in python i can divide the responsibilities of creating data and
filtering it using generators and the functional itertools. although
this is a lot of lately tacked on functionality i am struggling to
implement something similar in ruby.
--- python code (untested code, but should run ;-) ---
def fib:
a, b = 1, 1
while true:
yield a
a, b = b, a+b
import itertools
def fibUpTo(n):
return itertools.takeWhile(lambda i: i < n, fib())
--- a unified solution in ruby: ---
def fibUpTo n, &block
a, b = 1, 1
while a < n
block.call(a)
a, b = b, a+b
end
end
--- snip ---
i would really like to separate the creation of the fibonacci series
from the filtering. this should be possible in ruby, i just can't see it
right now ;-))
is maybe building a generator with the generator module an option?a
thx,
Johannes
blatantly lacking insight ;-))
i wonder how to divide responsibilities of block-driven methods in ruby.
in python i can divide the responsibilities of creating data and
filtering it using generators and the functional itertools. although
this is a lot of lately tacked on functionality i am struggling to
implement something similar in ruby.
--- python code (untested code, but should run ;-) ---
def fib:
a, b = 1, 1
while true:
yield a
a, b = b, a+b
import itertools
def fibUpTo(n):
return itertools.takeWhile(lambda i: i < n, fib())
--- a unified solution in ruby: ---
def fibUpTo n, &block
a, b = 1, 1
while a < n
block.call(a)
a, b = b, a+b
end
end
--- snip ---
i would really like to separate the creation of the fibonacci series
from the filtering. this should be possible in ruby, i just can't see it
right now ;-))
is maybe building a generator with the generator module an option?a
thx,
Johannes