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

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.

On 9/13/06, Peter B. [email protected] wrote:

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

Peter

Hmm this seems interestingly complicated - or I am just dully stupid;)

Well here is what I came up with:

a = [*1…20]
b = a.map{ |e| “Line #{e}” }

def ranges a, b, n, &block

The following is what you were asking for :wink:

b.values_at(*(0..a.length-1).zip(a).map{|e| block.call(e.last) ?

e.first: nil}.
compact.map{|i|
[*(i-n)…(i+n)]}.flatten.uniq.reject{|i|i<0}).compact

end

p ranges(a,b,1) { |e| (e%3).zero? }
p ranges(a,b,2) { |e| (e%3).zero? }
p ranges(a,b,1) { |e| (e%5).zero? }
p ranges(a,b,2) { |e| (e%5).zero? }

HTH
Robert

PS:
Golfers cut it down :wink:

R


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

I think…

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

Ruby seems a little verbose on this one.

-j

On 9/13/06, Jason N. [email protected] wrote:

I think…

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

I am afraid this does not what you want, select returns i’s not the
value of
the block
you had some great ideas I have stolen below though :slight_smile:
I’ll try to fix your interpretation, ah I see you think he just wants
the
indicies, so your solution is (almost) great, you can use [*0…a.size]
but
you might have a problem with f(nil) so use
[0…a.size-1]
OP also stated he wanted g(b[something]) not g(something) it is a riddle
an alternative might be
b.values_at([0…a.size-1].select{|i|f a[i]}.map{|i|
g(b.values_at(
[
(i-n)…(i+n)].reject
etc. etc.

Ruby seems a little verbose on this one.

-j

I still think OP thaught g to be something abstract so I would write

def ranges a, b, n, &block

The following is what you were asking for :wink:

b.values_at(
*(0..a.length-1).select{|i|  block.call(a[i]) }.map{|i| 

[*(i-n)…(i+n)]
}.
flatten.uniq.reject{|i|i<0}).compact

end

thanks to your ideas letting me see my stupid mistakes…

Robert

Peter B. wrote:

Can the block inside Enumerable#collect know it’s position inside the
recipient(s).
Europe


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

Peter B. wrote:

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| ii}
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] ))
}

Peter B. [email protected] writes:

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?

Kind of. But first I’m going to re-arrange your email to answer more
didactically. :slight_smile:

Can the block inside Enumerable#collect know it’s position inside the
Enumerable?

Enumerable::Enumerator – require ‘enumerator’ – can help you out with
this.
It lets you create an Enumerable object which can call any ‘each’-like
method
of any other object. You can in turn call the ‘collect’ method of the
Enumerator object. So:

array.to_enum(:each_with_index).collect { |value, index| ... }

(Using :each_with_index is a common enough case that Enumerator also
adds
#enum_with_index to Enumerable.)

Can one collect two parallel arrays?

In several ways… The most basic is the Enumerable#zip method:

(0..3).zip(('a'..'d')) # => [[0, "a"], [1, "b"], [2, "c"], [3, "d"]]

It returns a new array mapping each element of the receiving Enumerable
to the
corresponding elements of the Enumerables passed as arguments.

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.

Enumerable#inject is the most idiomatic way to perform most Enumerable
iteration not directly supported by other methods. If I understand you
properly, in this case you probably want something like:

a.enum_with_index.inject([]) do |result, (v, n)|
  result << (f(v) ? g(b[n-3..n+3]) : false)
end

Hope this helps!

-Marshall

[email protected] writes:

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

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

Hi –

On Thu, 14 Sep 2006, Peter B. wrote:

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

David

On Fri, Sep 15, 2006 at 06:15:59PM +0900, Marshall T. Vandegrift wrote:

[email protected] writes:

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

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!