1

For currying,if we have a function like this:

function multiply(x, y){
  return x * y;
}

multiply(3, 5); // 15

Then its currying version is:

function curriedMultiply(x) {
  return function(y) {
    return x * y;
  }
}
curriedMultiply(3)(5);  // 15

For Thunk,if we have a function like this(in Nodejs):

let fs = require('fs')
fs.readFile(fileName,callback)

Then its Thunk version is:

let thunkify = require('thunkify')
let read = thunkify(fs.readFile)
read(fileName)(callback)

Looks like thunk is something to do with currying?They all turn a multi-parameters function into a single-parameters function.And this single-parameters function will return another function which takes the original second parameter and then return the final result.But I notice thunk only takes the callback as the second parameter while for currying there is no limit on the type of the accepeted parameter.

So,is thunk one of the special cases of currying?

Chor
  • 833
  • 1
  • 5
  • 14
  • This is misleading. What [co calls a "thunk"](https://github.com/tj/co#thunks) is a quite specific [thunk](https://en.wikipedia.org/wiki/Thunk) that takes a [continuation](https://en.wikipedia.org/wiki/Continuation) (but it otherwise "fully evaluated"). – Bergi May 19 '20 at 10:56
  • No, `thunkify` does not curry a function. It turns a function that takes multiple arguments, the last of which is a callback, into a function that takes multiple arguments (but no callback) and returns a thunk. – Bergi May 19 '20 at 10:58
  • Notice that you would still call `read(fileName, "utf8")(callback)`. It's not a single-parameter function. – Bergi May 19 '20 at 10:58
  • @Bergi But here we can leave out the parameter `utf8` thus it is a single-parameter function.Like `read('package.json')(callback)` which is similar to currying. – Chor May 19 '20 at 11:01
  • It's a function with multiple parameters, some of which are optional because that's how `fs.readFile` is defined. Try thunkifying some other function. – Bergi May 19 '20 at 11:10
  • But for currying,each function must be a single-parameter function(no matter the parameter is optional or reuqired).So thunk is difference from currying,right? – Chor May 19 '20 at 11:15
  • Yes, that's what I'm saying, they're totally different - see also the links and the duplicate topic I provided. It's really just a coincidence that for a "single parameter + callback" function they are looking similar. – Bergi May 19 '20 at 11:21
  • @Bergi just fail to search the same question due to some reasons.Sorry to create a duplicate topic haha – Chor May 19 '20 at 11:22
  • Posting a duplicate is not a mistake :-) I only could find it because I remember having answered it. – Bergi May 19 '20 at 12:29

0 Answers0