Versions Compared

Key

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

KRL supports functions as first-class objects. KRL supports only anonymous functions, but they can be given names by binding them to a variable in a declaration. Here's an example:

...

Functions are declared using the keyword function followed by a possibly empty, parentheses-delimited, comma-separated list of parameters and a block of code.

The parameters are a possibly empty, comma-separated list of variable names. All parameters can be assigned a default value.  More details can be found in Using Optional and Named Parameters

The code block of the function is delimited by curly braces and contains a list of semicolon-separated declarations and a single expression. The declarations are optional, but every function code block must end with an expression. The value of the final expression is the return value of the function.

...

Code Block
languagejavascript
themeConfluence
plus1 = inc_gen(1);
plus25 = inc_gen(25);


Functions can also be applied using the dot (.) operator syntax sugar. The left hand of the period (.) is inserted as the first argument of the function call.  This is useful when you want to chain together many operations. For example:

Code Block
languagejavascript
themeConfluence
foo = function (self, arg1, arg2) {
  // `self` is the left hand side of `.`
}
 
// then use it
"something".foo(1, 2)
// which is the same as
foo("something", 1, 2)
 
// Another example:
a.split(re#;#).length()
// is the same as
length(split(a, re#;#))

// Instead of this
length(split(lc(a), re#;#));
// use . operator to chain them together
a.lc()
 .split(re#;#)
 .length();