Faster way to pick the every other array member?

D

Damphyr

we have [1,2,3,4,5,6,7] and want [2,4,6]

Is there a way other than

fields=[1,2,3,4,5,6,7]
fields.collect!{|f|
f if fields.index(f)%2==1
}.compact!

And by another way I mean something shorter and more beautiful :).
Cheers,
V.-
--
http://www.braveworld.net/riva

____________________________________________________________________
http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
http://www.freemail.gr - free email service for the Greek-speaking.
 
C

Christophe Poucet

Hello

You can try the following:

fields = ["a", "b", "c", "d", "e", "f"]
new_fields =[]
fields.each_index {|i| new_fields << fields if i %2 == 0}

Cheers,
Christophe
 
S

Simon Kröger

Damphyr said:
we have [1,2,3,4,5,6,7] and want [2,4,6]

Is there a way other than

fields=[1,2,3,4,5,6,7]
fields.collect!{|f|
f if fields.index(f)%2==1
}.compact!

And by another way I mean something shorter and more beautiful :).
Cheers,
V.-

what about:

f = true
fields = fields.select{f=!f}

cheers

Simon
 
C

Christophe Poucet

To be honest, I think that the hopscotching technique as well as:
f = true
fields = fields.select{f=!f}

are rather dirty hacks. They are short but they do not clearly convey
the meaning of the program.
 
G

Gene Tani

Christophe said:
To be honest, I think that the hopscotching technique as well as:


are rather dirty hacks. They are short but they do not clearly convey
the meaning of the program.

There's probably dozens of ways to do it:

require 'enumerator'
(1..10).each_slice(3) {|slc| p slc[0]}
 
D

David A. Black

Hi --

we have [1,2,3,4,5,6,7] and want [2,4,6]

Is there a way other than

fields=[1,2,3,4,5,6,7]
fields.collect!{|f|
f if fields.index(f)%2==1
}.compact!

And by another way I mean something shorter and more beautiful :).

Here's one possibility:

irb(main):016:0> a = [1,2,3,4,5,6,7]
=> [1, 2, 3, 4, 5, 6, 7]
irb(main):017:0> res = []
=> []
irb(main):018:0> (1...a.size).step(2) {|n| res << a[n] }
=> 1...7
irb(main):019:0> res
=> [2, 4, 6]


David
 
D

daz

Damphyr said:
we have [1,2,3,4,5,6,7] and want [2,4,6]


( A more general version of Simon's:
f=true; p a.select{f=!f} ):

#--------
ix=-1; p a.select{ (ix+=1)%2==1 } #-> [2, 4, 6]
#--------

(Not pretty ... not my fault)


I would prefer not having to maintain the index
in these situations. Something like this would
suit many similar needs:

<pseudo>
p a.select{ _i_%2==1 }
</>

- where _i_ (or whatever) is the index that Ruby
is using internally.


daz
 
G

Gregory Brown

And by another way I mean something shorter and more beautiful :).

class Array

def skipper(interval=3D2,position=3D0)
(position...length).select { |i| i % interval =3D=3D 0 }.map { |i| se=
lf }
end

end
 
M

Matthias Georgi

Another one:

module Enumerable
def select_with_index
index = 0
select { |item|
res = yield item, index
index += 1
res
}
end
end

[1,2,3,4,5,6,7].select_with_index {|n, i| i % 2 == 1 }

I know this is basically the same, but you can see clearly what is
happening.
 
P

Pit Capitain

Damphyr said:
we have [1,2,3,4,5,6,7] and want [2,4,6]

Is there a way other than

fields=[1,2,3,4,5,6,7]
fields.collect!{|f|
f if fields.index(f)%2==1
}.compact!

And by another way I mean something shorter and more beautiful :).

fields.values_at(*(0...fields.size).select{|i|i%2==1})

Regards,
Pit
 
W

William James

Damphyr said:
we have [1,2,3,4,5,6,7] and want [2,4,6]

Is there a way other than

fields=[1,2,3,4,5,6,7]
fields.collect!{|f|
f if fields.index(f)%2==1
}.compact!

fields.values_at( 1,3,5 )

or

fields.values_at( *Array.new(fields.size/2){|i|i*2+1} )
 
J

Jeff Wood

------=_Part_6752_4147753.1132964012276
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

I'm kind of surprised that you don't get an array of values back if you cal=
l
Range#step(x) without a block.

That would make things much easier ...

a.values_at( (2..6).step(2) )

... at least, that is what would make sense to me ... anybody else?

j.


res=3D[]; 1.step(a.size-1,2) {|n| res << a[n] }


--
"Remember. Understand. Believe. Yield! -> http://ruby-lang.org"

Jeff Wood

------=_Part_6752_4147753.1132964012276--
 
D

David A. Black

Hi --

I'm kind of surprised that you don't get an array of values back if you call
Range#step(x) without a block.

That would make things much easier ...

a.values_at( (2..6).step(2) )

... at least, that is what would make sense to me ... anybody else?

I can see your reasoning though I tend to come down on the side of not
liking iterators to create accumulator arrays unless you ask them to.
It's kind of the same thing as the (much discussed) question of
whether a.zip(b) with a block should also return the zipped array (as
opposed to having the block be a way to avoid creating the whole
array).


David
 
M

Mauricio Fernández

I'm kind of surprised that you don't get an array of values back if you call
Range#step(x) without a block.

That would make things much easier ...

a.values_at( (2..6).step(2) )

... at least, that is what would make sense to me ... anybody else?

a = (0..20).to_a
a.values_at( *(2..6).step(2) ) # => [2, 4, 6]
RUBY_VERSION # => "1.9.0"
 
J

Jeff Wood

------=_Part_10126_3801446.1133008181711
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Other than having to do the * that's very cool.

It should be smart enought to figure out input param types ....

or maybe add to the syntax for ranges ? ( 2..6 by 2 ) could be cool too

Anyways, thanks for the note about 1.9

j.


I'm kind of surprised that you don't get an array of values back if you call
Range#step(x) without a block.

That would make things much easier ...

a.values_at( (2..6).step(2) )

... at least, that is what would make sense to me ... anybody else?

a =3D (0..20).to_a
a.values_at( *(2..6).step(2) ) # =3D> [2, 4, 6]
RUBY_VERSION # =3D> "1.9.0"


--
"Remember. Understand. Believe. Yield! -> http://ruby-lang.org "

Jeff Wood

------=_Part_10126_3801446.1133008181711--
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top