Currying Broke My Brain

Currying Broke My Brain

So before I get into the meat of this blog. I have to say I'm excited to put my first blog out there. I have been told the best motivation behind blogging is to find something that either excites you and you want to share. Or alternatively, find something you want to learn and share it as you explore it. Well in trying to come up with my first topic, which I kept putting off... I came across a problem, that stumped me while working on some Codewars challenges. The problem was a level 7 Kata called Functional Addition.

Snuck Up On Me

So I loaded up this particular Kata, thinking to myself here's to knocking out another simple level 7. Look the problem instructions are a one liner this is going to be easy.

Create a function add(n)/Add(n) which returns a function that always adds n to any number

Okay that wasn't very descriptive, luckily Codewars has sample tests, heck sometimes when I'm trying to speed through some Katas I don't even look at the instructions first and just look right to the samples and see what my inputs and outputs are supposed to do.

Sample Tests

describe("Tests", () => {
  it("test", () => {
Test.assertEquals(add(1)(3), 4, 'add one to three equals four')
  });
});

Okay so I need to create a function that adds two parameters, no problem this should have been a level 8 intro Kata basic. But wait a minute why does that piece of code look so weird add(1)(3). Part of my brain goes you've seen that before, and I had I just couldn't place it, maybe the code snippet they started the challenge with will save the day.

function add(n) {

}

Nope, that isn't much to work with...

Google Engineering Here We Come

Well first off I did play with some code in trying to come to some solution myself. But the way the function was being called I couldn't figure out how to access all the parameters being sent to the function.

First up I just went ahead and open my most favorite and trusted tool, Google, never leave home without out it. My first search term, functional addition, gave me lots of results but no success. Most of my results were like basic lets create a simple addition function, well that not going to do it this time. Hmm, time to break out my next weapon, coffee, delicious coffee, that solves everything. Okay brain need better search terms for this one to work. Next I tried, JavaScript parameters in separate brackets, and finally got some success. Heck I basically found the solution to the problem I was currently on in the first result. But more important than that I found the terminology for what this problem really was... Currying!!

What is Currying?

First off in so far as I know it has nothing to do with Curry as a spice or as a dish. If you found this blog somehow while looking up Curry recipes, and somehow made it this far, my bad try this recipe Thai Red Curry with Vegetables.

One of the first definitions I could find described it as an advanced technique for working with functions, not only in JavaScript, but in other languages as well. Basically in that it can translate a function f(a,b,c) into one of f(a)(b)(c).

Sweet got a definition lets look at some sample code.

function curry(f) {
  return function(a) {
    return function(b) {
      return f(a, b);
    };
  };
}
function sum(a, b) {
  return a + b;
}
let curriedSum = curry(sum);
alert( curriedSum(1)(2) ); // 3

I read a few lines in and the code was so similar and yet so different I think my eye twitched a little.

Why Use Currying

After stumbling through and getting a basic understanding of why that code worked, and grabbed myself a fresh cup of coffee. Next up back to Google, new search term, why currying am good, sorry lost a few brain cells deciphering the currying code block. But seriously I found an awesome resource A Beginner's Guide to Currying in Functional Javascript. This guide goes on to explain how we can pass a subset of parameters into a function and get back a function that we can get back a function that is waiting for the rest of the arguments.

Finally Solving the Kata

So, given the original `function add(n){ }, we would need to return a function inside the other function.

function add(n){
  return function {
  }
}

Now, I think of what I need to return, well that simple I need to add n to what ever parameter we will call m is sent into the second function. Since. So a return n+m.

function add(n) {
  return function add(m){
     return n+m
  }
}

With that the Kata has been solved my brain is still fractured, I would still like to return to this subject and further look into how I can use currying to my benefit.

If you have found this, I hope you found it either entertaining or educational.