Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
97
Task/Call-a-function/Lang/call-a-function-1.lang
Normal file
97
Task/Call-a-function/Lang/call-a-function-1.lang
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue