Monday, November 5, 2012

Ruby 2.0 new feature : Enumerator::Lazy

In functional language, The feature called "lazy evaluation" exists as its key feature. For example, fibonacci in F# is:
Microsoft (R) F# Interactive version 11.0.50727.1
Copyright (c) Microsoft Corporation. All Rights Reserved.

For help type #help;;

> let fibonacci = Seq.cache <| Seq.unfold (fun (current, next) -> Some(current, (next, current + next))) (0I, 1I);;

val fibonacci : seq

> fibonacci |> Seq.nth 100;;
val it : System.Numerics.BigInteger =
  354224848179261915075 {IsEven = false;
                         IsOne = false;
                         IsPowerOfTwo = false;
                         IsZero = false;
                         Sign = 1;}

Ruby 2.0's Enumerator::Lazy can do "lazy evaluation" like this:
irb(main):001:0> RUBY_VERSION
=> "2.0.0"
irb(main):002:0> fibonacci = Enumerator.new { |y|
irb(main):003:1*   a = 0
irb(main):004:1*   b = 1
irb(main):005:1>   loop do
irb(main):006:2*     y << a
irb(main):007:2>     a, b = b, a + b
irb(main):008:2>   end
irb(main):009:1> }.lazy
=> #<Enumerator::Lazy: #<Enumerator: #<Enumerator::Generator:0x23cbbc0>:each>>
irb(main):010:0> def fibonacci.nth(n)
irb(main):011:1>   self.take(n+1).find.with_index { |elem, i| i == n }
irb(main):012:1> end
=> nil
irb(main):013:0> fibonacci.nth(100)
=> 354224848179261915075

If the above sample did not use Enumerator::Lazy, it would take many many seconds.

11 comments:

Tejuteju said...

Thank you.Well it was nice post and very helpful information onRuby on Rails Online Training

picmistikk1 said...

kayseriescortu.com - alacam.org - xescortun.com

마사지블루 said...

This paragraph gives clear idea for the new viewers of blogging, Thank you .

외국인출장

건마탑 said...

I am really happy to say it’s an interesting post to read . I learn new information from your article , you are doing a great job . Keep it up

마사지

coralgrace738♡ said...

Thanks for sharing your info. I truly appreciate your efforts and I will be waiting for your next
write ups thank you once again.
메이저사이트
경마사이트
경마

coralgrace738♡ said...

Hey There. I discovered your weblog the use of msn. This is a very
neatly written article. I will make sure to bookmark it and return to
read more of your helpful info. Thank you
for the post. I will certainly return.
카지노사이트
토토

baccaratsite.top said...

Great blog article. Really looking forward to read more.
카지노사이트

casinositetop.com said...

Wonderful web site. Plenty of useful info here. I am sending it to several pals ans additionally sharing. 카지노사이트

yadongbizz said...

You're so interesting! I don’t think I've truly read through a single thing like this before. So wonderful to discover another person with genuine thoughts on this subject. Seriously. 야한동영상

Click this link
야설

Buket Bal said...

gümüşhane masöz

batman masöz

çorlu masöz

marmaris masöz

fethiye masöz

çeşme masöz

iskenderun masöz

ısparta masöz

kıbrıs masöz

kırklareli masöz

Anonymous said...

Smm Panel
SMM PANEL
iş ilanları
İNSTAGRAM TAKİPÇİ SATIN AL
Hirdavatci burada
beyazesyateknikservisi.com.tr
servis
tiktok jeton hilesi

Post a Comment