1

Using ramda, I'm trying to find (or build) a very trivial operator that expects a function and returns a function that asks for its arguments before returning a new function that makes the actual call. You can think of this as "delaying" the invocation. As pointed out by replies below, this is usually call a thunk.

Essentially,

const wrap = fn => (...args) => () => fn(...args);

const sayHiWorld = wrap(console.log)('hi', 'world');
sayHiWorld();
// -> 'hi world'

Partial application won't work in my case because args are actually not known at moment of definition. Closest I got was by using R.useWith - but that restricts the number of arguments.

Any ideas?

Nicolás Fantone
  • 2,055
  • 1
  • 18
  • 24
  • This worked for me: `const sayHi = x => \`hi ${x}\`; const sayHiWorld = partial(sayHi, ['world']); sayHiWorld();` – Michael May 31 '18 at 11:08
  • Thanks for the example. Unfortunately, as explained in the question, that would not work for my use case because the actual arguments are unknown at creation time. – Nicolás Fantone Jun 02 '18 at 12:52

1 Answers1

2

I'm a little confused. It looks as though you have a perfectly good version here in your wrap. Are you expecting that Ramda must already have that? Do you want a version of it written with Ramda?

What you want to create with a call to this function is often known as a thunk, and I've sometimes seen such a function called thunkify. But wrap is fine too.

You might see the discussion here too.

Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
  • Thanks for both the answer and the link to the discussion. And for the reminder about the concept of `thunk`s. That's certainly what this is. Though I was expecting an answer along the lines of "but you have a working solution right there", my question was more towards "does such operator exist in Ramda, so I don't have to write it myself". Last time I checked, neither `ramda` nor `ramda-adjunct` offer it and being both so trivial and useful led me believe I was missing something here. – Nicolás Fantone May 24 '18 at 10:56
  • 1
    Ramda itself definitely doesn't offer it. If you wanted to create a PR for such a functions, it would get a hearing from the Ramda team. – Scott Sauyet May 24 '18 at 17:10
  • 1
    PR approved! Thank you for your contribution. – Scott Sauyet Jun 18 '18 at 15:19