background preloader

Functional programming

Facebook Twitter

Carlo Pescio - blog: Cut the red wire! We're all familiar with this scene: the good guys trying to save the day (or the world) by defusing a bomb. Quickly running out of time, they have to cut a wire. Failure is not an option. The red bar won't turn into a green bar. What if you had to write code that works the very first time? Lest you think I'm kinda crazy: I'm not actually suggesting that you shouldn't test your code. Yahtzee [sorry guys, gotta go into Dr. I came to write this post, and to choose Yahtzee as the underlying problem, mostly because I like coincidences :-). A friend of mine, a relatively young programmer in love with everything agile, every once in a while shows me some "code kata" that he has completed.

Still, shortly after, I was visiting the blog of an acquaintance of mine (xpmatteo), and after leaving a comment, I read a few more things including a post about Yahtzee and the removal of conditionals and loops. Before you get any further This is where I should ask you to try it on your own. Yo, I like this. Functional thinking: Thinking functionally, Part 2. In the first installment of this series, I began discussing some of the characteristics of functional programming, showing how those ideas manifest in both Java and more-functional languages.

In this article, I'll continue this tour of concepts by talking about first-class functions, optimizations, and closures. But the underlying theme of this installment is control: when you want it, when you need it, and when you should let it go. First-class functions and control Using the Functional Java library (see Resources), I last showed the implementation of a number classifier with functional isFactor() and factorsOf() methods, as shown in Listing 1: Listing 1. In the isFactor() and factorsOf() methods, I cede control of the looping algorithm to the framework — it now decides how best to iterate over the range of numbers. Even with the Functional Java framework, coding in this style in Java is cumbersome because the language doesn't really have syntax and constructs for it. Listing 2. Monads Are Not Metaphors. 27 Dec 2010 This article is also available in Japanese. I am about to break a promise. Almost three years ago, I promised myself that I would never write an article about monads.

There are too many such articles already; so many, in fact, that people are often confused by the sheer proliferation. Everyone seems to have a different take on the subject, meaning that those attempting to learn the concept for the first time are stuck trying to reason out the commonalities between burritos, space suits, elephants and desert Bedouins. I’m not going to add to this menagerie of confusing analogies. Math (or not) Here’s the thing about monads which is hard to grasp: monads are a pattern, not a specific type.

Just as a quick Ruby refresher, we can rewrite this code in the following way: Ruby has this neat convention (which is shared by most modern languages) which causes the final expression in a method to be turned into the implicit return statement. Still with me? You Could Have Invented Monads! How to build your own Monads. Monads are beautiful, misunderstood and useful. In this post we will explore how to build your own monads in JavaScript. We will rebuild jQuery's method chaining functionality and implement a monadic Google client for node.js. Before we begin, I think we should address why monads are useful by looking at where they are used. Let's take some popular examples: jQuery is a monad: One of the most popular JavaScript libraries is a monad!

The three laws of monads There are three simple laws of monads: 1) Monads wraps themselves around other data types. 2) Monads have an operation, called return, that performs the wrapping. 3) Monads have an operation, called bind, that allows to feed the value wrapped inside the monad into another function (these functions should return a monad). How do these laws apply to jQuery? $ function wraps itself around DOM elements (satisfies law 1 and 2) $('#some_div').html('test').fadeOut() == $('#some_div'). Implementing jQuery method chaining Last words! Learn You a Haskell for Great Good! - Chapters.