There is a complete lazy list implementation from Reginald Braithwaite
website:
http://raganwald.com/source/lazy_lists.html
It supports a lot of operations on this kind of “infinite” structures
such as merge and cartesian product.
That one could make it to the core!
Eric.
Eric TORREBORRE
tel: +81 (0)90 5580 3280
e-mail: [email protected] / [email protected]
blog: http://etorreborre.blogspot.com
----- Message d’origine ----
De : Robert D. [email protected]
À : ruby-talk ML [email protected]
Envoyé le : Jeudi, 7 Juin 2007, 20h29mn 46s
Objet : Re: What about a ‘series’ type?
On 6/7/07, Peter M. [email protected] wrote:
Honestly I do not believe that the core is the place to put such
things, furthermore I believe it is too specific a feature, a more
general approach might have better chances to be fit for the core; Yet
I do not think what follows is fit for the core either, but maybe you
find it interesting or helpful:
class Lazy
def initialize init, op, *args
@init = init
@op = op
@args = args.dup
end
def upto value
return [] if value < 1
(2…value).inject([@init]){ |acc,| acc << acc.last.send( @op, *@args
) }
end
end # class Lazy
505/6 > irb -r lazy.rb
irb(main):001:0> l = Lazy.new 1, :+, 2
=> #<Lazy:0xb7ddfa60 @args=[2], @init=1, @op=:+>
irb(main):002:0> l.upto 5
=> [1, 3, 5, 7, 9]
irb(main):003:0> m = Lazy.new 2, :, 3
=> #<Lazy:0xb7dd4908 @args=[3], @init=2, @op=:>
irb(main):004:0> m.upto 4
=> [2, 6, 18, 54]
irb(main):005:0>
Cheers
Robert
P.S. Implementations without #inject are theoretically possible
R.
You see things; and you say Why?
But I dream things that never were; and I say Why not?
– George Bernard Shaw
Découvrez une nouvelle façon d’obtenir des réponses à toutes vos questions !
Profitez des connaissances, des opinions et des expériences des
internautes sur Yahoo! Questions/Réponses