5 ms·
It's true that you can program in a functional style in almost any language, but it's just simpler for Javascript. Functions are first-class objects in Javascri
by darkkindness 10y ago
It's true that you can program in a functional style in almost any language, but it's just simpler for Javascript. Functions are first-class objects in Javascript, and arrays have built-in forEach, map, reduce, and filter -- all useful for a beginner in functional programming. C++ and Java have lambdas and foreach, but they aren't as simple to use.
- eptcyka 10y agoThere is nothing wrong with map, reduce and filter, but I believe this is just conflating nice and usable APIs for iterators with functional programming.
- sulam 10y agohttp://letoverlambda.com/index.cl/guest/chap5.html http://letoverlambda.com/index.cl/guest/chap5.html s/Lisp/JavaScript/g
- seanmcdirmid 10y agoC# on the other hand, support FP pretty well, as long as static typing without HM is acceptable.
- bad_user 10y agoFunctional Programming is about working with mathematical functions. Having "first-class functions" in the language is nice, but then passing function pointers around is nothing more than procedural programming [1] and yes, the difference is huge. As for those utilities you mention, Javascript is the most broken of them all: >>> ["10", "10", "10"].map(parseInt) [10, Nan, 2] [1] https://en.wikipedia.org/wiki/Procedural_programming https://en.wikipedia.org/wiki/Procedural_programming
- jameshart 10y agoWhy does functional programming have to be about math? That is precisely the kind of insistence that prevents people from adopting functional languages for real world programming tasks. JavaScript certainly lacks language level support for function composition, but it has the basic tools to allow you to build combinators and compositional functions yourself, and when dealing with functions as data you often find you can elegantly refactor code by building such a thing - so in terms of 'learning about functional programming' JS can be a great workbench to help procedural minded programmers discover those tools by discovering and building them. So maybe JS doesn't teach functional programming, but it does at least introduce you to paradigms from which it is possible to see the benefits of a functional language. Of course, if you then hear that those languages are only for doing math in then you'll stick with JavaScript.
- acomar 10y ago> Why does functional programming have to be about math? You've misunderstood the GP's point. It's not that FP is about math, it's that you want to emphasize mathematical functions -- functions that don't rely on inputs other than their parameters and return values in a consistent co-domain. This is just good style to improve testability and minimize sources of potential bugs.
- bad_user 10y ago> Why does functional programming have to be about math? Because that's its definition. Change that definition and it becomes meaningless. Functional programming is about working with functions that always have the same output for a given input - the definition of mathematical functions and I wouldn't have to say mathematical if software developers wouldn't feel the need to overload technical terms. Pascal and other languages had great names for pieces of code taking arguments and that aren't functions: procedures, routines, subroutines, etc. > That is precisely the kind of insistence that prevents people from adopting functional languages for real world programming tasks. So let me get this straight, in order to get people to adopt something, you want the definition changed in such a way as to become meaningless? Also, I do expect software developers to know at least high-school level math, because CS is math. Even if you lack a formal education, that's no excuse for ignoring theory that can help in your daily work, especially in this day and age with so many online resources available for free.
- Sean1708 10y agoSo that others don't have to spend time searching: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map#Tricky_use_case https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- YCode 10y agoWhat does that example prove, other than the two methods aren't compatible? If a properly designed method were passed into map it would work fine. In essence: ["10", "10", "10"].map(Number)
- Sean1708 10y agoThe link just explains why it's happening, I'm not passing judgement on whether it's bad or not.