Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a set of restrictions I can use to simulate functional programming in JavaScript?

I was watching a talk on clean code by Misko Hevery, and he mentioned trying to write a program with no if statements in them (well, as few as humanly possible) in order to simulate working on... Smalltalk or some such language, where polymorphism is preferred over inline conditional behavior.

To my limited understanding, functional programming is hard for only-imperative-so-far programmers like me - because our state changing methodologies don't have a way to be expressed in functional programs. A function only takes a value and returns a value and knows nothing about state.

I have also seen JS being hailed as being able to support a functional model.

So is there a simple set of restrictions, similar to my first paragraph, which will enable me to try out the functional paradigm in a language I know and love - rather than learn a full new language (I'll do that eventually but I want to try the ethos right now)?

like image 824
Aditya M P Avatar asked Oct 27 '13 19:10

Aditya M P


1 Answers

There are two meanings to the term "functional programming".

The first meaning is the ability of a program to manipulate functions. Not all languages can do this but javascript is one of the languages that can. Languages that can assign functions to variables, pass functions to arguments and return functions are called functional programming languages so javascript is functional as is.

In this sense, if you look at any modern javascript code with prevalent use of callbacks then you're already doing functional programming.

The second meaning of functional programming is the style of programming where the primary method of program composition is functions rather than variables. In this sense almost any language can be used in a functional style by avoiding variable assignments and loop structures (use recursion instead).

When you look at the functional community, what they mean by functional is the first meaning plus a very strong version of the second meaning -- that is, variables are not only avoided but banned. Languages like Haskell don't have the concept of variables. To handle side-effects and mutable state like I/O they use a concept called monads.

You don't have to go that far. Classical functional languages like Lisp and Forth allowed variables. You just have to avoid them where possible.

Lisp and Forth style functional programming is heavily driven by processing lists/arrays without assigning anything to temporary variables. To some people, this style is easier to read. Where in imperative style you'd do this:

var a = [1,2,3];
var b = 0;
for (var i=0;i=a.length;i++) {
    b += a[i] * 2;
}
// result is in b

in functional style you'd do this:

[1,2,3].
    map(function(x){return x*2}).
    reduce(function(x,y){return x+y},0);

Conceptually, functional style makes it look like you're applying filters to the array instead of iterating through the array. If you've ever used command line tools like grep then you'd find this concept very familiar.

Notice that we haven't introduced any variable assignments at all in the functional style.

The three core array methods/functions in the functional style are: map, reduce and filter. With them you can avoid something like 90% of for-loops.

like image 64
slebetman Avatar answered Sep 24 '22 15:09

slebetman