I'm not sure how I feel about inject's pathological case. What do you
all think should happen in the following code?
[2].inject {|a, i| puts i}
I might have thought that the block would never be called and the method
would return 2.
If the block must be called, I might expect "i" to be nil.
What actually happens is that the block is called and "i" is the same
thing as "a". I did not expect this and I'm trying to figure out how this
is either consistent or useful. Can someone clue me in to this behaviour?
Thank you...
I'm not sure how I feel about inject's pathological case. What do you
all think should happen in the following code?
[2].inject {|a, i| puts i}
I might have thought that the block would never be called and the
method
would return 2.
If the block must be called, I might expect "i" to be nil.
What actually happens is that the block is called and "i" is the
same
thing as "a". I did not expect this and I'm trying to figure out how
this
is either consistent or useful. Can someone clue me in to this
behaviour?
I'm not seeing that:
1) Two element array:
result = [2, 3].inject do |a, i|
puts "a:#{a}"
puts "i:#{i}"
puts "hello"
puts "--------"
end
puts "result=#{result}"
if result.nil?
puts 'yes'
end
--output:--
i:3
hello
···
a:2
--------
result=
yes
2) One element array:
result = [2].inject do |a, i|
puts "a:#{a}"
puts "i:#{i}"
puts "hello"
puts "--------"
end
puts "result=#{result}"
if result.nil?
puts 'yes'
end
I'm not sure how I feel about inject's pathological case. What do you
all think should happen in the following code?
[2].inject {|a, i| puts i}
I might have thought that the block would never be called and the method
would return 2.
If the block must be called, I might expect "i" to be nil.
What actually happens is that the block is called and "i" is the same
thing as "a". I did not expect this and I'm trying to figure out how this
is either consistent or useful. Can someone clue me in to this behaviour?
Thank you...
The value of the whole statement is 2, or, more generally, the first
(and only) element in the enumerable. I guess that's all it can do
since it doesn't have enough elements to call the block even once.
David
···
On Sun, 30 Mar 2008, Just Another Victim of the Ambient Morality wrote:
--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS April 14-17 New York City
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
See http://www.rubypal.com for details and updates!
Actually the block isn't being called:
I guess that's all it can do
since it doesn't have enough elements to call the block even once.
To add to that, on p.456 of pickaxe2 it says that if you don't supply an
argument for the inject call, then the first element in the enumerable
becomes 'a', and it is not included in subsequent iteration.
Apparently, because there are no values to iterate over, the block does
not execute.
"David A. Black" <dblack@rubypal.com> wrote in message
news:Pine.LNX.4.64.0803300208460.27343@rubypal.com...
Hi --
I'm not sure how I feel about inject's pathological case. What do you
all think should happen in the following code?
[2].inject {|a, i| puts i}
I might have thought that the block would never be called and the
method
would return 2.
If the block must be called, I might expect "i" to be nil.
What actually happens is that the block is called and "i" is the same
thing as "a". I did not expect this and I'm trying to figure out how
this
is either consistent or useful. Can someone clue me in to this
behaviour?
Thank you...
The value of the whole statement is 2, or, more generally, the first
(and only) element in the enumerable. I guess that's all it can do
since it doesn't have enough elements to call the block even once.
D'oh!
I was confused by IRB's interface, for no good reason. I'll put the
crack pipe down, now. It's doing exactly what I thought it should do...
···
On Sun, 30 Mar 2008, Just Another Victim of the Ambient Morality wrote:
def collect_repeats_inject list
return if == list
list[1..-1].inject([[ list.first ]]){|a,e|
if a[-1][0] == e
a[-1] << e
else
a << [e]
end
a
}.reject{|lst| lst.size < 2 }
end
def collect_repeats list
accum = [ [ list.shift ] ]
list.each{|e|
if accum[-1][0] == e
accum[-1] << e
else
accum << [e]
end }
accum.reject{|lst| lst.size < 2 }
end
p collect_repeats( %w(0 1 1 2 3 3 3 3 4 5 5 6) )
p collect_repeats( )
p collect_repeats_inject( %w(0 1 1 2 3 3 3 3 4 5 5 6) )
p collect_repeats_inject( )
def collect_repeats_inject list
return if == list
list[1..-1].inject([[ list.first ]]){|a,e|
if a[-1][0] == e
a[-1] << e
else
a << [e]
end
a
}.reject{|lst| lst.size < 2 }
end
def collect_repeats list
return if == list
accum = [ [ list.first ] ]
list[1..-1].each{|e|
if accum[-1][0] == e
accum[-1] << e
else
accum << [e]
end }
accum.reject{|lst| lst.size < 2 }
end
p collect_repeats( %w(0 1 1 2 3 3 3 3 4 5 5 6) )
p collect_repeats( )
p collect_repeats_inject( %w(0 1 1 2 3 3 3 3 4 5 5 6) )
p collect_repeats_inject( )
Or much better, understand that it's a way to reduce/fold the elements
of an enumerable into a single value.
Enumerable#inject is very useful, as long as you don't treat it as
Maslow's hammer and use it in inappropriate ways, such as giving it a
block like in the original posting to this thread.
···
On 3/30/08, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
By the way, using inject() is inefficient--not to mention confusing.
You might as well pretend it doesn't exist.
So 7stud, you learned Python and Ruby, decided you prefer Python, and now hang out on the Ruby Talk mailing list bad mouthing our language? What's the point, if you don't mind my asking?
James Edward Gray II
···
On Mar 30, 2008, at 1:27 AM, 7stud -- wrote:
By the way, using inject() is inefficient--not to mention confusing.
You might as well pretend it doesn't exist.
I absolutely can't agree with that. map and inject are two beautiful
functions which should be known for the average Ruby programmer imo.
Instead of using some loops, you can easily use map to apply a
function on each member. I like them.
···
On Sun, Mar 30, 2008 at 8:27 AM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
Completely agree with Rick and furthermore you will not be able to
understand Ruby code written by others if you do not have a basic
understanding of inject.
I found this code in Ruby1.9 though
if result.size > 0 and result.inject(false) {|k,s| s or k}
that kind of code explains why inject has a bad reputation.
Cheers
Robert
···
On Sun, Mar 30, 2008 at 3:34 PM, Rick DeNatale <rick.denatale@gmail.com> wrote:
On 3/30/08, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
> By the way, using inject() is inefficient--not to mention confusing.
> You might as well pretend it doesn't exist.
Enumerable#inject is very useful, as long as you don't treat it as
Maslow's hammer and use it in inappropriate ways, such as giving it a
block like in the original posting to this thread.
By the way, using inject() is inefficient--not to mention confusing.
You might as well pretend it doesn't exist.
I absolutely can't agree with that. map and inject are two beautiful
functions which should be known for the average Ruby programmer imo.
Instead of using some loops, you can easily use map to apply a
function on each member. I like them.
By the way, using inject() is inefficient--not to mention confusing.
You might as well pretend it doesn't exist.
There is some truth to that.
def collect_repeats_inject list
return if == list
list[1..-1].inject([[ list.first ]]){|a,e|
if a[-1][0] == e
a[-1] << e
else
a << [e]
end
a
}.reject{|lst| lst.size < 2 }
end
def collect_repeats list
accum = [ [ list.shift ] ]
list.each{|e|
if accum[-1][0] == e
accum[-1] << e
else
accum << [e]
end }
accum.reject{|lst| lst.size < 2 }
end
p collect_repeats( %w(0 1 1 2 3 3 3 3 4 5 5 6) )
p collect_repeats( )
p collect_repeats_inject( %w(0 1 1 2 3 3 3 3 4 5 5 6) )
p collect_repeats_inject( )
Syntax error (missing operator) in query expression ''http://
www.google.ca/search?q=Maslow’s+hamme
ROFL,
Thufir
···
On Sun, 30 Mar 2008 22:34:19 +0900, Rick DeNatale wrote:
Enumerable#inject is very useful, as long as you don't treat it as
Maslow's hammer and use it in inappropriate ways, such as giving it a
block like in the original posting to this thread.
On Mar 30, 11:51 am, James Gray <ja...@grayproductions.net> wrote:
On Mar 30, 2008, at 1:27 AM, 7stud -- wrote:
> By the way, using inject() is inefficient--not to mention confusing.
> You might as well pretend it doesn't exist.
I strongly disagree with that statement.
So 7stud, you learned Python and Ruby, decided you prefer Python, and
now hang out on the Ruby Talk mailing list bad mouthing our language?
What's the point, if you don't mind my asking?
It's because the same thing has surfaced in several languages using
different terms, and those languages have in turn influenced others.
AFAIK, Matz correct me if I'm wrong, Ruby got the name inject from
Smalltalk, along with collect, select, detect and several other
methods. The other three I mention have alias, map for collect,
find_all for select, and find for detect, but inject is still just
inject in Ruby.
···
On Sun, Mar 30, 2008 at 10:30 AM, Marc Heiler <shevegen@linuxmail.org> wrote:
> Understand that it's a way to reduce/fold the elements
> of an enumerable into a single value.
Not that this is important, but personally I never liked the name
.inject
Though fold(l/r) is not much better either.
A peculiar thing is that the wikipedia page gives "also known variously
as " four different names as alternative/example.