Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Excerpt

Create a new function based on an existing function but with some parameters pre-defined.

 

Syntax

Code Block
themeEclipseRDark
languagejavascript
linenumberstrue
// create new function based on existing one with one or more pre-defined arguments
var fNew = fExisting.uCurry(cArgs);
// use the new function, optionally specifying additional arguments
fNew(args);

Parameters / Functions

Parameter / FunctionTypeMandatoryDescriptionUtil.js version
fExistingFunction(tick)The original function0.5
cArgsOne or more arguments(tick)One or more arguments that will be passed in to fExisting when you access it via fNew0.5
argsAdditional arguments(question)Optionally (based on requirements of fExisting) pass in remaining arguments when calling fNew0.5

Return value

ValueTypeDescriptionUtil.js version
<fNew>FunctionA new function which will automatically set the cArgs arguments for fExisting and then append any additional arguments passed in to fNew.0.5
<fExisting>FunctionIf you do not specify cArgs then the existing function will be returned.0.5

Example

Code Block
themeEclipse
languagejavascript
titleBasic example - pre-defining "not" and "a" params of a "booler" function
linenumberstrue
function add(a,b) {
  return a+b;
}
 
var Add5To = add.curry(5); // parameter "a" is pre-defined with value 5
 
Add5To(10); // 15 (parameter "a" already set to 5, "b" set to 10)

A better example is shown in the Object.uAddProperty() example using a curried allianceExistsBetween() function.

Div
classbox

Availability

This feature requires:

  • Util.js v0.5 and above.
Div
classbox

See also

Related articles:

  • Function.uCompose() – create a new function by merging two existing functions
  • Curry – I based my uCurry function on code samples from this blog (which includes more detailed information on how Curry works).
  • For more info on currying, see Wikipedia.