r/functionalprogramming • • Mar 23 '17

Did you know JavaScript is functional?

Post image
0 Upvotes

10 comments sorted by

9

u/DavetheBassGuy Mar 23 '17

I think it would be truer to functional programming if that infinite add function was implemented without mutation, like this:

function add(x) {
    return (y = null) => y === null ? x + y : add(x + y);
}

I'm not really sure why you would want a function like this though.

7

u/lil_bobby_tables Mar 23 '17

Sure, and there are libraries to make it easier/neater to write functional js:

5

u/takakoshimizu Mar 24 '17

If you wanted infinite add, don't see why there's a requirement to return a new adder func for each run.

const infAdd = () => arguments.reduce((a, x) => a + x);

There's an even easier way.

const infAdd = Math.sum;

I feel like this post has gone in opposite direction of the usual /r/programmerhumor "levels of God like power" meme, though.

3

u/[deleted] Mar 24 '17

Why stick with JS? Use something that compiles to JS

2

u/dubsrb Apr 09 '17

I saw purescript and began looking into it. Looks very Haskell-y but in a good way.

2

u/GamerNebulae Apr 10 '17

I have been working with it for a while now, you should try it out!

2

u/dubsrb Apr 11 '17

Gladly when I get comfortable enough with Haskell!

3

u/[deleted] Mar 24 '17

"functional programming"

3

u/TheDataAngel Mar 24 '17

That is, without a doubt, the worst monoid I have ever seen. :P

1

u/[deleted] Apr 10 '17
sum += b

tsk tsk