From: Robert Dober Date: 2006-09-14T05:13:30+09:00 Subject: Re: How to collect over two arrays then look back to one? ------=_Part_52044_16612211.1158178371533 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 9/13/06, Jason Nordwick 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 o= f the block you had some great ideas I have stolen below though :) 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 ;) 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 Booth 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? > > > > 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. > > > > > > > --=20 Deux choses sont infinies : l'univers et la b=EAtise humaine ; en ce qui concerne l'univers, je n'en ai pas acquis la certitude absolue. - Albert Einstein ------=_Part_52044_16612211.1158178371533--