Enumerator in ruby 1.9.

A

Artem Voroztsov

# One could write
module Enumerable
def sum
inject(0) {|f,x| f+x}
end
end

# And we have
puts [1,2,3,4].sum # => 10

# But it works only for numbers.
# I would like to write more general code:

module Enumerable
def sum
each.skip_first.inject(first) {|f,x| f+x}
end
end

# so we have:
puts [1,2,3,4].sum #=> 10
puts ['a','b','c'].sum #=> 'abc'
------------------------

1) Is it a good idea to add method ''first' to Enumerable?
2) Please, show me the _right_ code for Enumerator#skip_first(n=1) (ruby1.9)

Thanks!
 
D

David A. Black

Hi --

# One could write
module Enumerable
def sum
inject(0) {|f,x| f+x}
end
end

# And we have
puts [1,2,3,4].sum # => 10

# But it works only for numbers.
# I would like to write more general code:

module Enumerable
def sum
each.skip_first.inject(first) {|f,x| f+x}
end
end

# so we have:
puts [1,2,3,4].sum #=> 10
puts ['a','b','c'].sum #=> 'abc'

If you don't give an argument to inject, it uses the first element in
the collection. So you can just do:

%w{a b c}.inject {|acc,e| acc + e }


David
 
A

Artem Voroztsov

Sorry, I've found than 'inject' has form without argument and it does the job.

module Enumerable
def sum
inject {|f,x| f+x}
end
end

But i am still interested in:
1) Is it a good idea to add method ''first' to Enumerable?
2) Please, show me the _right_ code for Enumerator#skip_first(n=1) (ruby1.9)


And one more question offtopic:

I see my messages duplicated in list. I use gmail.com
What should I do to stop duplicating my messages?
 
T

Thomas Wieczorek

And one more question offtopic:

I see my messages duplicated in list. I use gmail.com

I assume the following: You send a message to the ruby-talk, GMail
saves a copy in the conversation, ruby-talk sends you your email.
Not sure if it is like that, but that's how I explain it to me.
What should I do to stop duplicating my messages?

I am using Better GMail and delete my duplicated emails when I decide
to archive a ruby-talk conversation.
 
A

Artem Voroztsov

2008/3/13 said:
I assume the following: You send a message to the ruby-talk, GMail
saves a copy in the conversation, ruby-talk sends you your email.
Not sure if it is like that, but that's how I explain it to me.




I am using Better GMail and delete my duplicated emails when I decide
to archive a ruby-talk conversation.
Thanks!
 
A

Artem Voroztsov

For example

----------------
module Enumerable
class Enumerator
def skip_first(n=1)
n.times {puts "Skiping #{self.next}"}
self
end
end
end

puts [1,2,3,4].each.skip_first(2).map{|i| i}
----------------

Outputs

Skiping 1
Skiping 2
1
2
3
4

(ruby 1.9.0 (2007-12-25 revision 14709) [i386-mswin32])

So it does not skips in fact. Enumerator restarts inside method map.
It looks strange for me.

Can anyone help me to write 'skip_first' method that works?

Artem
 
J

James Gray

But i am still interested in:
1) Is it a good idea to add method ''first' to Enumerable?

You can use Ruby 1.9's take() for this:

$ ruby_dev -ve 'p((1..10).take(1).first)'
ruby 1.9.0 (2008-03-01 revision 15664) [i686-darwin9.2.0]
1
2) Please, show me the _right_ code for Enumerator#skip_first(n=1)
(ruby1.9)

Use take()'s cousin drop():

$ ruby_dev -ve 'p((1..10).drop(1))'
ruby 1.9.0 (2008-03-01 revision 15664) [i686-darwin9.2.0]
[2, 3, 4, 5, 6, 7, 8, 9, 10]

James Edward Gray II
 
A

Artem Voroztsov

2008/3/13 said:
But i am still interested in:
1) Is it a good idea to add method ''first' to Enumerable?


You can use Ruby 1.9's take() for this:

$ ruby_dev -ve 'p((1..10).take(1).first)'
ruby 1.9.0 (2008-03-01 revision 15664) [i686-darwin9.2.0]

1

Thanks.

2) Please, show me the _right_ code for Enumerator#skip_first(n=1)
(ruby1.9)


Use take()'s cousin drop():

$ ruby_dev -ve 'p((1..10).drop(1))'
ruby 1.9.0 (2008-03-01 revision 15664) [i686-darwin9.2.0]
[2, 3, 4, 5, 6, 7, 8, 9, 10]

James Edward Gray II

I want to have 'drop' for Enumerator so that no new collection was created.
It's important when collection is big one.
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top