[prev in list] [next in list] [prev in thread] [next in thread]
List: ruby-core
Subject: [ruby-core:33717] Re: Why is SyncEnumerator in REXML?
From: Ken Bloom <kbloom () gmail ! com>
Date: 2010-12-14 15:08:23
Message-ID: pan.2010.12.14.00.30.08 () gmail ! com
[Download RAW message or body]
On Wed, 08 Dec 2010 07:25:18 +0900, James Edward Gray II wrote:
> On Dec 7, 2010, at 3:41 PM, Asher wrote:
>
>> Would your preference be to direct them to manually iterate each of the
>> enumerators or create their own convenience class? Seems easy enough,
>> but given that SyncEnumerator is there it made sense to me to leave it
>> and move it into the main extension branch.
>
> Enumerator should be used for this now, as I explained before:
>
>>> enums = [1..3, %w[a b c]].map(&:each)
> => [#<Enumerator: 1..3:each>, #<Enumerator: ["a", "b", "c"]:each>]
>>> loop do
> ?> break if enums.all? { |e| e.peek && false rescue true }
>>> puts enums.map { |e| e.next rescue nil }.join(", ")
>>> end
> 1, a
> 2, b
> 3, c
> => nil
>
> or:
>
>>> enums = [1..3, %w[a b c]]
> => [1..3, ["a", "b", "c"]]
>>> se = enums[0].to_enum(:zip, *enums[1..-1])
> => #<Enumerator: 1..3:zip(["a", "b", "c"])>
>>> loop do
> ?> puts se.next.join(", ")
>>> end
> 1, a
> 2, b
> 3, c
> => nil
>
> James Edward Gray II
Both of these are messy and non-obvious. The convenience of the
SyncIterator is much appreciated, even if it only wraps the logic of one
of these methods.
--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic