0

Im not sure it this is allowed here, if it is not please tell me where I can ask this.

Although I have herd JavaScript is easy language to learn, i find it very difficult to learn. I am reading about functional programming(Ch 6) in Eloquent JavaScript. The whole point seems to be that functional programming is a way to make your programs more clear and intent by using functions to encapsulate ugly little details like a for loop, I get that. What drives me insane is trying to follow code that has a function within a function that returns a function that takes function values as parameters, ect... ect.... My head just spins after a while and I dont see that it makes my life any easier although the final program does look nicer.. Is this just something you get use to? or am I doing something wrong.

DEdesigns57
  • 361
  • 1
  • 5
  • 13
  • *"The whole point seems to be that functional programming is a way to make your programs more clear and intent by using functions to encapsulate ugly little details like a for loop"* You also do this in procedural programming. Functional programming is "more" than that. – Felix Kling Aug 10 '13 at 17:19
  • 3
    Like everything in life, it gets easier with practice. Stick at it. :-) Incidentally, pretty much every "approach" in programming is about abstraction/encapsulation so the lessons you're learning now will pay dividends when/if you take on other languages. – John Parker Aug 10 '13 at 17:20
  • 2
    I'd say the purpose of functional programming is to avoid state; making them clearer is just a handy side effect. It's a very different way of thinking, and one that will take time to figure out. However, once you learn it, you'll be able to write some really beautiful code, and you'll be a better programmer. – Matt Bryant Aug 10 '13 at 17:22
  • Once you learn that, you will have no more issues with JavaScript, but it does take some time to get your head around. JavaScript scopes are all functional. The reason you're downvoted is because you need a more specific question. – Alex W Aug 10 '13 at 17:25
  • 1
    @MattBryant: You cannot avoid state when you're *doing* something. Imho the point of FP is to use state explicitly, and in a way that you can abstract some things over it :-) – Bergi Aug 10 '13 at 18:22

2 Answers2

3

I think the main issue in using javascript to learn functional programming is that javascript doesn't enforce a functional style. While you can write pure functions in javascript, it is very easy not to. Furthermore, despite being easy to learn, javascript is a language with a lot of quirks; it's fairly hard to get a full understanding of the language's mechanics. Another issue is that Javascript doesn't provide most of the features that makes functional programming so fun: lambda expressions, pattern matching, etc aren't built into the language.

If your aim is to learn functional programming I would advice you to use a pure functional language such as Haskell. If, however, you want to learn Javascript specifically make sure you go in depth into understanding concepts such as the prototype chain, type coercion, automatic semicolon insertion, scoping, etc. Here are some useful resources:

Good luck!

Aegis
  • 1,749
  • 11
  • 12
  • I don't think you linked the right Youtube video – Bergi Aug 10 '13 at 18:18
  • Oh, that one. I wouldn't really consider that a "*useful resource*" -[this question](http://stackoverflow.com/questions/9032856/what-is-the-explanation-for-these-bizarre-javascript-behaviours-mentioned-in-the) is one :-) – Bergi Aug 10 '13 at 18:58
  • A bit of fun while learning a language isn't inappropriate :p And yes, looking behind the quirks to understand where they come from is essential! – Aegis Aug 10 '13 at 19:15
0

I sheep dipped myself into functional programming outside of my normal languages first. I didn't start doing it in Javascript, but learnt some Scheme, OCaml and f# first. I found brining the principles back to Javascript easier that way.

Javascript is great as a functional language (in my opinion). For example, a Javascript constructor is a monad (great video on this here: http://www.youtube.com/watch?v=b0EF0VTs9Dc).

I would say, Yes, you will have to get used to it - but the easiest way to do that is outside of JS. Recursion (for example) is a real head-bake when you are used to loops. (Tip: work out your exit condition first!).

Keep at it!

CodeBeard
  • 515
  • 3
  • 10
  • 1
    Could you please explain how a javascript constructor is a monad? (Or link to the specific moment of the talk where Doug shows it) – Bergi Aug 10 '13 at 18:14
  • http://www.youtube.com/watch?feature=player_detailpage&v=b0EF0VTs9Dc&t=714 "a function that takes a value and returns an object" – CodeBeard Aug 10 '13 at 18:19
  • Then you understood it wrong. He says that the `unit` function is a constructor. Only together with a `bind` function it will create a monadic type. – Bergi Aug 10 '13 at 18:28
  • So, how would you describe a monad? (Forgiving, obviously, the case that as soon as you understand monads you lose the ability to explain them) – CodeBeard Aug 10 '13 at 18:31
  • For example, if arrays were the monad (they indeed are one), then `Array` (or better literals, which work for one argument) are the type constructor and `bind` would be concatMap (`Array.prototype.bind = function(f){return [].concat.apply([], this.map(f));}`) – Bergi Aug 10 '13 at 18:31
  • Thanks! I am going to have to digest that a little. Would it be correct that the result of a constructor is a monad - because a monad is an object? – CodeBeard Aug 10 '13 at 18:33
  • 1
    I'm sorry, that comment was supposed to augment my second one, not to be an answer on your question. Indeed, I have already lost the ability to define them (I only do know [they're not](http://www.haskell.org/haskellwiki/What_a_Monad_is_not) burritos). They're an abstract concept that is very helpful for composing things. – Bergi Aug 10 '13 at 18:54