Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How does 'inject' work on enumerables? 1

Status
Not open for further replies.

Trevoke

Programmer
Jun 6, 2002
1,142
US
I read the rdoc but it's a little verbose for me.. I found this code online to compute nCr :
Code:
def nCr(n,r)
  a, b = r, n-r
  a, b = b, a if a < b  # a is the larger
  numer = (a+1..n).inject(1) { |t,v| t*v }  # n!/r!
  denom = (2..b).inject(1) { |t,v| t*v }    # (n-r)!
  numer/denom
end

Rdoc says ..
enum.inject(initial) {| memo, obj | block } => obj
enum.inject {| memo, obj | block } => obj

Combines the elements of enum by applying the block to an accumulator value (memo) and each element in turn. At each step, memo is set to the value returned by the block. The first form lets you supply an initial value for memo. The second form uses the first element of the collection as a the initial value (and skips that element while iterating).

Can someone give me .. Easier english, please?

Tao Te Ching Discussions : Chapter 9 (includes links to previous chapters)
What is the nature of conflict?
 
The block takes two arguments: a "current value" (memo), and the next item in the sequence (obj).

The block specifies an operation that is to be performed on its arguments (the current value and the next item), resulting in a new value.

The block is called for each element in the sequence. The new value produced from each call is used for the "current value" argument to the next call; the first "current value" is either the argument to inject or the first item in the sequence (if no argument was given).


It's probably easier to understand with an example:
Code:
# Sum the numbers in a list
numbers = [2, 4, 6, 7, 3, 15, 2, 27]

numbers.inject do |currentSum, nextItem|
  currentSum + nextItem
end

The block adds each value in the sequence to the current sum (the sum of all preceding values). You can imagine it as the following expression:

(((((((2 + 4) + 6) + 7) + 3) + 15) + 2) + 27)

where each parenthesis pair represents a separate call of the block whose return value is used in the next call to the block, evaluated outward.


That may or may not make any sense. If not, I'll try again tomorrow.

Also, are you a native English speaker? I'm not sure if you mean there's a language barrier keeping you from understanding that paragraph, or just that you don't grasp what it's saying.
 
I'm not a native speaker - after your explanation, the rdoc that I posted makes a lot more sense!
I can see where that would come in handy..
Thank you very much :)

Tao Te Ching Discussions : Chapter 9 (includes links to previous chapters)
What is the nature of conflict?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top