Javascript as a functional language
Asked Answered
C

8

62

I am looking get to grips with functional programming concepts.

I've used Javascript for many years for client side scripting in web applications and apart from using prototypes it was all simple DOM manipulation, input validation etc.

Of late, I have often read that Javascript is one of the languages that supports functional programming.

With my familiarity and experience with Javascript, my preference is to use it to learn functional programming. I expect I would be able to concentrate more on the main functional concepts and not get bogged down or distracted by a completely new syntax.

So in summary, is Javascript a good choice to learn functional programming concepts? What capabilities in Javascript are relevant/support functional programming?

Capers answered 28/9, 2008 at 2:41 Comment(1)
Best way to go is fully functional. Then you will understand the pros and cons.Eardrop
S
44

JavaScript supports first class functions. See Use functional programming techniques to write elegant JavaScript.

Skippet answered 28/9, 2008 at 2:49 Comment(1)
Nice link, exactly what I am looking for.Capers
C
9

Higher Order Javascript is a great way to get familiar with the functional aspects of javascript. It's also a relatively short read in case you want to get your feet wet without diving into a larger book.

Condemnatory answered 29/9, 2008 at 2:11 Comment(0)
T
6

Although javascript supports FP to some degree, it does not directly encourage it. That's why projects like Oliver Steele's Functional exist, to fill in the gaps. So I wouldn't recommend it for learning FP. Check out F# instead.

Trisomic answered 28/9, 2008 at 2:49 Comment(0)
H
6

I would say that although you can quickly grasp some functional programming concepts with JavaScript, using JavaScript consistently like a functional programming language is not a common practice. At least not obviously common. Most people don't post tutorials that pinpoint how to do functional programming with JavaScript -- the one marxidad pointed out is actually a pretty decent example, but you won't find a lot of that. The functional aspects are not often apparent, just like when people use closures in JavaScript, but are unaware that they are doing it.

The idea that you would pass two functions through as arguments to a third function, and then have the return value be some execution related to the first two functions is an advanced technique that almost always appears only in the core of full-blown libraries like jQuery. Self executing anonymous functions and the like have gained ground, but are still not used consistently. The majority of tutorials often focus instead on JavaScript's OO capabilities, like how to create properties and methods, scope, access control and also how to use the prototype property of constructors. Honestly, if functional programming is what you want, then I would choose a language known strictly for this capability.

Hedgerow answered 28/9, 2008 at 3:5 Comment(1)
Useful points, thanks. A more dedicated functional language may be a better choice. I might have a look at F# after all.Capers
Y
6

I don't remember who said it, but javascript has been called "Scheme with Algol syntax". So for learning Scheme/Lisp, Javascript isn't a bad start. Note though that functional languages like Lisp are quite different from pure functional languages, such as Haskell.

Apart from "first-class functions" (Meaning that functions are values, that can be assigned to variables), lexical scope is also an inherent part of what makes a functional language.

Higher Order Javascript and The Little Javascripter has been mentioned already. They are both excellent texts. In addition, Higher Order Programming in Javascript may be an easier start.

Yard answered 29/9, 2008 at 19:31 Comment(0)
C
3

I would recommend reading The Little Schemer, which is a fairly slim book about recursion and is a good introduction to the functional style. Whilst it's focused on Scheme it can easily be applied to JavaScript, see http://javascript.crockford.com/little.html. I found it really helpful in my javascript development, although it gets quite tricky towards the end.

Conversation answered 29/9, 2008 at 14:4 Comment(0)
V
2

Javascript is a multi-paradigm language. If your goal is to learn functional language concepts, try starting with a pure functional language like OCaml or Haskell.

Vernitavernoleninsk answered 28/9, 2008 at 2:50 Comment(2)
What does the "O" in "OCaml" mean, once again?Rurik
+1. Objective Caml, and so, it's not pure functionnal, but multi-paradigm too, as it adds OO.Springlet
R
2

Also, Eloquent JavaScript: Functional Programming chapter.

Restoration answered 29/9, 2008 at 6:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.