Call a function: Difference between revisions
Content added Content deleted
(→{{header|Groovy}}: Initial solution) |
|||
Line 1,603: | Line 1,603: | ||
* Partial application is not directly supported. |
* Partial application is not directly supported. |
||
::However something similar can be done, see [[Partial function application#Go]] |
::However something similar can be done, see [[Partial function application#Go]] |
||
=={{header|Groovy}}== |
|||
There are two types of first-class functions in Groovy. |
|||
# The first are functions defined in scripts, although they behave as if they are methods of the script "class". |
|||
# The second are closures, which are similar to lambdas in Java, except that they are defined as their own class type and must be explicitly converted to single-method "functional" interfaces. There are many methods within the Groovy API that accept closures as arguments. |
|||
* Calling a function that requires no arguments |
|||
<lang groovy>noArgs()</lang> |
|||
* Calling a function with a fixed number of arguments |
|||
<lang groovy>fixedArgs(1, "Zing", Color.BLUE, ZonedDateTime.now(), true)</lang> |
|||
* Calling a function with optional arguments |
|||
<lang groovy>optArgs("It's", "a", "beautiful", "day") |
|||
optArgs("It's", "a", "beautiful") |
|||
optArgs("It's", "a") |
|||
optArgs("It's")</lang> |
|||
* Calling a function with a variable number of arguments |
|||
<lang groovy>varArgs("It's", "a", "beautiful", "day") |
|||
varArgs("It's", "a", "beautiful") |
|||
varArgs("It's", "a") |
|||
varArgs("It's")</lang> |
|||
* Calling a function with named arguments |
|||
It's complicated |
|||
* Using a function in statement context |
|||
<lang groovy>def mean = calcAverage(1.2, 4.5, 3, 8.9, 22, 3)</lang> |
|||
* Using a function in first-class context within an expression |
|||
** Create new functions from preexisting functions at run-time |
|||
<lang groovy>def oldFunc = { arg1, arg2 -> arg1 + arg2 } |
|||
def newFunc = oldFunc.curry(30) |
|||
assert newFunc(12) == 42</lang> |
|||
** Store functions in collections |
|||
<lang groovy>def funcList = [func1, func2, func3]</lang> |
|||
** Use functions as arguments to other functions |
|||
<lang groovy>def eltChangeFunc = { it * 3 - 1 }</lang> |
|||
def changedList = list.collect(eltChangeFunc) |
|||
** Use functions as return values of other functions |
|||
<lang groovy>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"</lang> |
|||
* Obtaining the return value of a function |
|||
<lang groovy>def retVal = func(x, y, z)</lang> |
|||
* Distinguishing built-in functions and user-defined functions |
|||
There are no "built-in" functions. All is illusion. |
|||
* Stating whether arguments are passed by value or by reference |
|||
As with Java everything is passed by value, but object values are actually references (pointers). So, |
|||
** if the argument is a primative it is passed by value and changes are not manifested in the caller's context. |
|||
** if the argument is an object reference, the reference is passed by value and changes to the reference (re-assignment, for example) are not manifested in the caller's context, but changes in the object are. |
|||
* Is partial application possible and how |
|||
Partial application in Groovy is performed via currying (demonstrated above) |
|||
=={{header|Haskell}}== |
=={{header|Haskell}}== |