# Functional with Ruby

**URL:** <https://rubytalk.org/t/functional-with-ruby/21711>\
**Category:** ruby-talk\
**Created:** [19 October 2005 18:41 UTC](https://rubytalk.org/t/functional-with-ruby/21711 "2005-10-19T18:41:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andreas\_Semt](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@Andreas\_Semt](https://rubytalk.org/u/Andreas_Semt)\
**Post date:** [19 October 2005 18:41 UTC](https://rubytalk.org/t/functional-with-ruby/21711/1 "2005-10-19T18:41:09Z")

</div>

Hello list,

here a really simple Haskell function, which sums all elements of a list  
(from the book "The Haskell School of Expression"):

> **···**
>
> --------------------------------  
> listSum [] = 0  
> listSum (x : xs) = x + listSum xs  
> --------------------------------
> 
> Which is the best way to say it in Ruby (functional style)? Don't use  
> 'enum.inject', please!
> 
> Thanks for your thoughts.  
> Greetings,  
> Andreas Semt

---

<div class="post-metadata">

**Author:** ![Tomsonp](https://avatars.discourse-cdn.com/v4/letter/t/48db29/32.png) [@Tomsonp](https://rubytalk.org/u/Tomsonp)\
**Post date:** [19 October 2005 18:56 UTC](https://rubytalk.org/t/functional-with-ruby/21711/2 "2005-10-19T18:56:58Z")

</div>

In article \<8759.1129747265@www88.gmx.net\>,

> **···**
>
> Andreas Semt \<Andreas.Semt@gmx.net\> wrote:
> 
> > Hello list,
> > 
> > here a really simple Haskell function, which sums all elements of a list  
> > (from the book "The Haskell School of Expression"):  
> > --------------------------------  
> > listSum = 0  
> > listSum (x : xs) = x + listSum xs  
> > --------------------------------
> > 
> > Which is the best way to say it in Ruby (functional style)? Don't use  
> > 'enum.inject', please!
> 
> Ummm, why wouldn't we be able to use inject? I was under the impression that  
> inject was the functional way to do it in Ruby.
> 
> Phil

---

<div class="post-metadata">

**Author:** ![Ryan\_Leavengood](https://avatars.discourse-cdn.com/v4/letter/r/a587f6/32.png) [@Ryan\_Leavengood](https://rubytalk.org/u/Ryan_Leavengood)\
**Post date:** [19 October 2005 18:59 UTC](https://rubytalk.org/t/functional-with-ruby/21711/3 "2005-10-19T18:59:50Z")

</div>

What's wrong with inject? I find this very readable and not any more  
verbose than the Haskell:

irb(main):001:0\> a=[1,2,3,4,5,6,7]  
=\> [1, 2, 3, 4, 5, 6, 7]  
irb(main):002:0\> a.inject(0){|i,sum| i+sum}  
=\> 28  
irb(main):003:0\> 1+2+3+4+5+6+7  
=\> 28  
irb(main):004:0\> module Enumerable  
irb(main):005:1\> def list\_sum  
irb(main):006:2\> self.inject(0){|i,sum| i+sum}  
irb(main):007:2\> end  
irb(main):008:1\> end  
=\> nil  
irb(main):009:0\> a.list\_sum  
=\> 28

Ryan

> **···**
>
> On 10/19/05, Andreas Semt \<Andreas.Semt@gmx.net\> wrote:
> 
> > Hello list,
> > 
> > here a really simple Haskell function, which sums all elements of a list  
> > (from the book "The Haskell School of Expression"):  
> > --------------------------------  
> > listSum = 0  
> > listSum (x : xs) = x + listSum xs  
> > --------------------------------
> > 
> > Which is the best way to say it in Ruby (functional style)? Don't use  
> > 'enum.inject', please!

---

<div class="post-metadata">

**Author:** ![Edward\_Faulkner](https://avatars.discourse-cdn.com/v4/letter/e/e9bcb4/32.png) [@Edward\_Faulkner](https://rubytalk.org/u/Edward_Faulkner)\
**Post date:** [19 October 2005 19:00 UTC](https://rubytalk.org/t/functional-with-ruby/21711/4 "2005-10-19T19:00:39Z")

</div>

But inject \*is\* functional! It's equivalent to Lisp's accumulate. It  
doesn't modify the list at all or have any side-effects.

Perhaps you're asking for a function that takes the list as an  
argument, rather than a method that operates on the lisp. For  
example:

def listsum(x)  
&nbsp;&nbsp;x.size == 0 ? 0 : x[0] + listsum(x[1..-1])  
end

This looks more like the Haskell, but it's not idiomatic for Ruby. It  
only looks like a standalone function, it's really a method on Object.  
Ruby can be used this way, but it's going against the grain.

The Ruby way is definitely x.inject {|a,b| a + b}, or if you'll be  
using it a lot:

class Array  
&nbsp;&nbsp;def sum  
&nbsp;&nbsp;&nbsp;&nbsp;inject {|a,b| a+b}  
&nbsp;&nbsp;end  
end

regards,  
Ed

> **···**
>
> On Thu, Oct 20, 2005 at 03:41:09AM +0900, Andreas Semt wrote:
> 
> > here a really simple Haskell function, which sums all elements of a list  
> > (from the book "The Haskell School of Expression"):  
> > --------------------------------  
> > listSum = 0  
> > listSum (x : xs) = x + listSum xs  
> > --------------------------------
> > 
> > Which is the best way to say it in Ruby (functional style)? Don't use  
> > 'enum.inject', please!

---

<div class="post-metadata">

**Author:** ![Florian\_Frank2](https://avatars.discourse-cdn.com/v4/letter/f/a587f6/32.png) [@Florian\_Frank2](https://rubytalk.org/u/Florian_Frank2)\
**Post date:** [19 October 2005 19:06 UTC](https://rubytalk.org/t/functional-with-ruby/21711/5 "2005-10-19T19:06:32Z")

</div>

Andreas Semt wrote:

> here a really simple Haskell function, which sums all elements of a list  
> (from the book "The Haskell School of Expression"):  
> --------------------------------  
> listSum = 0  
> listSum (x : xs) = x + listSum xs  
> --------------------------------
> 
> Which is the best way to say it in Ruby (functional style)? Don't use  
> 'enum.inject', please!

You can abuse Ruby arrays for lists:

def list\_sum(l)  
&nbsp;&nbsp;case l  
&nbsp;&nbsp;when then 0  
&nbsp;&nbsp;else l.first + list\_sum(l[1, l.size])  
&nbsp;&nbsp;end  
end  
list\_sum([1,2,3]) # =\> 6

But using Array#slice for getting the "tail" of the "list" could be inefficient.

You could also use my lazylist gem and the do this:

require 'lazylist'  
def list\_sum(l)  
&nbsp;&nbsp;case l  
&nbsp;&nbsp;when LazyList::Empty then 0  
&nbsp;&nbsp;else l.head + list\_sum(l.tail)  
&nbsp;&nbsp;end  
end  
list\_sum list(1,2,3)

That way you can also create an infinite ones lazy list in Ruby with this nice notation:  
ones = list(1) { ones } # =\> [1,...]

> **···**
>
> --  
> Florian Frank

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [19 October 2005 19:12 UTC](https://rubytalk.org/t/functional-with-ruby/21711/6 "2005-10-19T19:12:08Z")

</div>

harp:~ \> cat a.rb  
list\_sum = lambda{|\*list| list.flatten!; list.empty? ? 0 : list.shift + list\_sum[list] }

p list\_sum[0b101000, 0b10]

harp:~ \> ruby a.rb  
42

-a

> **···**
>
> On Thu, 20 Oct 2005, Andreas Semt wrote:
> 
> > Hello list,
> > 
> > here a really simple Haskell function, which sums all elements of a list  
> > (from the book "The Haskell School of Expression"):  
> > --------------------------------  
> > listSum = 0  
> > listSum (x : xs) = x + listSum xs  
> > --------------------------------
> > 
> > Which is the best way to say it in Ruby (functional style)? Don't use  
> > 'enum.inject', please!
> 
> # --
> 
> > email :: ara [dot] t [dot] howard [at] noaa [dot] gov  
> > phone :: 303.497.6469  
> > anything that contradicts experience and logic should be abandoned.  
> > -- h.h. the 14th dalai lama
> 
> ===============================================================================

---

<div class="post-metadata">

**Author:** ![John\_Carter](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@John\_Carter](https://rubytalk.org/u/John_Carter)\
**Post date:** [19 October 2005 21:07 UTC](https://rubytalk.org/t/functional-with-ruby/21711/7 "2005-10-19T21:07:19Z")

</div>

I'm not going to answer your concrete question. I'm going to answer the question you should be asking....

The answer is with the exception of the the non '!' string methods and numbers most of ruby operations are non-"functional pure" in the sense that they have side effects and change state.

Thus to recapture "functional" programming purity in Ruby, for most objects and operations on them requires you to make very heavy use of the clone method. ie. Force yourself to go obj.clone.operation to make ruby into something like the "Self" language.

Certainly in some contexts (eg. when you can prove there are no other references to the object in question) a clone + modify is equivalent to just a modify. Thus you can make an optimization in such cases, but you will receive very little support from the language in proving this.

It doesn't even have C++'s "const" keyword. Although it does have "freeze", which unfortunately only works at run time after the object has been frozen, which tends to be a bit late.

On the other hand ruby is a "functional" language in the sense that procs, blocks and methods, etc are all first order objects and you can curry dice and slice them. It even has several flavours of eval.

John Carter Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : john.carter@tait.co.nz  
New Zealand

Carter's Clarification of Murphy's Law.

"Things only ever go right so that they may go more spectacularly wrong later."

From this principle, all of life and physics may be deduced.

> **···**
>
> On Thu, 20 Oct 2005, Andreas Semt wrote:
> 
> > Which is the best way to say it in Ruby (functional style)?

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [21 October 2005 00:51 UTC](https://rubytalk.org/t/functional-with-ruby/21711/8 "2005-10-21T00:51:59Z")

</div>

Andreas Semt wrote:

> Hello list,
> 
> here a really simple Haskell function, which sums all elements of a list  
> (from the book "The Haskell School of Expression"):  
> --------------------------------  
> listSum = 0  
> listSum (x : xs) = x + listSum xs  
> --------------------------------
> 
> Which is the best way to say it in Ruby (functional style)? Don't use  
> 'enum.inject', please!
> 
> Thanks for your thoughts.  
> Greetings,  
> Andreas Semt

irb(main):011:0\> def sum(a,i)  
irb(main):012:1\> i\<a.size ? a[i] + sum(a,i.succ) : 0  
irb(main):013:1\> end  
=\> nil  
irb(main):014:0\> sum([1,3,5,7,9], 0)  
=\> 25

---

<div class="post-metadata">

**Author:** ![Andreas\_Semt](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@Andreas\_Semt](https://rubytalk.org/u/Andreas_Semt)\
**Post date:** [22 October 2005 11:16 UTC](https://rubytalk.org/t/functional-with-ruby/21711/9 "2005-10-22T11:16:53Z")

</div>

Hello list,

so many thanks for all your answers!  
My original interest in this question was learning functional  
programming, with both languages.  
Ruby is my language of choice, so i try to compare both languages to  
find out how much functional ruby can be. All the proposals are really  
helpfull, both for understandig functional programming better \*and\* for  
understanding the principles of ruby.

&nbsp;&nbsp;This list is just great ...

Best regards,  
Andreas Semt

---

<div class="post-metadata">

**Author:** ![Ryan\_Leavengood](https://avatars.discourse-cdn.com/v4/letter/r/a587f6/32.png) [@Ryan\_Leavengood](https://rubytalk.org/u/Ryan_Leavengood)\
**Post date:** [19 October 2005 19:08 UTC](https://rubytalk.org/t/functional-with-ruby/21711/10 "2005-10-19T19:08:01Z")

</div>

One small thing, I had the order wrong on the parameters (i.e. the  
names are misleading.) It should be:

a.inject(0){|sum,i| sum+i}

It still works either way in this case. But other kinds of injects may  
not be commutative, so I just wanted to make sure I didn't mess up any  
newbies 🙂

ri is your friend.

Ryan

> **···**
>
> On 10/19/05, Ryan Leavengood \<leavengood@gmail.com\> wrote:
> 
> > irb(main):002:0\> a.inject(0){|i,sum| i+sum}

---

<div class="post-metadata">

**Author:** ![Lou\_Scoras](https://avatars.discourse-cdn.com/v4/letter/l/ee59a6/32.png) [@Lou\_Scoras](https://rubytalk.org/u/Lou_Scoras)\
**Post date:** [19 October 2005 19:22 UTC](https://rubytalk.org/t/functional-with-ruby/21711/11 "2005-10-19T19:22:48Z")

</div>

> But inject \*is\* functional! It's equivalent to Lisp's accumulate. It  
> doesn't modify the list at all or have any side-effects.

Exactly.

Incidentally, the original example isn't really the best way to do this in  
Haskell either. I haven't written any Haskell for a while, so some of this  
might not be syntactically correct, but you get the idea. Please correct me  
if any of this is wrong.

The above would best be expressed something like this (Iike I said, it's  
been a while, so the parameter order might be wrong):

foldr + 0 list

foldr does exactly the same thing as inject. It takes a binary function, a  
starting value, and a list. The neat thing about Haskell is that the  
addition operator--actually any function--is automatically curried.

I'd make the claim here that the use of the higher order function is  
actually better functional style than the explicit recursion.

If I recall correctly, many of the list functions in the Hugs standard  
library are actually implemented this manner.

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [19 October 2005 20:29 UTC](https://rubytalk.org/t/functional-with-ruby/21711/12 "2005-10-19T20:29:03Z")

</div>

That can get a bit shorter too, since you don't need the (0) part.

James Edward Gray II

> **···**
>
> On Oct 19, 2005, at 1:59 PM, Ryan Leavengood wrote:
> 
> > What's wrong with inject? I find this very readable and not any more  
> > verbose than the Haskell:
> > 
> > irb(main):001:0\> a=[1,2,3,4,5,6,7]  
> > =\> [1, 2, 3, 4, 5, 6, 7]  
> > irb(main):002:0\> a.inject(0){|i,sum| i+sum}  
> > =\> 28

---

<div class="post-metadata">

**Author:** ![Dave\_Burt2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/dave_burt2/32/2036_2.png) [@Dave\_Burt2](https://rubytalk.org/u/Dave_Burt2)\
**Post date:** [20 October 2005 02:26 UTC](https://rubytalk.org/t/functional-with-ruby/21711/13 "2005-10-20T02:26:59Z")

</div>

"Ara.T.Howard" \<Ara.T.Howard@noaa.gov\> wrote in message  
news:Pine.LNX.4.62.0510191259580.28380@harp.ngdc.noaa.gov...

> **···**
>
> > On Thu, 20 Oct 2005, Andreas Semt wrote:
> > 
> > > Hello list,
> > > 
> > > here a really simple Haskell function, which sums all elements of a list  
> > > (from the book "The Haskell School of Expression"):  
> > > --------------------------------  
> > > listSum = 0  
> > > listSum (x : xs) = x + listSum xs  
> > > --------------------------------
> > > 
> > > Which is the best way to say it in Ruby (functional style)? Don't use  
> > > 'enum.inject', please!
> > 
> > harp:~ \> cat a.rb  
> > list\_sum = lambda{|\*list| list.flatten!; list.empty? ? 0 : list.shift +  
> > list\_sum[list] }
> > 
> > p list\_sum[0b101000, 0b10]
> > 
> > harp:~ \> ruby a.rb  
> > 42
> 
> OK, this is the first actually functional post -- list\_sum is a first-class  
> function.
> 
> Here's my translation:
> 
> list\_sum = Proc.new {|x, \*xs| if x then x + list\_sum[\*xs] else 0 end }
> 
> Cheers,  
> Dave

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [21 October 2005 02:26 UTC](https://rubytalk.org/t/functional-with-ruby/21711/14 "2005-10-21T02:26:53Z")

</div>

Hi --

> Andreas Semt wrote:
> 
> > here a really simple Haskell function, which sums all elements of a list  
> > (from the book "The Haskell School of Expression"):  
> > --------------------------------  
> > listSum = 0  
> > listSum (x : xs) = x + listSum xs  
> > --------------------------------
> > 
> > Which is the best way to say it in Ruby (functional style)? Don't use  
> > 'enum.inject', please!
> 
> You can abuse Ruby arrays for lists:
> 
> def list\_sum(l)  
> case l  
> when then 0  
> else l.first + list\_sum(l[1, l.size])  
> end  
> list\_sum([1,2,3]) # =\> 6

Just for fun I was trying to see how visually close I could get to the  
Haskell. The answer is... not very 🙂 But the star operator  
actually handles the x : xs list thing rather well:

&nbsp;&nbsp;&nbsp;def list\_sum(x,\*xs)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case xs  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when : x # note: not 0 when done this way  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else x + list\_sum(\*xs)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

David

> **···**
>
> On Thu, 20 Oct 2005, Florian Frank wrote:
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [19 October 2005 19:34 UTC](https://rubytalk.org/t/functional-with-ruby/21711/15 "2005-10-19T19:34:49Z")

</div>

i don't think it is functional because the name of the function cannot be used  
as a first class object. eg you cannot to

&nbsp;&nbsp;&nbsp;the\_function = the\_other\_function

without calling 'the\_other\_function' \_\_unless\_\_ the objects in question are  
first class objects. in ruby you must use lambda to acheive something close  
to this. at least i think this right...

-a

> **···**
>
> On Thu, 20 Oct 2005, Louis J Scoras wrote:
> 
> > > But inject \*is\* functional! It's equivalent to Lisp's accumulate. It  
> > > doesn't modify the list at all or have any side-effects.
> > 
> > Exactly.
> 
> # --
> 
> > email :: ara [dot] t [dot] howard [at] noaa [dot] gov  
> > phone :: 303.497.6469  
> > anything that contradicts experience and logic should be abandoned.  
> > -- h.h. the 14th dalai lama
> 
> ===============================================================================

---

<div class="post-metadata">

**Author:** ![Florian\_Frank2](https://avatars.discourse-cdn.com/v4/letter/f/a587f6/32.png) [@Florian\_Frank2](https://rubytalk.org/u/Florian_Frank2)\
**Post date:** [19 October 2005 19:35 UTC](https://rubytalk.org/t/functional-with-ruby/21711/16 "2005-10-19T19:35:52Z")

</div>

Louis J Scoras wrote:

> The above would best be expressed something like this (Iike I said, it's  
> been a while, so the parameter order might be wrong):
> 
> foldr + 0 list

Well, foldr is defined recursively in Prelude.hs:

foldr :: (a -\> b -\> b) -\> b -\> [a] -\> b  
foldr f z = z  
foldr f z (x:xs) = f x (foldr f z xs)

BTW, it's

foldr (+) 0 list

> **···**
>
> --  
> Florian Frank

---

<div class="post-metadata">

**Author:** ![Florian\_Frank2](https://avatars.discourse-cdn.com/v4/letter/f/a587f6/32.png) [@Florian\_Frank2](https://rubytalk.org/u/Florian_Frank2)\
**Post date:** [19 October 2005 20:37 UTC](https://rubytalk.org/t/functional-with-ruby/21711/17 "2005-10-19T20:37:46Z")

</div>

James Edward Gray II wrote:

> That can get a bit shorter too, since you don't need the (0) part.

Yeah. And I always hated it, that your program is going to crash, if "a" happens to be empty and you naively  
use the inject result for further processing.

> **···**
>
> --  
> Florian Frank

---

<div class="post-metadata">

**Author:** ![Ryan\_Leavengood](https://avatars.discourse-cdn.com/v4/letter/r/a587f6/32.png) [@Ryan\_Leavengood](https://rubytalk.org/u/Ryan_Leavengood)\
**Post date:** [19 October 2005 20:38 UTC](https://rubytalk.org/t/functional-with-ruby/21711/18 "2005-10-19T20:38:24Z")

</div>

Yeah I figured that out after posting, hehe (at the same time ri told  
me my parameter ordering was wrong.) Inject is such a cool method.

Ryan

> **···**
>
> On 10/19/05, James Edward Gray II \<james@grayproductions.net\> wrote:
> 
> > That can get a bit shorter too, since you don't need the (0) part.

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [20 October 2005 04:01 UTC](https://rubytalk.org/t/functional-with-ruby/21711/19 "2005-10-20T04:01:58Z")

</div>

nice. still the functional vs. non is a very thin line in ruby:

&nbsp;&nbsp;&nbsp;harp:~ \> cat a.rb  
&nbsp;&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def list\_sum x = nil, \*xs  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x ? x + list\_sum(\*xs) : 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;list\_sum = method 'list\_sum'

&nbsp;&nbsp;&nbsp;p list\_sum[0b101000, 0b10]  
&nbsp;&nbsp;&nbsp;p list\_sum[42]  
&nbsp;&nbsp;&nbsp;p list\_sum

&nbsp;&nbsp;&nbsp;harp:~ \> ruby a.rb  
&nbsp;&nbsp;&nbsp;42  
&nbsp;&nbsp;&nbsp;0

ps. i'm trying to wrap my mind around ocaml attm.

regards.

-a

> **···**
>
> On Thu, 20 Oct 2005, Dave Burt wrote:
> 
> > "Ara.T.Howard" \<Ara.T.Howard@noaa.gov\> wrote in message  
> > news:Pine.LNX.4.62.0510191259580.28380@harp.ngdc.noaa.gov...
> > 
> > > On Thu, 20 Oct 2005, Andreas Semt wrote:
> > > 
> > > > Hello list,
> > > > 
> > > > here a really simple Haskell function, which sums all elements of a list  
> > > > (from the book "The Haskell School of Expression"):  
> > > > --------------------------------  
> > > > listSum = 0  
> > > > listSum (x : xs) = x + listSum xs  
> > > > --------------------------------
> > > > 
> > > > Which is the best way to say it in Ruby (functional style)? Don't use  
> > > > 'enum.inject', please!
> > > 
> > > harp:~ \> cat a.rb  
> > > list\_sum = lambda{|\*list| list.flatten!; list.empty? ? 0 : list.shift +  
> > > list\_sum[list] }
> > > 
> > > p list\_sum[0b101000, 0b10]
> > > 
> > > harp:~ \> ruby a.rb  
> > > 42
> > 
> > OK, this is the first actually functional post -- list\_sum is a first-class  
> > function.
> > 
> > Here's my translation:
> > 
> > list\_sum = Proc.new {|x, \*xs| if x then x + list\_sum[\*xs] else 0 end }
> 
> # --
> 
> > email :: ara [dot] t [dot] howard [at] noaa [dot] gov  
> > phone :: 303.497.6469  
> > anything that contradicts experience and logic should be abandoned.  
> > -- h.h. the 14th dalai lama
> 
> ===============================================================================

---

<div class="post-metadata">

**Author:** ![Edward\_Faulkner](https://avatars.discourse-cdn.com/v4/letter/e/e9bcb4/32.png) [@Edward\_Faulkner](https://rubytalk.org/u/Edward_Faulkner)\
**Post date:** [19 October 2005 19:51 UTC](https://rubytalk.org/t/functional-with-ruby/21711/20 "2005-10-19T19:51:19Z")

</div>

Well, that's true of Ruby in general. If you want to turn a method  
name into a first class object, you can do this:

x = myarray.instance\_method(:inject)  
x.call {|a,b| a+b}

Or this:

x = Array.instance\_method(:inject)  
x.bind(myarray).call {|a,b| a+b}

regards,  
Ed

> **···**
>
> On Thu, Oct 20, 2005 at 04:34:49AM +0900, Ara.T.Howard wrote:
> 
> > i don't think it is functional because the name of the function  
> > cannot be used as a first class object.

[Next page](https://rubytalk.org/t/functional-with-ruby/21711.md?page=2)
