# Functional programming in Ruby (Was: Reducing Enumerable (and writing again))

**URL:** <https://rubytalk.org/t/functional-programming-in-ruby-was-reducing-enumerable-and-writing-again/74317>\
**Category:** ruby-talk\
**Created:** [2 November 2017 13:54 UTC](https://rubytalk.org/t/functional-programming-in-ruby-was-reducing-enumerable-and-writing-again/74317 "2017-11-02T13:54:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andy\_Jones](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/andy_jones/32/1829_2.png) [@Andy\_Jones](https://rubytalk.org/u/Andy_Jones)\
**Post date:** [2 November 2017 13:54 UTC](https://rubytalk.org/t/functional-programming-in-ruby-was-reducing-enumerable-and-writing-again/74317/1 "2017-11-02T13:54:03Z")

</div>

> Have you used anything like  
> [GitHub - github/github-ds: A collection of Ruby libraries for working with SQL on top of ActiveRecord's connection](https://github.com/github/github-ds#githubresult) for error handling? I'm wondering if that's a useful next step (after introducing some immutability and attempting to isolate side effects) that could be a gateway to other functional techniques. I've used it in a codebase and I  
> \*think\* it was an improvement.

No, I've not tried it. I see the appeal from a functional perspective, but a part of me is not really buying it. Nothing is stopping you from chaining methods in Ruby without it, apart from the fact that a method could raise an exception, and if a method raises an exception then presumably that's for a good reason?

Instead of:

&nbsp;&nbsp;&nbsp;&nbsp;Result.new(foo).then Result.new(bar)

Why not just:

&nbsp;&nbsp;&nbsp;&nbsp;begin; bar( foo )  
&nbsp;&nbsp;&nbsp;&nbsp;rescue; .....  
&nbsp;&nbsp;&nbsp;&nbsp;end

It seems like what Result is mostly bringing to the table is Syntactic sugar, and I \_like\_ Ruby syntax, so...

Click here to view Company Information and Confidentiality Notice.\<[http://www.jameshall.co.uk/index.php/small-print/email-disclaimer&gt](http://www.jameshall.co.uk/index.php/small-print/email-disclaimer&gt);

---

<div class="post-metadata">

**Author:** ![Tom\_Copeland1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/tom_copeland1/32/1843_2.png) [@Tom\_Copeland1](https://rubytalk.org/u/Tom_Copeland1)\
**Post date:** [2 November 2017 14:09 UTC](https://rubytalk.org/t/functional-programming-in-ruby-was-reducing-enumerable-and-writing-again/74317/2 "2017-11-02T14:09:10Z")

</div>

I hear you. I'm not entirely sold on it yet either... but I feel like it wraps things up a bit more nicely. I'm not sure I can articulate why though.

One thing I saw on the internet somewhere that was helpful for me - someone said "if you're programming in a functional style your methods will look more like expressions". So instead of:

def foo  
x = get\_something  
x.twiddle!  
x  
end

you might do:

def foo  
get\_something.tap {|x| x.twiddle! }  
end

For some reason that kind of struck a chord with me.

> **···**
>
> On 11/2/17 9:54 AM, Andy Jones wrote:
> 
> > No, I've not tried it. I see the appeal from a functional perspective, but a part of me is not really buying it. Nothing is stopping you from chaining methods in Ruby without it, apart from the fact that a method could raise an exception, and if a method raises an exception then presumably that's for a good reason?  
> > Instead of:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Result.new(foo).then Result.new(bar)
> > 
> > Why not just:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin; bar( foo )  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue; .....  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > It seems like what Result is mostly bringing to the table is Syntactic sugar, and I \_like\_ Ruby syntax, so...

---

<div class="post-metadata">

**Author:** ![Kamal\_Hossain](https://avatars.discourse-cdn.com/v4/letter/k/b782af/32.png) [@Kamal\_Hossain](https://rubytalk.org/u/Kamal_Hossain)\
**Post date:** [2 November 2017 14:17 UTC](https://rubytalk.org/t/functional-programming-in-ruby-was-reducing-enumerable-and-writing-again/74317/3 "2017-11-02T14:17:16Z")

</div>

Thank you.

Get Outlook for Android\<[Bing](https://aka.ms/ghei36&gt);

> **···**
>
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> From: ruby-talk \<ruby-talk-bounces@ruby-lang.org\> on behalf of Tom Copeland \<tom@thomasleecopeland.com\>  
> Sent: Thursday, November 2, 2017 8:09:10 PM  
> To: Ruby users  
> Subject: Re: Functional programming in Ruby (Was: Reducing Enumerable (and writing again))
> 
> On 11/2/17 9:54 AM, Andy Jones wrote:
> 
> > No, I've not tried it. I see the appeal from a functional perspective,  
> > but a part of me is not really buying it. Nothing is stopping you from  
> > chaining methods in Ruby without it, apart from the fact that a method  
> > could raise an exception, and if a method raises an exception then  
> > presumably that's for a good reason?  
> > Instead of:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Result.new(foo).then Result.new(bar)
> > 
> > Why not just:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin; bar( foo )  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue; .....  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > It seems like what Result is mostly bringing to the table is Syntactic sugar, and I \_like\_ Ruby syntax, so...
> 
> I hear you. I'm not entirely sold on it yet either... but I feel like  
> it wraps things up a bit more nicely. I'm not sure I can articulate why  
> though.
> 
> One thing I saw on the internet somewhere that was helpful for me -  
> someone said "if you're programming in a functional style your methods  
> will look more like expressions". So instead of:
> 
> def foo  
> &nbsp;&nbsp;&nbsp;x = get\_something  
> &nbsp;&nbsp;&nbsp;x.twiddle!  
> &nbsp;&nbsp;&nbsp;x  
> end
> 
> you might do:
> 
> def foo  
> &nbsp;&nbsp;&nbsp;get\_something.tap {|x| x.twiddle! }  
> end
> 
> For some reason that kind of struck a chord with me.
> 
> Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> \<[https://nam02.safelinks.protection.outlook.com/?url=http%3A%2F%2Flists.ruby-lang.org%2Fcgi-bin%2Fmailman%2Foptions%2Fruby-talk&data=02|01|ac.kamal%40hotmail.com|9c3fe7cc2bb042b2ac5508d521fb5092|84df9e7fe9f640afb435aaaaaaaaaaaa|1|0|636452285620582841&sdata=3B%2Ftn8EGHWeT6DAlOBju4%2FObiuX9FCcrm5gjhrg9tOs%3D&reserved=0&gt](https://nam02.safelinks.protection.outlook.com/?url=http%3A%2F%2Flists.ruby-lang.org%2Fcgi-bin%2Fmailman%2Foptions%2Fruby-talk&data=02%7C01%7Cac.kamal%40hotmail.com%7C9c3fe7cc2bb042b2ac5508d521fb5092%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636452285620582841&sdata=3B%2Ftn8EGHWeT6DAlOBju4%2FObiuX9FCcrm5gjhrg9tOs%3D&reserved=0&gt);
