# Map\_if, collect\_if?

**URL:** <https://www.ruby-forum.com/t/map-if-collect-if/57690>\
**Category:** Ruby\
**Created:** [April 13, 2006, 2:33am UTC](https://www.ruby-forum.com/t/map-if-collect-if/57690 "2006-04-13T02:33:30Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [April 13, 2006, 2:33am UTC](https://www.ruby-forum.com/t/map-if-collect-if/57690/1 "2006-04-13T02:33:30Z")

</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

# returns an array containing the results of running block once for

every  
element in enum, for which block

# is not false.

def map\_if  
rt, r = [], nil  
self.each { |e| (r = yield(e)) and rt \<\< r }  
rt  
end  
alias :collect\_if :map\_if

end

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

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

p b ## [10, 30, 50]

---

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [April 13, 2006, 3:06am UTC](https://www.ruby-forum.com/t/map-if-collect-if/57690/2 "2006-04-13T03:06:45Z")

</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]

---

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [April 13, 2006, 3:21am UTC](https://www.ruby-forum.com/t/map-if-collect-if/57690/3 "2006-04-13T03:21:50Z")

</div>

On Apr 12, 2006, at 9:04 PM, Mark Van H. 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]

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  
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

---

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [April 13, 2006, 5:56am UTC](https://www.ruby-forum.com/t/map-if-collect-if/57690/4 "2006-04-13T05:56:55Z")

</div>

On Thu, 13 Apr 2006, Bruce W. 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:

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

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

regards.

-a

---

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [April 13, 2006, 2:02pm UTC](https://www.ruby-forum.com/t/map-if-collect-if/57690/5 "2006-04-13T14:02:00Z")

</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|  
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.

---

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [April 13, 2006, 1:46pm UTC](https://www.ruby-forum.com/t/map-if-collect-if/57690/6 "2006-04-13T13:46:42Z")

</div>

On 4/13/06, Bruce W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> There is find\_all but it returns an array containing the elements of enum,
> 
> # is not false.
> 
> b = a.map\_if { |i|  
> (i % 2 != 0) and (i \* 10)  
> }
> 
> p b ## [10, 30, 50]

module Enumerable  
def map\_if( &block )  
find\_all(&block).map(&block)  
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

–  
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:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [April 14, 2006, 5:08pm UTC](https://www.ruby-forum.com/t/map-if-collect-if/57690/7 "2006-04-14T17:08:40Z")

</div>

- removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) [060413 06:04]:

> harp:~ \> cat a.rb  
> a = 1 … 5  
> b = a.inject([]){|a,i| i[0] == 1 ? a.push(i \* 10) : a}  
> p b
> 
> harp:~ \> ruby a.rb  
> [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:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [April 13, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/map-if-collect-if/57690/8 "2006-04-13T14:17:25Z")

</div>

Robert and Logan,

Thanks for your replies. That was good thinking.

On 4/13/06, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 4/13/06, Bruce W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> module Enumerable  
> def map\_if( &block )  
> find\_all(&block).map(&block)  
> 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.

---

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [April 14, 2006, 5:17pm UTC](https://www.ruby-forum.com/t/map-if-collect-if/57690/9 "2006-04-14T17:17:51Z")

</div>

On Apr 14, 2006, at 11:07 AM, Stephan M. 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.

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

---

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [April 15, 2006, 9:16pm UTC](https://www.ruby-forum.com/t/map-if-collect-if/57690/10 "2006-04-15T21:16:31Z")

</div>

- Logan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) [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.
