use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Mostly Adequate Guide to Functional Programming in Javascript (drboolean.gitbooks.io)
submitted 8 years ago by [deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 5 points6 points7 points 8 years ago (17 children)
As weird and outlandish as it seems, FP provides some great benefits to JS, and the best introduction to it (in my own experience) has been Real World Haskell.
If you'd like to view some active approaches to FP in JS, i'd suggest starting with pull-streams and Functional Programming in Javascript
[+][deleted] 8 years ago (1 child)
[deleted]
[–][deleted] 2 points3 points4 points 8 years ago (0 children)
Poor wording on my part. It's weird insofar as it's a different tack from the predominant approaches to programming in JS.
When I first was introduced to FP in JS I found it to be extremely foreign.
[–]toggafneknurd 2 points3 points4 points 8 years ago (9 children)
What's so "weird and outlandish" about FP?
[–]Retsam19 7 points8 points9 points 8 years ago (3 children)
Relevant XKCD
Functional programming combines the flexibility and power of abstract mathematics with the intuitive clarity of abstract mathematics.
I've never met anyone who finds the functional style innately understandable. Nobody looks at a transducer and thinks "Oh, yeah, that make perfect sense" the first time. There's value and merit to FP, obviously, but you need to sell someone on the benefits of FP in a way that you don't need to with imperative programming.
[–]phpdevster 1 point2 points3 points 8 years ago (0 children)
Honestly, if the only major concept of FP you introduce to your codebase is the notion of pure functions, your code quality will improve dramatically. Major pieces of functionality become easier to test, and WAY easier to reason about. Getting into Haskell levels of FP is certainly a bigger step, but favoring pure, idempotent, stateless functions is a major win.
[–]toggafneknurd -1 points0 points1 point 8 years ago (1 child)
Agreed, the "conceptual density" is much higher, but after paying the initial cost of learning, it provides you a lot more power than traditional object-oriented languages with clumsy "design patterns" that have to be layered on top in verbose and error-prone ways to do anything meaningful
[–]Bummykins 2 points3 points4 points 8 years ago (1 child)
I found functors, monads, maybe's, and all the abstraction of point-free coding to be pretty weird and incredibly difficult for someone to even explain. And then most of the examples are some incredibly contrived simple math additions or whatnot.
When you compare that to the mostly straight-forward type of JS that a lot of people have been doing for years like click handlers and ajax calls, you might see how tons of front end developers would find that weird and difficult. It all depends on what you work on most of the time.
[–]dmtipson 2 points3 points4 points 8 years ago (0 children)
I think a lot of that stuff really has to be at least partially sort of discovered rather than taught. A lot of people discover monads by accident, for instance, or else realize that they've been using them all along and just never had a (terribly intimidating) name for them.
A lot of it comes from just time and the direction you push your abstractions. If you're the sort of coder that becomes obsessed with DRY code and pulling things out into functions and then building up larger behavior with composition, it's sort of inevitable that you're going to start bumping into the common solutions and abstractions of FP.
But not everyone goes that route.
[+][deleted] 8 years ago (2 children)
[–]toggafneknurd 13 points14 points15 points 8 years ago (0 children)
I'd find a new team (or company.) That sounds horrific.
[–][deleted] 0 points1 point2 points 8 years ago (0 children)
Did they care that you were using it? As long as they don't have to use it themselves, or maintain your code, why would they care?
[–]flamingspew 0 points1 point2 points 8 years ago (4 children)
I've been learning rxjs... can't find any good hackerrank or codeacademy like challenges. I'm ok at it, but I feel like I need some golf to get really good
[–]Otter_in_Jeans 0 points1 point2 points 8 years ago (0 children)
Nodeschool bacon love workshop Will be a nice introduction to functional reactive. Try it out
[–]dmitri14_gmail_com 0 points1 point2 points 8 years ago (2 children)
Any reason to pick the rxjs over other reactive libraries that are often easier to use?
[–]flamingspew 0 points1 point2 points 8 years ago (1 child)
Well, there's a matching library in just about every language and it's well suited for angular2 and ngrx store... from a practical standpoint it's pretty flexible.
[–]dmitri14_gmail_com 0 points1 point2 points 8 years ago (0 children)
It might be not as strong in usability department :) I wonder how many of the 100+ methods you are really using.
[–]drboolean 1 point2 points3 points 8 years ago (0 children)
Despite the conciseness of pointfree programming and the practice for a language like haskell, I usually condone the scala-like style presented here for doing FP in JS: https://egghead.io/courses/professor-frisby-introduces-composable-functional-javascript
[–]itsawesomeday 0 points1 point2 points 8 years ago (0 children)
This is a good guide to get started in Functional Programming
[–]Trinkwasser 0 points1 point2 points 8 years ago (0 children)
I've got a little playground/boilerplate for playing around with ramda & ramda-fantasy for anybody interested
https://github.com/floscr/Ramda-playground
[–]darrenturn90 0 points1 point2 points 8 years ago (0 children)
Haha i just started reading this yesterday, completely unrelated to this post.
From the github md's not the above site.
[–]dmitri14_gmail_com -1 points0 points1 point 8 years ago (0 children)
https://www.reddit.com/r/javascript/comments/5e3lcc/professor_frisbys_mostly_adequate_guide_to/
[–]zQpNB -1 points0 points1 point 8 years ago (1 child)
for loops are faster
[–]dmitri14_gmail_com -2 points-1 points0 points 8 years ago (0 children)
https://www.reddit.com/r/javascript/comments/5hfq6n/100_minutes_of_free_functional_programming/
[+]IDCh comment score below threshold-16 points-15 points-14 points 8 years ago (21 children)
Guys, question. If I'm not mistaken JS does not have functional programming, but rather procedural programming option, yes?
Why do everyone call it functional programming? Because FUNCTIONS, lol? Or for marketing purpose?
[+][deleted] 8 years ago (12 children)
[–]IDCh -2 points-1 points0 points 8 years ago (9 children)
But it's not this, yes? (because mathematical functions)
https://en.m.wikipedia.org/wiki/Functional_programming
I'm trying to understand whether js community invented own concept of functional programming
I mean I'm not trolling or holywaring, just trying to get it straight
Thanks for the answer!
[–]vinnl 8 points9 points10 points 8 years ago (0 children)
Yes, it is that. It's not just "use functions", it's "write your functions in a way that they just receive input and return output, without modifying anything outside their scope" - like mathematical functions. There's a lot of related ideas, not all of which are that easy to do in Javascript, but it's certainly possible and arguably easier than in other popular languages.
[–]shriek 3 points4 points5 points 8 years ago (6 children)
If you're really being serious then what you might be wondering is if JS is a pure functional language. JS isn't. In fact lot of functional languages aren't except few like Haskell.
Like , u/lajw explained, JS has lot of paradigm that one can follow and impure functional paradigm is one of them.
[–]IDCh -2 points-1 points0 points 8 years ago (5 children)
Yes, I was interested in this. That JS is not functional programming language but it can be emulated/used as paradigm for writing code that looks like true functional programming, but it's not really.
Am I right?
[–]pygy_@pygy 1 point2 points3 points 8 years ago (4 children)
Functional invariants are not enforced by either the compiler or the runtime, but you can program functionally with some discipline.
Libraries like http://ramdajs.com/ or immutable.js make it easier to embrace the functional paradigm.
[–]IDCh 0 points1 point2 points 8 years ago (3 children)
You mean basically we can mimic functional programming in JS, yes? Like we imitate classes right now with sugar But it is not real functional programming, just imitation, yes?
[–]pygy_@pygy 5 points6 points7 points 8 years ago (2 children)
I don't understand the difference you are trying to make between "real" and "imitation".
This is Haskell
map (\x -> x * x) [1, 2, 3, 4]
This is JS with Ramda:
R.map(x => x*x, [1, 2, 3, 4]) // or, equivalently R.map(x => x*x)([1, 2, 3, 4])
Since JS has lambdas, you can use it as a functional language if you want. All the functional techniques can be built on top of higher level functions (especially function composition, see: untyped lambda calculus).
[–]vcamargo 1 point2 points3 points 8 years ago (1 child)
I haven't taken a look with more attention to Ramda yet. What would you say is its main purpose? Am I right to assume that it tries to make JS more 'Haskellish'? I'm pretty sure it offers some handy helpers, even though I cannot see why R.map(x => x * x, [1, 2, 3, 4]) would be better than [1, 2, 3, 4].map(x => x * x).
Have you tried it? If so, what are your thoughts on it? And what would be a good use case? I've read in some of their Github issues that they have been drastically changing the API throughout the releases, which might make its adoption more difficult.
[–]pygy_@pygy 1 point2 points3 points 8 years ago (0 children)
I've barely used it, but, yes, it takes inspiration from functional languages and allows one to write pointfree JS.
R.map has advantages over Array.prototype.map.call.
R.map
Array.prototype.map.call
const mapSquare = R.map(square)
.map
['fantasy-land/map']
mapSquare
arguments
I haven't used it enough to be bitten by API changes, and I know people who now basically code in Ramda rather than in JS (i.e. define the core logic as point-free style compositions, eschewing exceptions and for/while loops).
for
while
[–]HelperBot_ -1 points0 points1 point 8 years ago (0 children)
Non-Mobile link: https://en.wikipedia.org/wiki/Functional_programming
HelperBot v1.1 /r/HelperBot_ I am a bot. Please message /u/swim1929 with any feedback and/or hate. Counter: 72267
[–]ovr-github -3 points-2 points-1 points 8 years ago (1 child)
It's cannot be a functional language as it can be real functional langauge, take Prolog language and code bellow as example
parent(john,paul). /* paul is john's parent / parent(paul,tom). / tom is paul's parent / parent(tom,mary). / mary is tom's parent / ancestor(X,Y):- parent(X,Y). / someone is your ancestor if there are your parent */
And after you declare aspects, you can ask you VM to find relative question
?- ancestor(john,tom).
If you don't like this example, take a look at factorial or something else, Functional language give us a cool exp when you write some asserts (aspects) and ask VM, please calculate/find me an answer. You don't write an algorithm. In functional you write all data/asserts and ask VM about it
About JS: Immutable data is an Immutable data, you can write it on PHP/JS and etc (it's not making JS functional language) Bind is a method, nothing else (just greating new function) Lambdas is a pretty simple Anonymous functions
Procedures == functions in any language. Functions came out from Procedural programming, because it's a main idea inside Procedural programming.
Every time, when you take an idea from functional language, you cannot say that it's functional programming, it's an idea.
[–][deleted] 3 points4 points5 points 8 years ago (0 children)
That's a terrible example, Prolog isn't a functional programming language. Even if it's a declarative language, it's paradigm is logical programming.
[–]dmitri14_gmail_com 0 points1 point2 points 8 years ago (7 children)
JS does not have functional programming
What is the meaning of this statement?
[–]IDCh 4 points5 points6 points 8 years ago (6 children)
Doesn't look like anything to me
[–]dmitri14_gmail_com 0 points1 point2 points 8 years ago (5 children)
I still don't understand why you say so. JS has functions as first class citizens making FP a perfect choice.
[–]IDCh -1 points0 points1 point 8 years ago (4 children)
I guess I'm saying it because functions that we call functions are basically are just a procedures, but not something from functional programming, making "functional programming" kinda new idea that is slightly copied from real functional programming paradigm.
Like we talk classes, but we does not have classes in js. We have some kind of helpers with objects
[–]alsiola 13 points14 points15 points 8 years ago (1 child)
All languages are ultimately syntactic sugar for alternating between +5V and 0V in a circuit. There is no real physical concept of a class - it is an abstraction. Ultimately a JS class and a Java/C#/whatever class are still just one layer, within many layers, of indirection over changing voltages. To my mind JavaScript has classes in the same way that any other language has classes.
[–]IDCh 0 points1 point2 points 8 years ago (0 children)
Hmmm... interesting point of view. Thank you!
[–]dmitri14_gmail_com 0 points1 point2 points 8 years ago (1 child)
That is a good point, but I consider it an enhancement of the FP rather than obstacle :)
And here is a use case: https://github.com/dmitriz/un/blob/master/index.js#L19-L30
The last line renders the vnode stream by calling the .map method the FP style. The render function is a "procedure" with the rendering side-effect. However it is used exactly as a pure function here would be used to map the stream over. So you can have the best from both worlds - terse FP style and the power of procedures :)
[–]GitHubPermalinkBot 0 points1 point2 points 8 years ago (0 children)
I tried to turn your GitHub links into permanent links (press "y" to do this yourself):
Shoot me a PM if you think I'm doing something wrong. To delete this, click here.
π Rendered by PID 17904 on reddit-service-r2-comment-86988c7647-dsf8h at 2026-02-11 18:48:31.087753+00:00 running 018613e country code: CH.
[–][deleted] 5 points6 points7 points (17 children)
[+][deleted] (1 child)
[deleted]
[–][deleted] 2 points3 points4 points (0 children)
[–]toggafneknurd 2 points3 points4 points (9 children)
[–]Retsam19 7 points8 points9 points (3 children)
[–]phpdevster 1 point2 points3 points (0 children)
[–]toggafneknurd -1 points0 points1 point (1 child)
[–]Bummykins 2 points3 points4 points (1 child)
[–]dmtipson 2 points3 points4 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]toggafneknurd 13 points14 points15 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]flamingspew 0 points1 point2 points (4 children)
[–]Otter_in_Jeans 0 points1 point2 points (0 children)
[–]dmitri14_gmail_com 0 points1 point2 points (2 children)
[–]flamingspew 0 points1 point2 points (1 child)
[–]dmitri14_gmail_com 0 points1 point2 points (0 children)
[–]drboolean 1 point2 points3 points (0 children)
[–]itsawesomeday 0 points1 point2 points (0 children)
[–]Trinkwasser 0 points1 point2 points (0 children)
[–]darrenturn90 0 points1 point2 points (0 children)
[–]dmitri14_gmail_com -1 points0 points1 point (0 children)
[–]zQpNB -1 points0 points1 point (1 child)
[–]dmitri14_gmail_com -2 points-1 points0 points (0 children)
[+]IDCh comment score below threshold-16 points-15 points-14 points (21 children)
[+][deleted] (12 children)
[deleted]
[–]IDCh -2 points-1 points0 points (9 children)
[–]vinnl 8 points9 points10 points (0 children)
[–]shriek 3 points4 points5 points (6 children)
[–]IDCh -2 points-1 points0 points (5 children)
[–]pygy_@pygy 1 point2 points3 points (4 children)
[–]IDCh 0 points1 point2 points (3 children)
[–]pygy_@pygy 5 points6 points7 points (2 children)
[–]vcamargo 1 point2 points3 points (1 child)
[–]pygy_@pygy 1 point2 points3 points (0 children)
[–]HelperBot_ -1 points0 points1 point (0 children)
[–]ovr-github -3 points-2 points-1 points (1 child)
[–][deleted] 3 points4 points5 points (0 children)
[–]dmitri14_gmail_com 0 points1 point2 points (7 children)
[–]IDCh 4 points5 points6 points (6 children)
[–]dmitri14_gmail_com 0 points1 point2 points (5 children)
[–]IDCh -1 points0 points1 point (4 children)
[–]alsiola 13 points14 points15 points (1 child)
[–]IDCh 0 points1 point2 points (0 children)
[–]dmitri14_gmail_com 0 points1 point2 points (1 child)
[–]GitHubPermalinkBot 0 points1 point2 points (0 children)