TechHui

Hawaiʻi's Technology Community

It wasn't so long ago that functional programming seemed to be an arcane science. If you knew Haskell, Standard ML, or Common Lisp, you've either been programming for a very long time, involved in computer science research, or just had a lot of free time on your hands. However, functional programming has been making a comeback with languages like Erlang being used more in production settings. In addition, newer languages such as ScalaClojure, and F# build on functional programming principles from previous languages. Still, their applications are either highly specialized or can be accomplished with more mainstream languages such as JavaPython, and C#. So, why should we even bother learning functional programming?

1. It's a completely different way of programming.

A language that doesn't affect the way you think about programming, is not worth knowing. - Alan Perlis

If you haven't done much functional programming before, it feels very restrictive. In most of them, you don't have loops or classes. In some cases, you can't mutate variables (once they're set, you can't change them). Doing something as simple as "fizzbuzz" can be very difficult and unintuitive. Students today start with Java and, when introduced to C, have difficulty grasping the idea of pointers. Going from a object oriented language to functional programming can be that kind of jump.

But stick with it and it'll start making more sense. Eventually, you'll find that functional programming takes some different approaches that, in many ways, can be even simpler and faster than their object oriented counterparts. Concepts such as currying and pattern matching provide new ways to attack old problems. Some concepts that you might've forgotten or maybe never learned like recursion come back in a big way. In fact, if you don't have a firm grasp of recursion, then learn a functional programming language. Without iteration and loops, you'll have to depend on it.

2. It's making a comeback.

Despite the relatively small number of programmers that use functional languages, old and new functional languages are making a resurgence. Erlang, a language developed at Ericcson, is designed to handle distributed and concurrent workloads. Facebook chose to use Erlang to implement their messaging systems. Opscode also used Erlang to rewrite Chef, a tool for automating infrastructure, which is used to support large scale systems like Facebook's. F#, developed at Microsoft Research, is being pitched as a language that helps programmers analyze and visualize big data.

3. Concepts from functional programming are being integrated into current languages.

Scripting languages such as Ruby and Python integrate many concepts from functional languages, including lambda expressions, functions as first class objects, list comprehensions, and more. It's all but impossible nowadays to write Javascript without anonymous functions in callbacks. One of the big features in the next release of Java are closures, which essentially allows functions to refer to variables in the environment they were defined in. And for developers who have looked into functional programming, writing functional-style code for non-functional languages minimizes side effects and make it easier to write unit tests for.

4. Lots of resources

As I had described in my previous blog on learning something new, there are many ways to learn about functional programming. In the past year, Coursera offered two classes that involved functional programming (Functional Programming Principles in Scala and Programming Languages). In addition to online resources, you can seek out local user groups. In Honolulu, we have the OUDL, which meets a few times every month. The topics go beyond just programming languages and is a good chance to see what the tech community in Honolulu is like.

Functional programming used to be extremely difficult to get into, but now it's so much easier. Stick with it and you'll get the most out of the code you write, whether it's a functional language or not.

Views: 260

Comment

You need to be a member of TechHui to add comments!

Join TechHui

Comment by George Lee on March 26, 2013 at 8:45am

I do utilize operations over collections (like maps and list comprehensions) more often, but like Kyle mentioned I'm more inclined to isolate mutable state as much as possible. I have looked at Python code and thought, "Man, I wish I had Erlang/ML pattern matching" at least once.

Oddly enough, learning about functional languages really helped me understand the more advanced concepts in Javascript (in particular, closures). I've been reading Secrets of the Javascript Ninja by John Resig and I've been amazed by some of the ways he applies closures. My favorite example is this implementation of method overloading for an object.

Comment by Kyle Oba on March 25, 2013 at 11:21pm

Thanks for the OUDL shout-out.  I should say that if folks have an interest in more functional programming topics they should chime in on the meetup page.  There was also a Functional Javascript with Underscore book released recently by Fogus (prominent Clojure programmer).  And... there's a small band of Clojure programmers initiating a local Clojure ninja-hacker-team.

Apropos of Dan's question... I find myself writing more "maps" in Ruby, and other ways of operating over collections of data (in a declarative manner).  Also, my programs end up with many more static methods, so as to avoid unnecessary mutable state.

Comment by Daniel Leuck on March 25, 2013 at 2:46pm

Great post George. Do you find that learning functional programming with languages like Erlang has affected the way you code in mainstream OO languages such as Python and Ruby? Are you now making heavier use of recursion, lambdas, etc.?

Comment by Joseph Lui on March 22, 2013 at 1:33pm

Nice! Is the opposite of "functional programming", "dysfunctional programming"?

Sponsors

web design, web development, localization

© 2024   Created by Daniel Leuck.   Powered by

Badges  |  Report an Issue  |  Terms of Service