yea look into the difference between functional / imperative
programming languages / most languages [Java, C, C++, etc] are
imperative.. inject, filter, map, et al are very common place in
functional languages and require a different mindset than their
imperative cousins. obviously ruby [as well as python] have
functional elements. it's worth taking the time to understand
what they do / they're very useful/powerful when dealing with
collections..
Or an alias inject_operator perhaps...
This thread has discussed "sum" already and it's no good because you
can use any operator. I also like "accumulate" as mentioned, but
inject_operator is sooo clear to me.
···
On 5/22/06, Regg Mr <spamwhite@cox.net> wrote:
Leslie Viljoen wrote:
> On 5/21/06, Jim Weirich <jim@weirichhouse.org> wrote:
>> Jeff Pritchard wrote:
>> > As for Inject, nobody has come up with a wording that makes any sense to
>> > me yet. Does Inject have any aliases/synonyms?
>>
>> How I remember: Inject takes a binary operation (e.g. +) and injects it
>> between each element of a list.
>>
>> [1,2,3].inject { |a,b| a+b } => 1+2+3
>
> Awesome! That really helps me!
On 5/21/06, Jim Weirich <jim@weirichhouse.org> wrote:
Jeff Pritchard wrote:
As for Inject, nobody has come up with a wording that makes any sense to
me yet. Does Inject have any aliases/synonyms?
How I remember: Inject takes a binary operation (e.g. +) and injects it
between each element of a list.
[1,2,3].inject { |a,b| a+b } => 1+2+3
Awesome! That really helps me!
Seems like "sum" would have been a better name.
Only in the case where you're calculating a sum Inject does the
sum-like thing but in a generalized way.
David
--
David A. Black (dblack@wobblini.net)
* Ruby Power and Light, LLC (http://www.rubypowerandlight.com)
> Ruby and Rails consultancy and training
* Author of "Ruby for Rails" from Manning Publications!
> Ruby for Rails
(2..n).inject(1) { |left, right| left * right }
# => n! => 2*3* ... * n
Also
[1,2,3].each {|a| b += a}
is easier to read ...for me.
Perhaps, but there is a fundamental difference between the inject and
each versions. The "each" verison is procedural. It defines state (the
value of b) and how that state changes in each iteration (b += a).
The "inject" version is functional. There are no extra state variables
in our code that need initialization. And the expression itself is the
value, rather than having a side effect on a local variable.
Not that one way is better than the other, just noting differences.
..then again, using inject to find longest seems a bit silly...
longest_sample = data[0]
data.each do |sample|
longest_sample = sample if sample.length > longest_sample.length
end
puts "Longest sample: #{longest_sample.inspect}, length #{longest_length}"
···
On 5/22/06, Leslie Viljoen <leslieviljoen@gmail.com> wrote:
On 5/22/06, Regg Mr <spamwhite@cox.net> wrote:
> Leslie Viljoen wrote:
> > On 5/21/06, Jim Weirich <jim@weirichhouse.org> wrote:
> >> Jeff Pritchard wrote:
> >> > As for Inject, nobody has come up with a wording that makes any sense to
> >> > me yet. Does Inject have any aliases/synonyms?
> >>
> >> How I remember: Inject takes a binary operation (e.g. +) and injects it
> >> between each element of a list.
> >>
> >> [1,2,3].inject { |a,b| a+b } => 1+2+3
> >
> > Awesome! That really helps me!
>
> Seems like "sum" would have been a better name.
Or an alias inject_operator perhaps...
This thread has discussed "sum" already and it's no good because you
can use any operator. I also like "accumulate" as mentioned, but
inject_operator is sooo clear to me.
Iteration 1
cur_sum = 0, elem = 1
cur_sum = cur_sum + elem #=> cur_sum = 0 + 1 (Replace cur_sum with the
results of the block => 1 )
Iteration 2
cur_sum = 1, elem = 2
cur_sum = cur_sum + elem #=> cur_sum = 1 + 2 (Replace cur_sum with the
results of the block => 3 )
Iteration 3
cur_sum = 3, elem = 3
cur_sum = cur_sum + elem #=> cur_sum = 3 + 3 (Replace cur_sum with the
results of the block => 6 )
Return cur_sum #=> 6
I think that part so far is ok. I get this part. but how would I step
through this?
klass = fully_qualified_name.split('::').inject(Module) { |_module,
_symbol|
_module.const_get(_symbol)
}
instance = klass.new
···
On 5/22/06, Leslie Viljoen <leslieviljoen@gmail.com> wrote:
On 5/22/06, Leslie Viljoen <leslieviljoen@gmail.com> wrote:
> On 5/22/06, Regg Mr <spamwhite@cox.net> wrote:
> > Leslie Viljoen wrote:
> > > On 5/21/06, Jim Weirich <jim@weirichhouse.org> wrote:
> > >> Jeff Pritchard wrote:
> > >> > As for Inject, nobody has come up with a wording that makes any
sense to
> > >> > me yet. Does Inject have any aliases/synonyms?
> > >>
> > >> How I remember: Inject takes a binary operation (e.g. +) and
injects it
> > >> between each element of a list.
> > >>
> > >> [1,2,3].inject { |a,b| a+b } => 1+2+3
> > >
> > > Awesome! That really helps me!
> >
> > Seems like "sum" would have been a better name.
>
> Or an alias inject_operator perhaps...
> This thread has discussed "sum" already and it's no good because you
> can use any operator. I also like "accumulate" as mentioned, but
> inject_operator is sooo clear to me.
..though the other uses of inject defy "inject_operator"...
data = [[1, 7, 3], [1, 2], [1, 5, 4, 1], [1, 8]]
longest_sample = nil
longest_length = data.inject(0) do |accum, sample|
if accum > sample.length
accum
else
longest_sample = sample
sample.length
end
end
Christian Neukirchen schrieb:
>
> longest = data.inject{|max, this| [this.length, max.length].max }
>
> (That's how they do it in APL too, btw.)
I know why I prefer Ruby
data = %w"hello christian and APL"
p data.inject{|max, this| [this.length, max.length].max }
# => undefined method `length' for 9:Fixnum (NoMethodError)
This works:
p data.inject(0){|max, this| [this.length, max].max }
And in APL it's 4 Unicode characters plus the symbol:
(max)(reduce)(shape)(each)data,
where (reduce) is an operator on (max) producing the function which
is equivalent to Ruby's inject method.
I must say I'm surprised that my tired old thread is still kicking
around.
If I may summarize, the reason I was confused by Inject is that it is
useful but poorly named.
many thanks for all the replies,
jp
The thread is still kicking exactly because names are important. And
when it comes to inventing stupid names Ruby is a professional. inject?
yieiayeld? How the heck is the meaningless nonsense spellllled? Not
speaking about the fact that restiricting (well, almost) the methods to
a single block/closure/function passed is silly, though not as silly as
doing so behind the scenes (not included in the argument list) and
calling it using a silly keyword.
it is not my style to feed trolls, but I just want to state:
you are polluting this ML
your posts are extremely annoying
I have no way to get rid of your posts without losing potential information
If your email address is authentic I consider informing CPAN about
your behavior.
Have a nice day.
Robert
···
On 3/1/07, Jenda Krynicky <jenda@cpan.org> wrote:
Jeff Pritchard wrote:
> I must say I'm surprised that my tired old thread is still kicking
> around.
>
> If I may summarize, the reason I was confused by Inject is that it is
> useful but poorly named.
>
> many thanks for all the replies,
> jp
The thread is still kicking exactly because names are important. And
when it comes to inventing stupid names Ruby is a professional. inject?
yieiayeld? How the heck is the meaningless nonsense spellllled? Not
speaking about the fact that restiricting (well, almost) the methods to
a single block/closure/function passed is silly, though not as silly as
doing so behind the scenes (not included in the argument list) and
calling it using a silly keyword.
--
We have not succeeded in answering all of our questions.
In fact, in some ways, we are more confused than ever.
But we feel we are confused on a higher level and about more important things.
-Anonymous
I have to ask, from the apparent bitterness and outright hostility of
your frequent and recent posts -- did a Ruby-programmed DARPA GRand
Challenge[1] contestant run over your puppy?
Jeff Pritchard wrote:
> I must say I'm surprised that my tired old thread is still kicking
> around.
>
> If I may summarize, the reason I was confused by Inject is that it is
> useful but poorly named.
>
> many thanks for all the replies,
> jp
The thread is still kicking exactly because names are important. And
when it comes to inventing stupid names Ruby is a professional. inject?
yieiayeld? How the heck is the meaningless nonsense spellllled? Not
speaking about the fact that restiricting (well, almost) the methods to
a single block/closure/function passed is silly, though not as silly as
doing so behind the scenes (not included in the argument list) and
calling it using a silly keyword.