Call a function: Difference between revisions
Content added Content deleted
m (→{{header|Kotlin}}: made the Kotlin example not use Java) |
(Add lang example) |
||
Line 3,300: | Line 3,300: | ||
More can be seen in http://lambdaway.free.fr/lambdawalks/?view=lambda |
More can be seen in http://lambdaway.free.fr/lambdawalks/?view=lambda |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Lang}}== |
|||
<syntaxhighlight lang="lang"> |
|||
fp.noArgs = () -> \! |
|||
fp.noArgs() |
|||
# For user defined-functions, the argument count is not checked: If too many arguments were provided, they are ignored. If not enough arguments were provided, the last argument will be duplicated (If none was provided, VOID values will be filled in). [This process is is referred to as implict argument duplication] |
|||
fp.noArgs(42) # No Error nor Warning |
|||
fp.fixArgs = ($x, $y) -> \! |
|||
fp.fixArgs(1, 2) |
|||
fp.fixArgs(2) # Fix args will be called with $x=2 and $y=2 |
|||
fp.fixArgs() # Fix args will be called with $x=VOID and $y=VOID |
|||
# fn.argCntX (X must be replaced with 0, 1, 2, 3, 4, or 5) can be used to force the caller to provided the exact argument count |
|||
# fn.argCntX must be called with the function to apply the constraint to and will return a new function |
|||
fp.realFixArgs = fn.argCnt2(fp.fixArgs) |
|||
fp.realFixArgs(1, 2) |
|||
fp.realFixArgs() # Error |
|||
fp.realFixArgs(1) # Error |
|||
fp.realFixArgs(1, 2, 3) # Error |
|||
# Arrays can be unpacked in function calls |
|||
&values $= [1, 2] |
|||
fp.fixArgs(&values...) # Fix args will be called with $x=1 and $y=2 |
|||
# In lang there are text and array varags parameters |
|||
fp.varArgsText = ($text...) -> \! |
|||
fp.varArgsText(1) # Var args text will be called with "1" |
|||
fp.varArgsText(1, 2) # Var args text will be called with "1, 2" |
|||
fp.varArgsText(1,2) # Var args text will be called with "1,2" |
|||
fp.varArgsText(1,text ,3) # Var args text will be called with "1,text ,3" |
|||
fp.varArgsArray = (&args...) -> \! |
|||
fp.varArgsArray(1) # Var args array will be called with [1] |
|||
fp.varArgsArray(1, 2) # Var args array will be called with [1, 2] |
|||
fp.varArgsArray(1,2) # Var args array will be called with [1, 2] |
|||
fp.varArgsArray(1,text ,3) # Var args array will be called with [1, text, 3] |
|||
# Functions with named arguments can not be created |
|||
# Using a function in a statement context |
|||
$x = fp.fixArgs(1, 2) |
|||
# Functions (Even predefined and linker functions) can be used as values |
|||
fp.retAFunc = () -> { |
|||
return ($x) -> \! |
|||
} |
|||
fp.func = fp.retAFunc() |
|||
fp.func(2) |
|||
# Multiple call-expressions can be used directly |
|||
fp.retAFunc()(2) |
|||
fp.retAFunc = () -> return fn.println |
|||
fp.retAFunc()(test, values) |
|||
fp.retAFunc = () -> return ln.loadModule |
|||
fp.retAFunc()(x.lm) # Error, because file not found |
|||
# The return value or the thrown error can be obtained with the assignment operator |
|||
fp.inc2 = ($x) -> return parser.op($x + 2) |
|||
$ret = fp.inc2(40) # $ret is 42 |
|||
# Built-in (They are called predefined functions in lang) start with the "func." or "fn." prefix wheras user-defined functions start with "fp." |
|||
# Linker functions start with "linker." or "ln." |
|||
# Predefined and linker functions can be stored in a user-defined function |
|||
fp.userDefinedFunc = fn.println |
|||
fp.userDefinedFunc(Called println) |
|||
# In lang there are no subroutines |
|||
# In lang functions can have call-by-pointer values |
|||
# $ptr is a pointer to the called value |
|||
fp.callByPtr = ($[ptr]) -> \! |
|||
fp.callByPtr(42) # This will create a pointer to an anonymous value, therefor it can not be changed |
|||
fp.inc2 = ($[ptr]) -> $*ptr += 2 |
|||
fp.inc2(40) # Error |
|||
$val = 40 |
|||
fp.inc2($val) # $val is now 42 |
|||
# Functions can also be called with pointers directly |
|||
fp.inc2 = ($ptr) -> $*ptr += 2 |
|||
fp.inc2(40) # Multiple Errors (Value will be dereferenced as NULL -> + is not defined for NULL and INT AND anonymous values can not be changed) |
|||
$val = 40 |
|||
fp.inc2($[val]) # $val is now 42 |
|||
# Partial apllication of functions is possible by using combinator functions |
|||
# The simplest combinator function-family is the A combinator family (Other families change the order of arguments, can call multiple function, ...) |
|||
fp.partialAdd = fn.combA2(fn.add) # When all arguments for fn.combA2(a, b, c) are provided, the execution of a(b, c) will begin |
|||
fp.add42 = fp.partialAdd(42) # Creates a function which still needs 1 argument |
|||
fp.add42(2) # Will return 44 |
|||
# Without the use of fn.argCntX 0 args can also be provied |
|||
fp.add42()()()(2) # Will also return 44 |
|||
</syntaxhighlight> |
|||
===Special function-related features=== |
|||
<syntaxhighlight lang="lang"> |
|||
# 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 |
|||
</syntaxhighlight> |
|||
=={{header|langur}}== |
=={{header|langur}}== |
||
User-defined and built-in functions can be called using parentheses. |
User-defined and built-in functions can be called using parentheses. |