September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -1,13 +1,86 @@
|
|||
:: http://rosettacode.org/wiki/Call_a_function
|
||||
:: Demonstrate the different syntax and semantics provided for calling a function.
|
||||
|
||||
@echo off
|
||||
|
||||
::call a function with no arguments
|
||||
call :myFunction
|
||||
echo Calling myFunction1
|
||||
call:myFunction1
|
||||
echo.
|
||||
|
||||
::call a function with arguments
|
||||
call :myFunction arg1 "arg 2"
|
||||
echo Calling myFunction2 11 8
|
||||
call:myFunction2 11 8
|
||||
echo.
|
||||
|
||||
::initiate a "function".
|
||||
:myFunction
|
||||
echo arg1 - %1
|
||||
echo arg2 - %~2
|
||||
goto :eof
|
||||
echo Calling myFunction3 /fi and saving the output into %%filecount%%
|
||||
call:myFunction3 /fi
|
||||
echo.%filecount%
|
||||
echo.
|
||||
|
||||
echo Calling myFunction4 1 2 3 4 5
|
||||
call:myFunction4 1 2 3 4 5
|
||||
echo.
|
||||
|
||||
echo Calling myFunction5 "filename=test.file" "filepath=C:\Test Directory\"
|
||||
call:myFunction5 "filename=test.file" "filepath=C:\Test Directory\"
|
||||
echo.
|
||||
echo Calling myFunction5 "filepath=C:\Test Directory\" "filename=test.file"
|
||||
call:myFunction5 "filepath=C:\Test Directory\" "filename=test.file"
|
||||
echo.
|
||||
|
||||
pause>nul
|
||||
exit
|
||||
|
||||
:: Requires no arguments
|
||||
:myFunction1
|
||||
echo myFunction1 has been called.
|
||||
goto:eof
|
||||
|
||||
:: Fixed number of arguments (%a% & %b%)
|
||||
:myFunction2
|
||||
:: Returns %a% + %b%
|
||||
setlocal
|
||||
set /a c=%~1+%~2
|
||||
endlocal & echo %c%
|
||||
goto:eof
|
||||
|
||||
:: Optional arguments
|
||||
:myFunction3
|
||||
:: Returns the amount of folders + files in the current directory
|
||||
:: /fi Returns only file count
|
||||
:: /fo Returns only folder count
|
||||
|
||||
setlocal
|
||||
set count=0
|
||||
|
||||
if "%~1"=="" set "command=dir /b"
|
||||
if "%~1"=="/fi" set "command=dir /b /A-d"
|
||||
if "%~1"=="/fo" set "command=dir /b /Ad"
|
||||
|
||||
for /f "usebackq" %%i in (`%command%`) do set /a count+=1
|
||||
|
||||
endlocal & set filecount=%count%
|
||||
goto:eof
|
||||
|
||||
:: Variable number of arguments
|
||||
:myFunction4
|
||||
:: Returns sum of arguments
|
||||
setlocal
|
||||
:myFunction4loop
|
||||
set sum=0
|
||||
for %%i in (%*) do set /a sum+=%%i
|
||||
endlocal & echo %sum%
|
||||
goto:eof
|
||||
|
||||
:: Named Arguments (filepath=[path] & filename=[name])
|
||||
:myFunction5
|
||||
:: Returns the complete path based off the 2 arguments
|
||||
if "%~1"=="" then goto:eof
|
||||
setlocal enabledelayedexpansion
|
||||
set "param=%~1"
|
||||
|
||||
for /l %%i in (1,1,2) do (
|
||||
for /f "tokens=1,2 delims==" %%j in ("!param!") do set %%j=%%k
|
||||
set "param=%~2"
|
||||
)
|
||||
endlocal & echo.%filepath%%filename%
|
||||
goto:eof
|
||||
|
|
|
|||
|
|
@ -1,2 +1,2 @@
|
|||
var c0 := [ console writeLine("No argument provided") ].
|
||||
var c2 := (:a:b)<int,int>[ console printLine("Arguments ",a," and ",b," provided") ].
|
||||
var c0 := (){ console.writeLine("No argument provided") };
|
||||
var c2 := (int a, int b){ console.printLine("Arguments ",a," and ",b," provided") };
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
c0().
|
||||
c0();
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
c2(2,4).
|
||||
c2(2,4);
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
var exch := (:x)<ref<object>>[ x value := 2 ].
|
||||
var a := 1.
|
||||
exch(&a).
|
||||
var exch := (ref object x){ x := 2 };
|
||||
var a := 1;
|
||||
exch(ref a);
|
||||
|
|
|
|||
1
Task/Call-a-function/Groovy/call-a-function-1.groovy
Normal file
1
Task/Call-a-function/Groovy/call-a-function-1.groovy
Normal file
|
|
@ -0,0 +1 @@
|
|||
noArgs()
|
||||
1
Task/Call-a-function/Groovy/call-a-function-10.groovy
Normal file
1
Task/Call-a-function/Groovy/call-a-function-10.groovy
Normal file
|
|
@ -0,0 +1 @@
|
|||
def retVal = func(x, y, z)
|
||||
1
Task/Call-a-function/Groovy/call-a-function-2.groovy
Normal file
1
Task/Call-a-function/Groovy/call-a-function-2.groovy
Normal file
|
|
@ -0,0 +1 @@
|
|||
fixedArgs(1, "Zing", Color.BLUE, ZonedDateTime.now(), true)
|
||||
4
Task/Call-a-function/Groovy/call-a-function-3.groovy
Normal file
4
Task/Call-a-function/Groovy/call-a-function-3.groovy
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
optArgs("It's", "a", "beautiful", "day")
|
||||
optArgs("It's", "a", "beautiful")
|
||||
optArgs("It's", "a")
|
||||
optArgs("It's")
|
||||
4
Task/Call-a-function/Groovy/call-a-function-4.groovy
Normal file
4
Task/Call-a-function/Groovy/call-a-function-4.groovy
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
varArgs("It's", "a", "beautiful", "day")
|
||||
varArgs("It's", "a", "beautiful")
|
||||
varArgs("It's", "a")
|
||||
varArgs("It's")
|
||||
1
Task/Call-a-function/Groovy/call-a-function-5.groovy
Normal file
1
Task/Call-a-function/Groovy/call-a-function-5.groovy
Normal file
|
|
@ -0,0 +1 @@
|
|||
def mean = calcAverage(1.2, 4.5, 3, 8.9, 22, 3)
|
||||
3
Task/Call-a-function/Groovy/call-a-function-6.groovy
Normal file
3
Task/Call-a-function/Groovy/call-a-function-6.groovy
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
def oldFunc = { arg1, arg2 -> arg1 + arg2 }
|
||||
def newFunc = oldFunc.curry(30)
|
||||
assert newFunc(12) == 42
|
||||
1
Task/Call-a-function/Groovy/call-a-function-7.groovy
Normal file
1
Task/Call-a-function/Groovy/call-a-function-7.groovy
Normal file
|
|
@ -0,0 +1 @@
|
|||
def funcList = [func1, func2, func3]
|
||||
2
Task/Call-a-function/Groovy/call-a-function-8.groovy
Normal file
2
Task/Call-a-function/Groovy/call-a-function-8.groovy
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
def eltChangeFunc = { it * 3 - 1 }
|
||||
def changedList = list.collect(eltChangeFunc)
|
||||
6
Task/Call-a-function/Groovy/call-a-function-9.groovy
Normal file
6
Task/Call-a-function/Groovy/call-a-function-9.groovy
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
def funcMaker = { String s, int reps, boolean caps ->
|
||||
caps ? { String transString -> ((transString + s) * reps).toUpperCase() }
|
||||
: { String transString -> (transString + s) * reps }
|
||||
}
|
||||
def func = funcMaker("a", 2, true)
|
||||
assert func("pook") == "POOKAPOOKA"
|
||||
|
|
@ -3,7 +3,6 @@
|
|||
;;; Calling a function that requires no arguments
|
||||
(define (no-args-function)
|
||||
(print "ok."))
|
||||
)
|
||||
|
||||
(no-args-function)
|
||||
; ==> ok.
|
||||
|
|
@ -13,7 +12,6 @@
|
|||
(define (two-args-function a b)
|
||||
(print "a: " a)
|
||||
(print "b: " b))
|
||||
)
|
||||
|
||||
(two-args-function 8 13)
|
||||
; ==> a: 8
|
||||
|
|
@ -29,7 +27,7 @@
|
|||
(print "b: " (car args)))
|
||||
(if (less? 1 (length args))
|
||||
(print "c: " (cadr args)))
|
||||
; etc.
|
||||
; etc...
|
||||
)
|
||||
|
||||
(optional-args-function 3)
|
||||
|
|
@ -53,10 +51,10 @@
|
|||
|
||||
|
||||
;;; Calling a function with named arguments
|
||||
; /no named arguments "from the box" provided, but it can be simulated using builtin maps (named "ff")
|
||||
; /no named arguments "from the box" is provided, but it can be easily simulated using builtin associative arrays (named "ff")
|
||||
(define (named-args-function args)
|
||||
(print "a: " (get args 'a 8))
|
||||
(print "b: " (get args 'b 13))
|
||||
(print "a: " (get args 'a 8)) ; 8 is default value if no variable value given
|
||||
(print "b: " (get args 'b 13)); same as above
|
||||
)
|
||||
|
||||
(named-args-function #empty)
|
||||
|
|
@ -65,10 +63,14 @@
|
|||
(named-args-function (list->ff '((a . 3))))
|
||||
; ==> a: 3
|
||||
; ==> b: 13
|
||||
(named-args-function (list->ff '((b . 7))))
|
||||
; or nicer (and shorter) form available from ol version 2.1
|
||||
(named-args-function '{(a . 3)})
|
||||
; ==> a: 3
|
||||
; ==> b: 13
|
||||
(named-args-function '{(b . 7)})
|
||||
; ==> a: 8
|
||||
; ==> b: 7
|
||||
(named-args-function (list->ff '((a . 3) (b . 7))))
|
||||
(named-args-function '{(a . 3) (b . 7)})
|
||||
; ==> a: 3
|
||||
; ==> b: 7
|
||||
|
||||
|
|
@ -83,6 +85,12 @@
|
|||
(first-class-arg-function - 2 3)
|
||||
; ==> -1
|
||||
|
||||
;;; Using a function in statement context
|
||||
(let ((function (lambda (x) (* x x))))
|
||||
(print (function 4))
|
||||
; ==> 16
|
||||
;(print (function 4))
|
||||
; ==> What is 'function'?
|
||||
|
||||
;;; Obtaining the return value of a function
|
||||
(define (return-value-function)
|
||||
|
|
@ -93,7 +101,15 @@
|
|||
(print result))
|
||||
; ==> ok.
|
||||
; ==> 123
|
||||
; actually
|
||||
|
||||
;;; Obtaining the return value of a function while breaking the function execution (for example infinite loop)
|
||||
(print
|
||||
(call/cc (lambda (return)
|
||||
(let loop ((n 0))
|
||||
(if (eq? n 100)
|
||||
(return (* n n)))
|
||||
(loop (+ n 1))))))) ; this is infinite loop
|
||||
; ==> 10000
|
||||
|
||||
|
||||
;;; Is partial application possible and how
|
||||
|
|
@ -103,17 +119,20 @@
|
|||
)
|
||||
|
||||
(define plus (make-partial-function +))
|
||||
(define minus (make-partial-function -))
|
||||
|
||||
(plus 2 3)
|
||||
; ==> 5
|
||||
(define minus (make-partial-function -))
|
||||
(minus 2 3)
|
||||
; ==> -1
|
||||
|
||||
|
||||
; TBD:
|
||||
;;; Using a function in statement context
|
||||
;;; Using a function in first-class context within an expression
|
||||
;;; Obtaining the return value of a function
|
||||
;;; Distinguishing built-in functions and user-defined functions
|
||||
; ol has no builtin functions but only eight builtin forms: quote, values, lambda, setq, letq, ifeq, either, values-apply.
|
||||
; all other functions is "user-defined", and some of them defined in base library, for example (scheme core) defines if, or, and, zero?, length, append...
|
||||
|
||||
;;; Distinguishing subroutines and functions
|
||||
; Both subroutines and functions is a functions in Ol.
|
||||
; Btw, the "subroutine" has a different meaning in Ol - the special function that executes simultaneously in own context. The intersubroutine messaging mechanism is provided, sure.
|
||||
|
||||
;;; Stating whether arguments are passed by value or by reference
|
||||
; The values in Ol always passed as values and objects always passed as references. If you want to pass an object copy - make a copy by yourself.
|
||||
|
|
|
|||
129
Task/Call-a-function/VBA/call-a-function.vba
Normal file
129
Task/Call-a-function/VBA/call-a-function.vba
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
'definitions/declarations
|
||||
|
||||
'Calling a function that requires no arguments
|
||||
Function no_arguments() As String
|
||||
no_arguments = "ok"
|
||||
End Function
|
||||
|
||||
'Calling a function with a fixed number of arguments
|
||||
Function fixed_number(argument1 As Integer, argument2 As Integer)
|
||||
fixed_number = argument1 + argument2
|
||||
End Function
|
||||
|
||||
'Calling a function with optional arguments
|
||||
Function optional_parameter(Optional argument1 = 1) As Integer
|
||||
'Optional parameters come at the end of the parameter list
|
||||
optional_parameter = argument1
|
||||
End Function
|
||||
|
||||
'Calling a function with a variable number of arguments
|
||||
Function variable_number(arguments As Variant) As Integer
|
||||
variable_number = UBound(arguments)
|
||||
End Function
|
||||
|
||||
'Calling a function with named arguments
|
||||
Function named_arguments(argument1 As Integer, argument2 As Integer) As Integer
|
||||
named_arguments = argument1 + argument2
|
||||
End Function
|
||||
|
||||
'Using a function in statement context
|
||||
Function statement() As String
|
||||
Debug.Print "function called as statement"
|
||||
statement = "ok"
|
||||
End Function
|
||||
|
||||
'Using a function in first-class context within an expression
|
||||
'see call the functions
|
||||
|
||||
'Obtaining the return value of a function
|
||||
Function return_value() As String
|
||||
return_value = "ok"
|
||||
End Function
|
||||
|
||||
'Distinguishing built-in functions and user-defined functions
|
||||
'There is no way to distinguish built-in function and user-defined functions
|
||||
|
||||
'Distinguishing subroutines And functions
|
||||
'subroutines are declared with the reserved word "sub" and have no return value
|
||||
Sub foo()
|
||||
Debug.Print "subroutine",
|
||||
End Sub
|
||||
'functions are declared with the reserved word "function" and can have a return value
|
||||
Function bar() As String
|
||||
bar = "function"
|
||||
End Function
|
||||
|
||||
'Stating whether arguments are passed by value or by reference
|
||||
Function passed_by_value(ByVal s As String) As String
|
||||
s = "written over"
|
||||
passed_by_value = "passed by value"
|
||||
End Function
|
||||
'By default, parameters in VBA are by reference
|
||||
Function passed_by_reference(ByRef s As String) As String
|
||||
s = "written over"
|
||||
passed_by_reference = "passed by reference"
|
||||
End Function
|
||||
|
||||
'Is partial application possible and how
|
||||
'I don't know
|
||||
|
||||
'calling a subroutine with arguments does not require parentheses
|
||||
Sub no_parentheses(myargument As String)
|
||||
Debug.Print myargument,
|
||||
End Sub
|
||||
|
||||
'call the functions
|
||||
Public Sub calling_a_function()
|
||||
'Calling a function that requires no arguments
|
||||
Debug.Print "no arguments", , no_arguments
|
||||
Debug.Print "no arguments", , no_arguments()
|
||||
'Parentheses are not required
|
||||
|
||||
'Calling a function with a fixed number of arguments
|
||||
Debug.Print "fixed_number", , fixed_number(1, 1)
|
||||
|
||||
'Calling a function with optional arguments
|
||||
Debug.Print "optional parameter", optional_parameter
|
||||
Debug.Print "optional parameter", optional_parameter(2)
|
||||
|
||||
'Calling a function with a variable number of arguments
|
||||
Debug.Print "variable number", variable_number([{"hello", "there"}])
|
||||
'The variable number of arguments have to be passed as an array
|
||||
|
||||
'Calling a function with named arguments
|
||||
Debug.Print "named arguments", named_arguments(argument2:=1, argument1:=1)
|
||||
|
||||
'Using a function in statement context
|
||||
statement
|
||||
|
||||
'Using a function in first-class context within an expression
|
||||
s = "no_arguments"
|
||||
Debug.Print "first-class context", Application.Run(s)
|
||||
'A function name can be passed as argument in a string
|
||||
|
||||
'Obtaining the return value of a function
|
||||
returnvalue = return_value
|
||||
Debug.Print "obtained return value", returnvalue
|
||||
|
||||
'Distinguishing built-in functions and user-defined functions
|
||||
|
||||
'Distinguishing subroutines And functions
|
||||
foo
|
||||
Debug.Print , bar
|
||||
|
||||
'Stating whether arguments are passed by value or by reference
|
||||
Dim t As String
|
||||
t = "unaltered"
|
||||
Debug.Print passed_by_value(t), t
|
||||
Debug.Print passed_by_reference(t), t
|
||||
|
||||
'Is partial application possible and how
|
||||
'I don 't know
|
||||
|
||||
'calling a subroutine with arguments does not require parentheses
|
||||
no_parentheses "calling a subroutine"
|
||||
Debug.Print "does not require parentheses"
|
||||
Call no_parentheses("deprecated use")
|
||||
Debug.Print "of parentheses"
|
||||
|
||||
End Sub
|
||||
Loading…
Add table
Add a link
Reference in a new issue