RosettaCodeData/Task/Call-a-function/Lang/call-a-function-2.lang

32 lines
1.6 KiB
Plaintext

# The function argument auto un-pack operator (▲ or +|) can be used to create a function which must be called with an array value that is automatically unpacked
fp.fixArgs = ($x, $y) -> \!
fp.unpackingFixArgs $= +|fp.fixArgs
fp.unpackingFixArgs(&values) # Fix args will be called with $x=1 and $y=2
# The function argument auto pack operator (▼ or -|) can be used to cerate a function wich must be called with varargs and will call the original function with a single array argument
fp.arrayArg = (&arr) -> \!
fp.packingArrayArg $= -|fp.arrayArg
fp.packingArrayArg(1, 2) # Array arg will be called with [1, 2]
# Functions can also be called with the pipe operators (|, >>, and >>>)
# The "|" and ">>" pipe operators are identical apart from the operator precedence
# The ">>>" pipe operator automatically unpacks array values
fp.func = ($x) -> \!
parser.op(42 | fp.func) # fp.func is called with 42
parser.op(42 >> fp.func) # fp.func is called with 42
parser.op([42] >>> fp.func) # fp.func is called with 42
# Function calls can be concatinated with the concat operator (|||)
fp.incAndPrint $= fn.inc ||| fn.println # Calling fp.incAndPrint($x) has the same effect as calling fn.println(fn.inc($x))
fp.incAndPrint(2) # Prints 3
# The pow operator can be used to call a function multiple times in succession
# This works only with exponents >= 0 (If the exponent is 0 a function will be returned, that always returns VOID)
fp.voidFunc $= fn.inc ** 0
fp.voidFunc(2) # Returns VOID
fn.pow(fn.inc, 1)(2) # Returns 3
fn.pow(fn.inc, 2)(2) # Returns 4
fn.pow(fn.inc, 3)(2) # Returns 5
fn.pow(fn.inc, 10)(2) # Returns 12