thunkify.js 946 B

123456789101112131415161718192021222324252627282930313233
  1. import curryN from "./curryN.js";
  2. import _curry1 from "./internal/_curry1.js";
  3. /**
  4. * Creates a thunk out of a function. A thunk delays a calculation until
  5. * its result is needed, providing lazy evaluation of arguments.
  6. *
  7. * @func
  8. * @memberOf R
  9. * @since v0.26.0
  10. * @category Function
  11. * @sig ((a, b, ..., j) -> k) -> (a, b, ..., j) -> (() -> k)
  12. * @param {Function} fn A function to wrap in a thunk
  13. * @return {Function} Expects arguments for `fn` and returns a new function
  14. * that, when called, applies those arguments to `fn`.
  15. * @see R.partial, R.partialRight
  16. * @example
  17. *
  18. * R.thunkify(R.identity)(42)(); //=> 42
  19. * R.thunkify((a, b) => a + b)(25, 17)(); //=> 42
  20. */
  21. var thunkify =
  22. /*#__PURE__*/
  23. _curry1(function thunkify(fn) {
  24. return curryN(fn.length, function createThunk() {
  25. var fnArgs = arguments;
  26. return function invokeThunk() {
  27. return fn.apply(this, fnArgs);
  28. };
  29. });
  30. });
  31. export default thunkify;