Math: sum and faculty

I hereby propose two additions to Ruby. Please come with some comments before I file an RCR.

Range#sum

···

=========

Usage
-----

   (lower_limit..upper_limit).sum { |i| expression }

Implementation
--------------

   class Range
     def sum
       raise ArgumentError unless block_given?

       result = 0.0
       each { |i| result += yield(i) }

       result
     end
   end

Notes
-----

Could also be implemented as Math#sum

Integer#faculty

Usage
-----

   n.faculty # => n * (n - 1) * (n - 2) ... 1

Implementation
--------------

   class Integer
     def faculty
       self == 0 ? 1 : self * (self - 1).faculty
     end
   end

Cheers,
Daniel

Daniel Schierbeck wrote:

Range#sum

This could be done with Enumerable#inject. Your implementation assumes that range is for values of Float class.

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.

Am I missing something, but why "faculty" ? Why not factorial ?

HaPK wrote:

Daniel Schierbeck wrote:

Range#sum

This should go into Enumerable if at all.

This could be done with Enumerable#inject.

Exactly.

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 ?

Maybe because of http://akas.imdb.com/title/tt0133751 ? :-))

To sum up, #sum seems too specialized. #inject is there and does the job
quite nicely - and easily.

Kind regards

    robert

HaPK wrote:

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 :slight_smile: ), 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.

···

--
Christophe Grandsire.

http://rainbow.conlang.free.fr

You need a straight mind to invent a twisted conlang.

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)}
collect = enum.accumulate(, :<<) {|x| f(x)}

and if you leave off the block, it simply applies f(x) = x. It avoids
cluttering the block with the accumulator the way inject does.

martin

···

Robert Klemme <bob.news@gmx.net> wrote:

To sum up, #sum seems too specialized. #inject is there and does the job
quite nicely - and easily.

Christophe Grandsire <christophe.grandsire@free.fr> writes:

HaPK wrote:

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 :slight_smile: ), 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... :slight_smile:

···

Christophe Grandsire.

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org

Martin DeMello wrote:

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)}

collect = enum.accumulate(, :<<) {|x| f(x)}

I prefer
collect = enum.map {|x| f(x)}
collect = enum.collect {|x| f(x)}

and if you leave off the block, it simply applies f(x) = x. It avoids
cluttering the block with the accumulator the way inject does.

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...

Kind regards

    robert

···

Robert Klemme <bob.news@gmx.net> wrote:

sum = enum.accumulate(0, :+) {|x| f(x)}

How about

module Enumerable

  def accumulate(initial, method, &block)
    if not block_given?
      block = proc {|x| x}
    end
    inject(initial) {|accumulator, value|

···

#
      # apply block to input value
      #
      accumulator.send(method, block.call(value))
      }
  end

end

It's worth getting to know inject however.

Sean

Christian Neukirchen wrote:

The real reason is probably that both "factorial" (maths) and
"faculty" (university) mean "Fakultät" in German... :slight_smile:

Must be! :slight_smile: 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...

···

--
Christophe Grandsire.

http://rainbow.conlang.free.fr

You need a straight mind to invent a twisted conlang.

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...

> sum = enum.accumulate(0, :+) {|x| f(x)}

How about

module Enumerable

  def accumulate(initial, method, &block)

<snip>

It's worth getting to know inject however.A

Definitely. OTOH, it's also worth stepping back every so often and
seeing if you can "pretty up" your code.

(Incidentally, a fun exercise to get familiar with inject is to try
writing all of Enumerable in terms of inject rather than each.)

martin

···

Sean O'Halpin <sean.ohalpin@gmail.com> wrote:

It seems to be similar in Danish (fakultet), Dutch (fakulteit) and Swedish (fakultet) (from http://www.websters-online-dictionary.org/definition/factorial\). And I think Daniel actually is from Denmark...

···

On Mon, 19 Sep 2005 00:58:40 +0200, Christophe Grandsire <christophe.grandsire@free.fr> wrote:

Christian Neukirchen wrote:

The real reason is probably that both "factorial" (maths) and
"faculty" (university) mean "Fakultät" in German... :slight_smile:

Must be! :slight_smile: 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...

Dominik Bathon wrote:

···

On Mon, 19 Sep 2005 00:58:40 +0200, Christophe Grandsire > <christophe.grandsire@free.fr> wrote:

Christian Neukirchen wrote:

The real reason is probably that both "factorial" (maths) and
"faculty" (university) mean "Fakultät" in German... :slight_smile:

Must be! :slight_smile: 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...

It seems to be similar in Danish (fakultet), Dutch (fakulteit) and Swedish (fakultet) (from http://www.websters-online-dictionary.org/definition/factorial\). And I think Daniel actually is from Denmark...

I am :slight_smile: