Reducing Enumerable (and writing again)

Been a while, but I've started writing a lot of Ruby tutorials and articles
again. You all may find a few of these interesting.

The two ones I currently have running are:

*Reducing Enumerable*

Reimplementing Enumerable in terms of Reduce.

   1.
   https://medium.com/@baweaver/reducing-enumerable-the-basics-fa042ce6806
   2.
   https://medium.com/@baweaver/reducing-enumerable-no-op-and-boolean-b2ce4ef8bd79
   3.
   https://medium.com/@baweaver/reducing-enumerable-no-op-and-boolean-b2ce4ef8bd79

*Ruby Black Magic*

Some of the more fun and interesting corners of Ruby, some of which should
probably get my Ruby card revoked.

   1. https://medium.com/rubyinside/triple-equals-black-magic-d934936a6379
   2. https://medium.com/@baweaver/abusing-hash-constructors-65d59c0a5b27

···

---

Anything else people are particularly keen on seeing? I intend to take a
good dive into Ramda in Ruby soon and get a few more parts out for Reducing
Enumerable.

That's interesting stuff, thank you! I hadn't thought about those aspects of reduce.

I'd be really interested in reading anything around functional programming + Ruby. After having made a mess of a number of codebases while trying to do classic OOAD any other methodology gets my attention...

···

On 10/17/17 3:03 AM, Brandon Weaver wrote:

Been a while, but I've started writing a lot of Ruby tutorials and articles again. You all may find a few of these interesting.

The two ones I currently have running are:

*Reducing Enumerable*
*
Reimplementing Enumerable in terms of Reduce.

1. Reducing Enumerable — The Basics. One of the lesser understood functions… | by Brandon Weaver | Medium
2. Reducing Enumerable — No-Op and Boolean | by Brandon Weaver | Medium
3. Reducing Enumerable — No-Op and Boolean | by Brandon Weaver | Medium

*Ruby Black Magic*
*
Some of the more fun and interesting corners of Ruby, some of which should probably get my Ruby card revoked.

1. Triple Equals Black Magic. For the most part, === is either… | by Brandon Weaver | Ruby Inside | Medium
2. Abusing Hash Constructors. Hash constructors are one of those… | by Brandon Weaver | Medium

---

Anything else people are particularly keen on seeing? I intend to take a good dive into Ramda in Ruby soon and get a few more parts out for Reducing Enumerable.

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Why not just test-drive a functional language?

Learning Elixir/Erlang certainly changed my Ruby coding style...

···

On Tue, Oct 31, 2017 at 6:48 AM, Tom Copeland <tom@thomasleecopeland.com> wrote:

I'd be really interested in reading anything around functional programming +
Ruby.

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan
Consulting Availability : Silicon Valley or remote

True, I should do that too.

···

On 10/31/17 3:50 PM, Hassan Schroeder wrote:

On Tue, Oct 31, 2017 at 6:48 AM, Tom Copeland <tom@thomasleecopeland.com> wrote:

I'd be really interested in reading anything around functional programming +
Ruby.

Why not just test-drive a functional language?

Learning Elixir/Erlang certainly changed my Ruby coding style...

Learning Elixir/Erlang certainly changed my Ruby coding style...

True, I should do that too.

I'm you are finding the learning curve a bit steep, try Elm. Then again, most Rubyists seem to say that Elixir is an easy transition.

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

I think mainly I'm trying to figure out how to make my Ruby code better using functional techniques (immutability and such) without necessarily getting into cofree comonads and whatnot. Unless cofree comonads are the way to go, in which case, hey hey...

···

On 11/1/17 4:30 AM, Andy Jones wrote:

Learning Elixir/Erlang certainly changed my Ruby coding style...

True, I should do that too.

I'm you are finding the learning curve a bit steep, try Elm. Then again, most Rubyists seem to say that Elixir is an easy transition.

I think mainly I'm trying to figure out how to make my Ruby code better using functional techniques (immutability and such) without necessarily
getting into cofree comonads and whatnot. Unless cofree comonads are the way to go, in which case, hey hey...

You won't find any of that jargon in Elm.

I'm in the same boat as you; I want to use Ruby in a more functional way, because it makes the code more reusable, and much easier to test. I'm still playing with ideas. But I have found that dipping my toes in functional languages and then returning has helped that.

* Brief foray into Haskell: exit stage left pursued by an Applicative Functor. (They mostly only come at night. Mostly.)

* Scala, which isn't a pure functional language but has some powerful functional ideas baked in. I liked that a lot.

* Elm, which, well, the basics took me a lunchtime. But web front end only AFAICS; Not terribly useful to me as a language, but it has to be the easiest "in" for basic functional concepts.

* Elixir, briefly, and I should probably find the time to go back. At first glance it looks Just. Like. Ruby. But I've heard people say that after a while you get to see that everything is far more deeply functional, and actually it's nothing like. ("Oh my god, it's full of monads"?)

My current thinking on how to be functional in Ruby can be summed up as: (1) if you can make a method that has no side effects or a class that sets its state on initialization and never changes that state, you should *definitely* do that. (2) Sometimes you can't.

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

Nah, no monads to fear. And it really *is* functional, not OO, so
working with it a while will certainly influence your thinking and
your Ruby coding.

For interesting alternatives, try Clojure or kinda-Haskell-y Idris
(https://www.idris-lang.org/\)

I'm mostly working with Elixir right now, so feel free to ask anything
about it off-list, or jump into the Elixir Slack :slight_smile:

···

On Thu, Nov 2, 2017 at 1:34 AM, Andy Jones <Andy.Jones@jameshall.co.uk> wrote:

* Elixir, briefly, and I should probably find the time to go back. At first glance it looks Just. Like. Ruby. But I've heard people say that after a while you get to see that everything is far more deeply functional, and actually it's nothing like. ("Oh my god, it's full of monads"?)

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan
Consulting Availability : Silicon Valley or remote

Have you used anything like 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.

···

On 11/2/17 4:34 AM, Andy Jones wrote:

My current thinking on how to be functional in Ruby can be summed up as: (1) if you can make a method that has no side effects or a class that sets its state on initialization and never changes that state, you should *definitely* do that. (2) Sometimes you can't.

Yessir. I've tried and failed at that a time or two as well. Maybe if I did "asteroids in haskell" or something; I see there's https://leanpub.com/gameinhaskell but looks like it stalled after the first chapter.

···

On 11/2/17 4:34 AM, Andy Jones wrote:

* Brief foray into Haskell: exit stage left pursued by an Applicative Functor. (They mostly only come at night. Mostly.)

Well this has blossomed a bit. So then, functional programming in Ruby...

The short answer is you can do a lot of it, but in ways it works against the language itself in terms of optimizations. TCO was one of the bigger hits there (though it _is_ a compile flag if you want it.)

As far as how to learn, honestly I'd spend more time in Javascript. Unlike Haskell and some of the others you're almost certain to be using Javascript with Ruby projects and it has a bit less of a ramp up as well. Noted that I know a handful of Haskell, Elixir, and Scala and I do enjoy them but on all practical terms Javascript will suit more developers even being less pure.

I'm going to say something quite controversial: Javascript is good enough to learn Functional Programming ideas.

One of the more interesting ideas to look into there would be Ramda, and this article was amazing at explaining some of it: Thinking in Ramda: Getting Started

Also take a look into:

* Functional Javascript: Functional JavaScript [Book]
* FP in JS: Functional Programming in JavaScript
* Dr Boolean's Guide: https://drboolean.gitbooks.io/mostly-adequate-guide/content/ch1.html

Note though that Ruby has a Ramda implementation as well, and considering they all return Procs which respond to === you can have some real fun with case statements and making query languages. See that Triple Equals Black Magic post above.

Likely I'll do a piece on Ramda for Ruby a bit later, the current focus is on an absolute beginner's illustrated guide to Ruby.

As far as Monads though, they're not as bad as a lot of people make them out to be. It's basically just a container with some extra state tacked onto the end of it for things you can't cleanly reduc.... er... recurse over with an array or straight value. Railway Oriented Programming was an interesting read that will cleanly illustrate a good deal of this:

* Railway Oriented Programming: Railway Oriented Programming | F# for fun and profit
* Monads in Pictures: Functors, Applicatives, And Monads In Pictures - adit.io

Oh, and if you've caught on to my little secret in some of these reducing enumerable posts, shh. :slight_smile:

- baweaver

···

Get Outlook for iOS<Bing;
________________________________
From: ruby-talk <ruby-talk-bounces@ruby-lang.org> on behalf of Hassan Schroeder <hassan.schroeder@gmail.com>
Sent: Thursday, November 2, 2017 7:56:58 AM
To: Ruby users
Subject: Re: Reducing Enumerable (and writing again)

On Thu, Nov 2, 2017 at 1:34 AM, Andy Jones <Andy.Jones@jameshall.co.uk> wrote:

* Elixir, briefly, and I should probably find the time to go back. At first glance it looks Just. Like. Ruby. But I've heard people say that after a while you get to see that everything is far more deeply functional, and actually it's nothing like. ("Oh my god, it's full of monads"?)

Nah, no monads to fear. And it really *is* functional, not OO, so
working with it a while will certainly influence your thinking and
your Ruby coding.

For interesting alternatives, try Clojure or kinda-Haskell-y Idris
(https://www.idris-lang.org/\)

I'm mostly working with Elixir right now, so feel free to ask anything
about it off-list, or jump into the Elixir Slack :slight_smile:

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan
Consulting Availability : Silicon Valley or remote

Yessir. I've tried and failed at that a time or two as well. Maybe if I did "asteroids in haskell" or something; I see there's Game programming in… by Elise Huard [Leanpub PDF/iPad/Kindle] but looks like it stalled after the first chapter.

"Learn You a Haskell For A Great Good" is supposed to be pretty friendly.

But really the functional stuff only started to make sense for me when I tried Scala. I don't know whether that’s because the language is more friendly, or the documentation is; all the stuff I read online on Haskell quickly degraded into Phd category theory.

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

Very nice, thanks for the pointers!

···

On 11/2/17 11:59 AM, Brandon Weaver wrote:

Well this has blossomed a bit. So then, functional programming in Ruby...