46
Skayo
6y

React-Redux's connect() function inspired me to create the coolest way to add numbers in JS:

<script>
function add(num) {
return function(otherNum) {
return num + otherNum
};
};

console.log( add(2)(3) ); // Outputs 5
</script>

I didn't know you could do that. Just found it out!

Comments
  • 15
    Oh dear lord
  • 6
    Dear lord indeed
  • 9
    Even turning numbers into strings then making ascii calculations to find sum is better than that. Can we kill js with fire? I can't take it anymore.
  • 10
    @Skayo

    Welcome to advanced javascript ;-)

    @hack

    This is functional programming. Used unsparingly it's a performance killer, but it has great properties and allows awesome things. Think factory design pattern.

    So no, no fire needed.
  • 8
    It's called currying 😀
  • 2
    @Skayo Check out RamdaJs if you like functional programming
  • 4
    @react-guy didn't you know it's trendy to hate javascript nowadays?

    we must shit-talk it at every opportunity!

    that said i'm surprised you don't run into scoping issues with this.
  • 4
    and that is how Haskell has only functions that take a single argument.

    I fucking love currying.
  • 1
    What you did there is called currying
  • 2
    Currying is awesome 😊

    I use it pretty extensively for things like redux reducers and other factories.
  • 2
    congratulations, you just discovered the main point of JavaScript: writing needlessly convoluted shit just because it looks cool ;)
  • 2
    @pskfry the thing is I have valid reason for myself: complicating things unnecessarily. I can't see any reasons to support functional programming and im really not into functional programming.

    P.s: I don't hate anything.
Add Comment