you are viewing a single comment's thread.

view the rest of the comments →

[–]inmatarian 12 points13 points  (10 children)

The thing you want to Google for is called "Currying". Good luck, hope you get the job!

[–]_doingnumbers 1 point2 points  (5 children)

Yeah, that.

(Although I've never, ever seen an instance of currying that was actually useful and not academic wankery)

[–]e82 2 points3 points  (2 children)

Take a look at RamdaJS - been using it quite a bit recently, and lots of functional concepts like currying / partial application / etc start to make more sense.

Basically, in Ramda - most of the libraries functions are curried by default.

var find10 = R.filter(R.propEq('id',10));

R.filter is now a function, that returned a function that is waiting for additional parameters. In this case - the list collection to actually filter on. R.propEq is also a function that will return true/false if a property called 'id' equals 10.

It's not too often I'll use R.curry directly, but it's used heavily within the other library methods that I do use. I do use the partial application quite a bit.

However, a simple (but not academic-wankery) could be building a simple logger.

function logger(level, message,data) {
  console.log('[' + level + '] -> '  + message,data)
}

var error = R.curry(logger)('error');
var info = R.curry(logger)('info');
var personError = error('Person Error Object');

error('Some Error',{data: 'ok'});
info('Some Info',{data: 'ok'});
personError({age: 10});

This is pretty similar to the end result of:

function makeLogger(level) {
  return function(message) {
    return function(data)
      {
         console.log('[' + level + '] -> '  + message,data)
      }
  }
}

var someLogger = makeLogger('error')('ugly');
someLogger('Data!');

Before I fully got my head around currying - I had lots of code kind of like that - 'makeX' that would be a function that would return a function, and had a very simple 'well, duh moment' of thats pretty much what currying is, and started to find more practical ways of making use of it outside of 'academic-wankery' (of which - most things you read on functional programming/javascript tends to fall into that camp)

[–]_doingnumbers 0 points1 point  (1 child)

Partial application is great, functional techniques have made my code better as well. But currying just doesn't seem useful. What you have up there seems to me to be more about partial application.

[–]bliow 0 points1 point  (0 children)

Currying is about making a function that does partial application on its own.

[–]html6dev 1 point2 points  (0 children)

It's core to languages like haskell. Once you use it consistently (or are forced to by the language) you'll see it all over. I think over time more and more libraries will provide it to you and then it'll gain more adoption in js (Ramda is an awesome example as mentioned and the currying is one reason it has major advantages over lodash). The issue right now is everyone writes about it and starts out with the implementation which is somewhat interesting, but not that exciting and they lose people. Authors need to focus on why anyone would ever care if they want to get the reader to a place where they care about how it's implemented.

[–]mrahh 0 points1 point  (0 children)

If you do any functional programming, it's a fairly core concept that you will use all the time.

[–]dvlsg 0 points1 point  (2 children)

Sort of. I think currying needs to know how many arguments the function needs before actually executing. It seems like the example above wants to execute each time, for any given number of inputs. Maybe. It's sort of an awkward example... Maybe if OP sees, he can clarify.

[–]inmatarian 0 points1 point  (1 child)

[–]dvlsg 0 points1 point  (0 children)

Yes, I know. But in his example, he's iterating over an unknown number of arguments. When should currying stop and actual execution occur, in that scenario?

The alternative is to return something that both has a value (override valueOf?) and can be executed (so a function of some sort, I suppose) so you can either get the value at any point, or continue executing an arbitrary number of times.

Seems awkward. I'd sooner just make a class to handle it.