How to collect over two arrays then look back to one?

P

Peter Booth

I am reading Ruby for Rails and Ruby Cookbock and delighted by the
Array#find_all and Enumerable#collect methods. I'm wondering what the
idiomatic way to do the following is?

Two parallel arrays A, B . For each element n of A for which f(An) is true,
is g(B[n-3:n+3]) true? Where B[n-3:n+3] refers to seven elements of B whose
position is centered over An' position.

Can the block inside Enumerable#collect know it's position inside the
Enumerable? Can one collect two parallel arrays?

Peter
----------------------------------------------------------------------------
----------------------------------------

The information contained in and accompanying this communication is strictly
confidential and intended solely for the use of the intended recipient(s).
If you have received it by mistake please let us know by reply and then
delete it from your system; you should not copy the message or disclose its
content to anyone.
MarketAxess reserves the right to monitor the content of emails sent to or
from its systems.
Any comments or statements made are not necessarily those of MarketAxess.
For more information, please visit www.marketaxess.com. MarketAxess Europe
Limited is regulated in the UK by the FSA, registered in England no.
4017610, registered office at 71 Fenchurch Street, London, EC3M 4BS.
Telephone (020) 7709 3100.
MarketAxess Corporation is regulated in the USA by the SEC and the NASD,
incorporated in Delaware, executive offices at 140 Broadway, New York, NY
10005. Telephone (1) 212 813 6000.
 
J

Jason Nordwick

I think...

a.values_at(*(0...a.size).to_a.select {|i| f(a) && g([i-3,0].max..[i+3,a.size-1].min)})

Ruby seems a little verbose on this one.

-j
 
W

William James

Peter said:
I am reading Ruby for Rails and Ruby Cookbock and delighted by the
Array#find_all and Enumerable#collect methods. I'm wondering what the
idiomatic way to do the following is?

Two parallel arrays A, B . For each element n of A for which f(An) is true,
is g(B[n-3:n+3]) true? Where B[n-3:n+3] refers to seven elements of B whose
position is centered over An' position.

Can the block inside Enumerable#collect know it's position inside the
Enumerable? Can one collect two parallel arrays?

a = Array.new(12) {|i| i*i}
b = Array.new(12) {|i| i*3}
def f(n)
1 == n % 2
end
def g(a)
return false if a.size != 7
0 == a.inject{|p,n| p*n}/a.inject{|s,n| s+n} % 4
end
result = []
a.each_with_index{ |e,i|
result << (f(e) && g( b[i-3 .. i+3] ))
}
 
D

dblack

Hi --

I am reading Ruby for Rails and Ruby Cookbock and delighted by the
Array#find_all and Enumerable#collect methods. I'm wondering what the
idiomatic way to do the following is?

Two parallel arrays A, B . For each element n of A for which f(An) is true,
is g(B[n-3:n+3]) true? Where B[n-3:n+3] refers to seven elements of B whose
position is centered over An' position.

Can the block inside Enumerable#collect know it's position inside the
Enumerable? Can one collect two parallel arrays?

To answer the first question: no. You are free to join Citizens for
map_with_index, of which I am the founder and president :)


David

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 
L

Logan Capaldo

What about Citizens Who Are Happy With enum_with_index.map ? I think they
have a thing-or-two to say on this issue...

-Marshall
Here, here! Down with the giant collection of methods! Up with
higher order methods!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top