Page 1

What is Functional Programming While imperative code tells the machine, step-by-step, what it needs to do to solve the problem, functional programming instead seeks to describe the problem mathematically so that the machine can do the rest. What's the problem that we're trying to solve? The problem is the so called software crisis. We all know as developers that writing code is pretty difficult. We're writing huge programs and what we need to do is to reduce the time and the cost of development. We want to deliver software fast and we want to deliver correct software. The question is how can we make programs and code such that this code is fast. It doesn't take years to deliver but it takes doesn’t take months or as long as it needs to be. One way to do that, and we see this now in all modern programming languages, is to add features to our languages that allow programs to be written clearly concisely and at a high level of abstraction. And the techniques that we use in these modern languages are all based on functional programming. These techniques are all based on concepts from the lambda calculus. And Haskell is a functional language that is pure and therefore a really good vehicle to teach these concept. Functional languages therefore provide an elegant framework of abstraction. You can apply functional programming to any language even if your programming in assembly.

What is a functional programming language? The concepts vary, there is not clear definition. If we take a very purist approach a functional language is a language in which we program using mathematical functions. Functional, or declarative, programming is a very powerful programming method and is gaining popularity in the software industry. It is a style of programming in which expressions are more important than using statements. What we want to do is: we want to compose programs using expressions and these


expressions deliver a value so we take two expressions that deliver a value and compose them in a bigger expression. Just to clarify, when you compose statements the statements have an implicit side-effect on the global state and they communicate values via that global state. Whereas in an expression based programming style expressions return values and we compose these values directly. A functional programming language is a language that supports and encourages writing code using expressions. As I said before, most modern languages now support this style. And they support the style specifically by supporting lambda expressions. Because functional programming encompasses a very different way of composing programs, programmers who are used to the imperative paradigm can find it difficult to learn. Historically, functional programming languages have not been very popular for various reasons. Some of the existing languages borrow concepts from functional programming languages. Iterators and continuations show up in some implementations of C++ and some functional constructs provided in JavaScript. However, by borrowing functional constructs, the overall programming paradigm of the language does not change. There are a view concepts such as (a) computation as the application of functions, (b) Stateless, and (c) avoiding side effects. (a) Object-oriented programming uses functions but functional programming is unique in that functions are values. You can store functions in variables, return them as the value of other functions and pass them in as arguments to other functions. As a result functional programs produce answers by applying these functions in different ways. (b) Programming languages have the notion of “state”. A state is a description of a program’s current environment: all variables are declared, and functions are created. An expression in a programming language can be “stateful” or “stateless”. A stateful expression is one that changes a program’s current environment. A very simple example in Javascript would be incrementing a variable by 1. (c) If a function executes it may change the state after returning a value. Functional programming promotes the use of “pure functions”: functions that have no side effects. If we can always write functions that have no side effects our program becomes “referentially transparent”. In short, your program becomes deterministic.

So why bother programming functional code? Functional programming does look inferior when faced with environments that are constantly in flux. This is particularly true of the DOM which can constantly change through insertions, deletions, animations etc.


The big advantage functional programming gives us is that we can reason about and test our programs much more easily. This can help us to be more productive, reduce bugs and write better software. It is not always possible to write everything in a purely functional manner but it is something to strive for. John Carmack of Id Software wrote a good article discussing the practical considerations of functional programming.

Think Function You don’t have to abandon everything you learned to adapt to the functional paradigm. You can get started thinking about your JavaScript in a functional way by considering a few questions when you write your next program: • • • • •

Are my functions “dry”? Are they pure and independent - single responsibility principle? Are these functions deterministic? (always returning the same result for a given input) Am I sure that my functions don’t modify anything outside of their scope? If I wanted to use these functions in another program, would I need to make changes to them? This introduction barely scratches the surface of functional JavaScript, but I hope it whets your appetite to learn more.

In conclusion Whilst functional programming has many benefits including more rigorous code and enhancements to productivity, it can be difficult at times to write programs in a purely functional manner in Javascript. Consequently, some pragmatism is required. Second, don't be scared of functional programming. Yes, it is often thought of as pure logic in the form of computer language, but we don't need to understand Lambda calculus to be able to apply it to everyday tasks. The fact is, by allowing our programs to be broken down into smaller pieces, they're easier to understand, simpler to maintain, and more reliable. map() and reduce() function's are lesser-known built-in functions in JavaScript, but we'll look at them. And just what could I do for you? Well, let’s find out. You can get in touch with me through the following mediums. Let’s talk coding and all that it could deliver for your project, whatever that may be. Email - michel.herszak@gmail.com Twitter - @MHerszak (twitter.com/MHerszak) Want to know even more about the way in which I work? You can check me out over on my website, right here.

What is Functional Programming  

What's the problem that we're trying to solve? The problem is the so called software crisis. We all know as developers that writing code is...

What is Functional Programming  

What's the problem that we're trying to solve? The problem is the so called software crisis. We all know as developers that writing code is...

Advertisement