def things_n_stuff

Adventures in Code

(Hints of) Functional Programming in Ruby

Cursory yields little

I keep hearing about functional programming. I had a vague notion of what that entails and a bit of curiosity, so a wonderful TA at the Flatiron School suggested a speakerdeck presentation for me to check out. The results are fairly mixed.

As a burgeoning Ruby developer I pick up hints of the ongoing larger conceptual war for programming language/paradigm supremacy, but all I know so far are the lovely objects of Ruby and the messy, almost-consist forrest of special characters of a few other languages. Just a “barbarian’s” perspective I’m sure, but still.

So when I picked up Pat Shaghnessey’s slides on Functional Programming in Ruby on speaker deck, I was hoping for a bit of elightenment about the advantages of mixing paradigms. All I really knew about functional prior was that functional programming treats actions like mathematical functions. And as I see from Shaghnessey’s slides that is the case, and one of it’s primary differences from how I’ve thought of Ruby-like object-oriented programming. Functional takes input, runs it through the function magic, and consistently spits out the transformed input on the other side. Unlike object oriented languages in general, functional focuses on being state-independent. So a function always has the same effect on input, with very few side effects or variations based on other structures.metaphysics

The presentation itself was high on the “Gee that seems cool” factor, but still left me mostly in the dark and googling for answers. I’ve always liked working through Rube-goldberg style function puzzles and tracking input and output so following the actual functions was easy even when I couldn’t parse the haskel. Also yeah, haskel seems cool to write but awful to read.

Things got more interesting at the midpoint. Seeing beneath the hood on the collect method is fun, and getting the code for lazy enumeration is pretty cool. I wish programming books in general would speak more about the internal workings of object as they describe the functionality, lengthy as that would be. I’m definitely going to have to read this again after a few more weeks of programming… Structure of Lazy Enumerator

I was also reminded that I really need to dig deeper and learn about the Enumerable module and the Enumerator class.

Haskel itself looks a bit crazy. The contrast of Haskell’s vs Ruby’s handling of infinity was nifty and mysterious.
What's going onWhat's going on

But by the end of the presentation I couldn’t tell anymore if Shaughnessy was a proponent of using function programming within Ruby, or if he was showing how much ‘better’ Haskell is at certain tasks and how cool he is for using it. Both could be the case. Obviously the presentation is missing speach, context, and the oral component, but my takeaway is that it would appear that using aspects of functional within Ruby code holds much promise for optimization but limitations. Hmm. I think I could’ve guessed that before. I have few additional details than I had before.