Your implementation assumes
that range is for values of Float class.
Even more so as this restriction does not apply when using inject:
# the sum of all members
enum.inject {|a,b| a + b}
Although, if you want to apply some expression to the value even with
inject you are going to need some starting value unless you do the mapping
beforehand:
# all work
enum.inject(0) {|s,a| s + expr(a)}
enum.inject(0.0) {|s,a| s + expr(a)}
enum.map {|a| expr(a)}.inject {|a,b| a + b}
Integer#faculty
Implementation
--------------
class Integer
def faculty
self == 0 ? 1 : self * (self - 1).faculty
end
end
(1..self).inject(1) { |result, x| result*x }
would be more compact.
You can even do
(1..10).inject {|a,b|a*b}
=> 3628800
(2..10).inject {|a,b|a*b}
=> 3628800
Hint: if there is no argument to #inject the first pair in the block is
fed the first and second element.
Am I missing something, but why "faculty" ? Why not factorial ?
Am I missing something, but why "faculty" ? Why not factorial ?
It's just a (not so much used in my experience) synonym for "factorial" AFAIK. Incidently (and coincidently ), I first saw this word used as a synonym of factorial a few days ago only, in the Haskell98 report. The little I've seen it when googling seems to indicate that its use is limited to programming. The term "factorial" is by far the most used.
Actually, something in between would be quite nice in terms of code
aesthetics. I'm thinking of the specialised but very common case of a
binary accumulator and a unary function, thus (calling it 'accumulate'
for want of a better name):
Am I missing something, but why "faculty" ? Why not factorial ?
It's just a (not so much used in my experience) synonym for
"factorial" AFAIK. Incidently (and coincidently ), I first saw this
word used as a synonym of factorial a few days ago only, in the
Haskell98 report. The little I've seen it when googling seems to
indicate that its use is limited to programming. The term "factorial"
is by far the most used.
The real reason is probably that both "factorial" (maths) and
"faculty" (university) mean "Fakultät" in German...
To sum up, #sum seems too specialized. #inject is there and does
the job quite nicely - and easily.
Actually, something in between would be quite nice in terms of code
aesthetics. I'm thinking of the specialised but very common case of a
binary accumulator and a unary function, thus (calling it 'accumulate'
for want of a better name):
sum = enum.accumulate(0, :+) {|x| f(x)}
The difference isn't really that big, is it?
sum = enum.inject(0) {|s,x| s + f(x)}
The real reason is probably that both "factorial" (maths) and
"faculty" (university) mean "Fakultät" in German...
Must be! However, I found this use in at least one page about Java, and another one (although I don't remember which language it was about, and I can't find it right now), and they didn't strike me as having been written by Germans...
The cool thing about Ruby is that if you don't like the clutter, you
can easily add the method to Enumerable for your own programs.
Greg
···
On 9/15/05, Robert Klemme <bob.news@gmx.net> wrote:
Martin DeMello wrote:
Personally I don't find the "s +" to be cluttering the block. At least
not enough to justify adding this to Enumerable. My 0.02EUR...
The real reason is probably that both "factorial" (maths) and
"faculty" (university) mean "Fakultät" in German...
Must be! However, I found this use in at least one page about Java, and another one (although I don't remember which language it was about, and I can't find it right now), and they didn't strike me as having been written by Germans...
The real reason is probably that both "factorial" (maths) and
"faculty" (university) mean "Fakultät" in German...
Must be! However, I found this use in at least one page about Java, and another one (although I don't remember which language it was about, and I can't find it right now), and they didn't strike me as having been written by Germans...