'each' returning array 
Author Message
 'each' returning array

A newbie problem:

Is there a way to transparently accumulate expresions evaluated within
a block into an array, without introducing an external variable? For
example, I'd like to write something like this:

"HAL".each_byte__returning_array {|x| (x+1).chr} .collect # more processing

..instead of this:

l=[];  "HAL".each_byte {|x| l<< (x+1).chr};  l.collect #...

One of my first impressions with ruby was the surprise that each*
method family doesn't return such accumulated arrays just for method
chaining.

Am I thinking totally non-rubyish? ;-) If so, please show me the Right
Way..

--
He who foos last foos best



Tue, 16 Aug 2005 04:47:32 GMT  
 'each' returning array
Would Array#map do what you want?

Quote:
>> %w{ H A L }  
=> ["H", "A", "L"]
>> %w{ H A L }.map {|x| (x[0]+1).chr}

=> ["I", "B", "M"]

Quote:

> A newbie problem:

> Is there a way to transparently accumulate expresions evaluated within
> a block into an array, without introducing an external variable? For
> example, I'd like to write something like this:

> "HAL".each_byte__returning_array {|x| (x+1).chr} .collect # more processing

> ...instead of this:

> l=[];  "HAL".each_byte {|x| l<< (x+1).chr};  l.collect #...

> One of my first impressions with ruby was the surprise that each*
> method family doesn't return such accumulated arrays just for method
> chaining.

> Am I thinking totally non-rubyish? ;-) If so, please show me the Right
> Way..

> --
> He who foos last foos best

--
Daniel Carrera
Graduate Teaching Assistant.  Math Dept.
University of Maryland.  (301) 405-5137


Tue, 16 Aug 2005 05:08:17 GMT  
 'each' returning array
Hi,

At Fri, 28 Feb 2003 06:01:31 +0900,

Quote:

> Is there a way to transparently accumulate expresions evaluated within
> a block into an array, without introducing an external variable? For
> example, I'd like to write something like this:

> "HAL".each_byte__returning_array {|x| (x+1).chr} .collect # more processing

Enumerator in "rough".

  require "enumerator"
  "HAL".enum_for(:each_byte).collect {|x| (x+1).chr} # => ["I", "B", "M"]

--
Nobu Nakada



Tue, 16 Aug 2005 05:39:45 GMT  
 'each' returning array

Quote:
> Hi,

> At Fri, 28 Feb 2003 06:01:31 +0900,

> > Is there a way to transparently accumulate expresions evaluated within
> > a block into an array, without introducing an external variable? For
> > example, I'd like to write something like this:

> > "HAL".each_byte__returning_array {|x| (x+1).chr} .collect # more processing

> Enumerator in "rough".

>   require "enumerator"
>   "HAL".enum_for(:each_byte).collect {|x| (x+1).chr} # => ["I", "B", "M"]

how have i lived so long without knowing that!

can you explain more?

-a

--
  ====================================
  | Ara Howard
  | NOAA Forecast Systems Laboratory
  | Information and Technology Services
  | Data Systems Group
  | R/FST 325 Broadway
  | Boulder, CO 80305-3328

  | Phone:  303-497-7238
  | Fax:    303-497-7259
  ====================================



Tue, 16 Aug 2005 05:44:59 GMT  
 'each' returning array

Quote:

>> Is there a way to transparently accumulate expresions evaluated within
>> a block into an array, without introducing an external variable? For
>> example, I'd like to write something like this:

>> "HAL".each_byte__returning_array {|x| (x+1).chr} .collect # more processing

>> ...instead of this:

>> l=[];  "HAL".each_byte {|x| l<< (x+1).chr};  l.collect #...

>> One of my first impressions with ruby was the surprise that each*
>> method family doesn't return such accumulated arrays just for method
>> chaining.

> Would Array#map do what you want?

> >> %w{ H A L }  
> => ["H", "A", "L"]
> >> %w{ H A L }.map {|x| (x[0]+1).chr}
> => ["I", "B", "M"]

In this case: yes, or more general (not involving %w-style literal
array): aString.unpack("C*").map {...}

..but that was only an example. The heart of the matter is that I want
to chain methods like this:

anArray.method1{block1}.method2{block2}

Well, I can just write a general mixin with something like 'each' but
with accumulating block data into an array. I wondered whether is it
done in builtin stuff.

--
We demand rigidly defined areas of doubt and uncertainty!



Tue, 16 Aug 2005 07:16:25 GMT  
 'each' returning array
Hi,

At Fri, 28 Feb 2003 07:01:47 +0900,

Quote:

> > Enumerator in "rough".

> >   require "enumerator"
> >   "HAL".enum_for(:each_byte).collect {|x| (x+1).chr} # => ["I", "B", "M"]

> how have i lived so long without knowing that!

> can you explain more?


from enumerator.txt:
| ** Enumerable::Enumerator(Class)
|
| A class which provides a method `each' to be used as an Enumerable
| object.

--
Nobu Nakada



Tue, 16 Aug 2005 08:30:51 GMT  
 'each' returning array

Quote:

>> Is there a way to transparently accumulate expresions evaluated within
>> a block into an array, without introducing an external variable? For
>> example, I'd like to write something like this:

>> "HAL".each_byte__returning_array {|x| (x+1).chr} .collect # more processing

Finally I've written a 5-lines long mixin, ..

Quote:
> Enumerator in "rough".

>   require "enumerator"
>   "HAL".enum_for(:each_byte).collect {|x| (x+1).chr} # => ["I", "B", "M"]

..but this module is great, nice that it's going to be included in
ruby distro. Thanks!

--
This is not a healthy way of thinking.
(Emacs Psychiatrist)



Wed, 17 Aug 2005 21:33:46 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. 'array of array' declaration

2. (subtypep '(simple-array character (*)) '(array character (*)))

3. (typep (make-array 10 :element-type 'bit) '(array bit (10)))

4. DLL's returning an array of floats

5. return -code return doesn't play nice with set

6. NMTVCUSTOMDRAW does not return 'isChecked'

7. Intellimouse Returns 'File Not Open'

8. Clearing Error()'s and Errorcode()'s return values

9. Change request: return value from 'trap'

10. Newbie confusion about 'return'

11. msvcrt.getch() from IDLE returns '\377'

12. 'read' returns more bytes than requested

 

 
Powered by phpBB® Forum Software