# Map\_if, collect\_if?

**URL:** <https://rubytalk.org/t/map-if-collect-if/26617>\
**Category:** ruby-talk\
**Created:** [13 April 2006 00:30 UTC](https://rubytalk.org/t/map-if-collect-if/26617 "2006-04-13T00:30:20Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bruce\_Woodward](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@Bruce\_Woodward](https://rubytalk.org/u/Bruce_Woodward)\
**Post date:** [13 April 2006 00:30 UTC](https://rubytalk.org/t/map-if-collect-if/26617/1 "2006-04-13T00:30:20Z")

</div>

Hi,

I have been through the pickaxe book, looking at the array and enumerable  
built-in class and module and can't find a way of achieving a map\_if or  
collect\_if.  
From pickaxe, collect "Returns a new array containing the results of running  
block once for every element in enum". Of course map is an alias to collect.  
There is find\_all but it returns an array containing the elements of enum,  
not the results of running the block, so I have written one.  
Is there a better way that already exists?  
thanks,  
Bruce.

module Enumerable

&nbsp;&nbsp;# returns an array containing the results of running block once for every  
element in enum, for which block  
&nbsp;&nbsp;# is not false.  
&nbsp;&nbsp;def map\_if  
&nbsp;&nbsp;&nbsp;&nbsp;rt, r = [], nil  
&nbsp;&nbsp;&nbsp;&nbsp;self.each { |e| (r = yield(e)) and rt \<\< r }  
&nbsp;&nbsp;&nbsp;&nbsp;rt  
&nbsp;&nbsp;end  
&nbsp;&nbsp;alias :collect\_if :map\_if

end

a = [1,2,3,4,5]

b = a.map\_if { |i|  
&nbsp;&nbsp;(i % 2 != 0) and (i \* 10)  
}

p b ## [10, 30, 50]

---

<div class="post-metadata">

**Author:** ![Mark\_Van\_Holstyn1](https://avatars.discourse-cdn.com/v4/letter/m/c6cbf5/32.png) [@Mark\_Van\_Holstyn1](https://rubytalk.org/u/Mark_Van_Holstyn1)\
**Post date:** [13 April 2006 01:04 UTC](https://rubytalk.org/t/map-if-collect-if/26617/2 "2006-04-13T01:04:21Z")

</div>

Try using the 'select' method...

irb(main):002:0\* a = [1,2,3,4,5]  
irb(main):003:0\> b = a.select { |i|  
irb(main):004:1\* (i % 2 != 0) and (i \* 10)  
irb(main):005:1\> }  
=\> [1, 3, 5]

> **···**
>
> On 4/12/06, Bruce Woodward \<bruce.woodward@gmail.com\> wrote:
> 
> > Hi,
> > 
> > I have been through the pickaxe book, looking at the array and enumerable  
> > built-in class and module and can't find a way of achieving a map\_if or  
> > collect\_if.  
> > From pickaxe, collect "Returns a new array containing the results of  
> > running  
> > block once for every element in enum". Of course map is an alias to  
> > collect.  
> > There is find\_all but it returns an array containing the elements of enum,  
> > not the results of running the block, so I have written one.  
> > Is there a better way that already exists?  
> > thanks,  
> > Bruce.
> > 
> > module Enumerable
> > 
> > &nbsp;&nbsp;# returns an array containing the results of running block once for  
> > every  
> > element in enum, for which block  
> > &nbsp;&nbsp;# is not false.  
> > &nbsp;&nbsp;def map\_if  
> > &nbsp;&nbsp;&nbsp;&nbsp;rt, r = , nil  
> > &nbsp;&nbsp;&nbsp;&nbsp;self.each { |e| (r = yield(e)) and rt \<\< r }  
> > &nbsp;&nbsp;&nbsp;&nbsp;rt  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;alias :collect\_if :map\_if
> > 
> > end
> > 
> > a = [1,2,3,4,5]
> > 
> > b = a.map\_if { |i|  
> > &nbsp;&nbsp;(i % 2 != 0) and (i \* 10)  
> > }
> > 
> > p b ## [10, 30, 50]
> 
> --  
> Mark Van Holstyn  
> mvette13@gmail.com  
> [http://lotswholetime.com](http://lotswholetime.com)

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [13 April 2006 03:54 UTC](https://rubytalk.org/t/map-if-collect-if/26617/3 "2006-04-13T03:54:01Z")

</div>

inject is very powerfull:

&nbsp;&nbsp;&nbsp;harp:~ \> cat a.rb  
&nbsp;&nbsp;&nbsp;a = 1 .. 5  
&nbsp;&nbsp;&nbsp;b = a.inject(){|a,i| i[0] == 1 ? a.push(i \* 10) : a}  
&nbsp;&nbsp;&nbsp;p b

&nbsp;&nbsp;&nbsp;harp:~ \> ruby a.rb  
&nbsp;&nbsp;&nbsp;[10, 30, 50]

regards.

-a

> **···**
>
> On Thu, 13 Apr 2006, Bruce Woodward wrote:
> 
> > a = [1,2,3,4,5]
> > 
> > b = a.map\_if { |i|  
> > (i % 2 != 0) and (i \* 10)  
> > }
> > 
> > p b ## [10, 30, 50]
> 
> --  
> be kind whenever possible... it is always possible.  
> - h.h. the 14th dali lama

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [13 April 2006 11:44 UTC](https://rubytalk.org/t/map-if-collect-if/26617/4 "2006-04-13T11:44:51Z")

</div>

module Enumerable  
&nbsp;&nbsp;&nbsp;&nbsp;def map\_if( &block )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;find\_all(&block).map(&block)  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

this for sure is shorter, had no time to test performance though. ( I yield  
twice, you yield only once, which might be costy!)

Cheers  
Robert

> **···**
>
> On 4/13/06, Bruce Woodward \<bruce.woodward@gmail.com\> wrote:
> 
> > Hi,
> > 
> > I have been through the pickaxe book, looking at the array and enumerable  
> > built-in class and module and can't find a way of achieving a map\_if or  
> > collect\_if.  
> > From pickaxe, collect "Returns a new array containing the results of  
> > running  
> > block once for every element in enum". Of course map is an alias to  
> > collect.  
> > There is find\_all but it returns an array containing the elements of enum,  
> > not the results of running the block, so I have written one.  
> > Is there a better way that already exists?  
> > thanks,  
> > Bruce.
> > 
> > module Enumerable
> > 
> > &nbsp;&nbsp;# returns an array containing the results of running block once for  
> > every  
> > element in enum, for which block  
> > &nbsp;&nbsp;# is not false.  
> > &nbsp;&nbsp;def map\_if  
> > &nbsp;&nbsp;&nbsp;&nbsp;rt, r = , nil  
> > &nbsp;&nbsp;&nbsp;&nbsp;self.each { |e| (r = yield(e)) and rt \<\< r }  
> > &nbsp;&nbsp;&nbsp;&nbsp;rt  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;alias :collect\_if :map\_if
> > 
> > end
> > 
> > a = [1,2,3,4,5]
> > 
> > b = a.map\_if { |i|  
> > &nbsp;&nbsp;(i % 2 != 0) and (i \* 10)  
> > }
> > 
> > p b ## [10, 30, 50]
> 
> --  
> Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui  
> concerne l'univers, je n'en ai pas acquis la certitude absolue.
> 
> - Albert Einstein

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [13 April 2006 01:18 UTC](https://rubytalk.org/t/map-if-collect-if/26617/5 "2006-04-13T01:18:36Z")

</div>

Not the effect he was looking for:

b = a.map\_if { |i| (i % 2 != 0) and (i \* 10) }  
p b  
[10, 30, 50]

Personally I think his solution is a little icky, what if you wanted to do

if x  
&nbsp;&nbsp;&nbsp;then map false

a.map\_if { |i| (i % 2 != 0) and false }  
would break down and you'd get an empty array

I would just do

a.select { |i| (i % 2 != 0) }.map { |i| i \* 10 }

if I needed map-if style functionality

> **···**
>
> On Apr 12, 2006, at 9:04 PM, Mark Van Holstyn wrote:
> 
> > Try using the 'select' method...
> > 
> > irb(main):002:0\* a = [1,2,3,4,5]  
> > irb(main):003:0\> b = a.select { |i|  
> > irb(main):004:1\* (i % 2 != 0) and (i \* 10)  
> > irb(main):005:1\> }  
> > =\> [1, 3, 5]

---

<div class="post-metadata">

**Author:** ![Bruce\_Woodward](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@Bruce\_Woodward](https://rubytalk.org/u/Bruce_Woodward)\
**Post date:** [13 April 2006 12:00 UTC](https://rubytalk.org/t/map-if-collect-if/26617/6 "2006-04-13T12:00:57Z")

</div>

Ara,

Thanks for your reply. I have been using inject but shyed away in this case  
because for the 'real' code that I have been working on I am actually  
already using an accumualtor.

So I had;

acc =   
array.each { |v|  
&nbsp;&nbsp;condition\_expression and acc \<\< v.attribute  
}

Using inject seems to be roughly equivalent. In the example above v is a  
instance of the Struct class.

cheers,  
Bruce.

> **···**
>
> On 4/12/06, ara.t.howard@noaa.gov \<ara.t.howard@noaa.gov\> wrote:
> 
> > On Thu, 13 Apr 2006, Bruce Woodward wrote:
> > 
> > \> a = [1,2,3,4,5]  
> > \>  
> > \> b = a.map\_if { |i|  
> > \> (i % 2 != 0) and (i \* 10)  
> > \> }  
> > \>  
> > \> p b ## [10, 30, 50]
> > 
> > inject is very powerfull:
> > 
> > &nbsp;&nbsp;&nbsp;harp:~ \> cat a.rb  
> > &nbsp;&nbsp;&nbsp;a = 1 .. 5  
> > &nbsp;&nbsp;&nbsp;b = a.inject(){|a,i| i[0] == 1 ? a.push(i \* 10) : a}  
> > &nbsp;&nbsp;&nbsp;p b
> > 
> > &nbsp;&nbsp;&nbsp;harp:~ \> ruby a.rb  
> > &nbsp;&nbsp;&nbsp;[10, 30, 50]
> > 
> > regards.
> > 
> > -a  
> > --  
> > be kind whenever possible... it is always possible.  
> > - h.h. the 14th dali lama

---

<div class="post-metadata">

**Author:** ![Bruce\_Woodward](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@Bruce\_Woodward](https://rubytalk.org/u/Bruce_Woodward)\
**Post date:** [13 April 2006 12:14 UTC](https://rubytalk.org/t/map-if-collect-if/26617/7 "2006-04-13T12:14:12Z")

</div>

Robert and Logan,

Thanks for your replies. That was good thinking.

> module Enumerable  
> &nbsp;&nbsp;&nbsp;&nbsp;def map\_if( &block )  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;find\_all(&block).map(&block)  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> end

I particularly like the use of calling block twice. At first I thought that  
this couldn't work but of course once the block has been successful for the  
call to find\_all, it must be successful for the call to map -- very nice.

I will file away the find\_all {}.map {} idiom for future use but for this  
case I will either use a manual accumulator or inject. The only reason is  
performance and performance only matters in this case because the code is  
likely to be get call many hundreds of times in a loop and I will be  
impatiently waiting for it finish.

WRT to performance, not only is the block being called twice but an extra  
array is being created and then freed. In the past I have had issues ruby  
processes using a lot of memory and spending time with the garbage  
collector. I don't want to start any kind of anti-ruby flame war here; Ruby  
is my first choice. Manually stopping the GC at the top of the loop and  
starting the GC at the bottom of the loop, solved the problem but I just  
want to minimse memory use where I can, just for this code.

thanks again.  
Bruce.

> **···**
>
> On 4/13/06, Robert Dober \<robert.dober@gmail.com\> wrote:
> 
> > On 4/13/06, Bruce Woodward \<bruce.woodward@gmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Stephan\_Mueller](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@Stephan\_Mueller](https://rubytalk.org/u/Stephan_Mueller)\
**Post date:** [14 April 2006 15:07 UTC](https://rubytalk.org/t/map-if-collect-if/26617/8 "2006-04-14T15:07:56Z")

</div>

\* ara.t.howard@noaa.gov \<ara.t.howard@noaa.gov\> [060413 06:04]:

> &nbsp;&nbsp;harp:~ \> cat a.rb  
> &nbsp;&nbsp;a = 1 .. 5  
> &nbsp;&nbsp;b = a.inject(){|a,i| i[0] == 1 ? a.push(i \* 10) : a}  
> &nbsp;&nbsp;p b
> 
> &nbsp;&nbsp;harp:~ \> ruby a.rb  
> &nbsp;&nbsp;[10, 30, 50]

I don't get it: why is i[0] == 1 returning true in case i is an integer  
with an odd value?

Thanks & cheers,

Steph.

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [14 April 2006 15:15 UTC](https://rubytalk.org/t/map-if-collect-if/26617/9 "2006-04-14T15:15:56Z")

</div>

For integers gives you the value of the bit at the given index. All odd integers will have an LSB of 1  
i.e.  
1 -\> 0b001  
2 -\> 0b010 # even  
3 -\> 0b011 #odd  
4 -\> 0b100 #even  
5 -\> 0b101 #odd

> **···**
>
> On Apr 14, 2006, at 11:07 AM, Stephan Mueller wrote:
> 
> > I don't get it: why is i[0] == 1 returning true in case i is an integer  
> > with an odd value?
> > 
> > Thanks & cheers,
> > 
> > Steph.

---

<div class="post-metadata">

**Author:** ![Stephan\_Mueller](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@Stephan\_Mueller](https://rubytalk.org/u/Stephan_Mueller)\
**Post date:** [15 April 2006 19:12 UTC](https://rubytalk.org/t/map-if-collect-if/26617/10 "2006-04-15T19:12:54Z")

</div>

\* Logan Capaldo \<logancapaldo@gmail.com\> [060414 17:34]:

> For integers gives you the value of the bit at the given index. All odd integers will have an LSB of 1  
> i.e.

Ah, I see. Thanks for your help!

Cheers,

Steph.
