Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #4730

Re: Making fibers enumerable

From Peter Zotov <whitequark@whitequark.org>
Newsgroups comp.lang.ruby
Subject Re: Making fibers enumerable
Date 2011-05-18 07:14 -0500
Organization Service de news de lacave.net
Message-ID <1e69b75dd76444f5eb14eb642b2012d1@mail.whitequark.org> (permalink)
References <BANLkTin6qyj3gQkwPZzLBjJjW2Jz3O_+qw@mail.gmail.com> <BANLkTim7dWcpzCwEbJAbR=ZXyGkRPhWLiA@mail.gmail.com>

Show all headers | View raw


 On Wed, 18 May 2011 20:12:43 +0900, Robert Klemme wrote:
> How would your example look if there was another generation that you
> wanted to do concurrently?  Because for the single threaded generator
> case there is already a tool: Enumerator.new.  The example in the
> documentation even uses Fibonacci Numbers as example. :-)
>
> http://www.ruby-doc.org/core/classes/Enumerator.html#M000299
>
> fib_gen = Enumerator.new { |y|
>   a = b = 1
>   loop {
>     y << a
>     a, b = b, a + b
>   }
> }
>
> Now you can do exactly the same as you did with your example
>
> # Find the fibonacci number greater than 1000
> fib_gen.find {|x| x > 1000 }
> # take first 10 fibonacci numbers
> fib_gen.take 10
> # take_while numbers are smaller than 1000
> fib_gen.take_while {|x| x < 1000 }
>
> For this Fiber would be the wrong tool.

 The Enumerator.new uses Fibers internally. Check enumerator.c in the 
 sources;
 besides that, how would you achieve the needed effect without 
 coroutines?

-- 
   WBR, Peter Zotov.

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Making fibers enumerable Rahul Kumar <rahulsinner@gmail.com> - 2011-05-18 02:39 -0500
  Re: Making fibers enumerable Robert Klemme <shortcutter@googlemail.com> - 2011-05-18 06:12 -0500
    Re: Making fibers enumerable Peter Zotov <whitequark@whitequark.org> - 2011-05-18 07:14 -0500
      Re: Making fibers enumerable Robert Klemme <shortcutter@googlemail.com> - 2011-05-18 09:31 -0500
    Re: Making fibers enumerable Rahul Kumar <rahulsinner@gmail.com> - 2011-05-18 10:50 -0500
      Re: Making fibers enumerable Christopher Dicely <cmdicely@gmail.com> - 2011-05-19 08:51 -0500

csiph-web