Call a function: Difference between revisions

→‎{{header|jq}}: paragraph breaks
(jq)
(→‎{{header|jq}}: paragraph breaks)
Line 649:
 
* They are like commands in modern operating systems, in that they are <tt>parameterized filters</tt> that can accept input from the previous command and provide output to the next command if there is one in the pipeline of commands.
 
* Functions can not only process a stream of inputs, one at a time, but each argument can also accept a stream of inputs. The outputs are then a Cartesian product of the various inputs.
 
Line 654 ⟶ 655:
 
'''Calling a function that requires no arguments'''
 
0-arity jq functions are invoked simply by specifying their name.
* Example: .
(Yes, "." is a 0-arity jq function.)
 
'''Calling a function with a fixed number of arguments'''
 
The conventional syntax is used except that ";" is the parameter separator. "," is used to construct a stream of values.
* Example: range(0;100;2)
 
'''Calling a function with optional arguments'''
 
Recent versions of jq allow one to define functions with the same name but different arities, and therefore if both fn/1 and fn/2 are defined, we may say that fn requires one parameter but accepts 2. In all cases, the syntax for function invocation is the same.
* Example: range(0; 10) and range(0; 10; 2)
 
Since jq functions can accept JSON arrays and objects, there are other ways to simulate optional arguments.
 
'''Calling a function with a variable number of arguments'''
 
See above.
 
'''Calling a function with named arguments'''
 
This is not directly supported but can be simulated by defining the function to accept JSON objects. For example, if fn were such a function,
we might invoke fn like so: <tt>fn( {"required": 1, "optional" 2} )</tt>.
'''Using a function in statement context'''
 
The assignment to a local variable (e.g. <tt>(2*2) as $two<tt>) is similar to a statement context in that the expression as a whole does nothing to the flow of values from its input to its output.
 
'''Using a function in first-class context within an expression'''
 
jq functions cannot be assigned to variables but are otherwise "first-class" in that the composition of functions can be passed as arguments to other functions. No special syntax is required.
* Example: <tt>2 | recurse(. * .)</tt> # generate the sequence 2, 4, 16, 256, ...
 
'''Obtaining the return value of a function'''
 
The value (or stream of values) returned by a function is (or are) automatically available to the next function in the pipeline (e.g. sin | cos); the returned value(s) can also be assigned to a local variable (e.g. sin as $v).
 
'''Distinguishing built-in functions and user-defined functions'''
 
Currently there is no such distinction, but user-defined functions always have the conventional form, whereas some built-in functions have special syntax.
 
'''Distinguishing subroutines and functions'''
 
A jq function can be written so as never to return anything (either because it returns without generating a value or because it generates an infinite stream of values), but there is no distinctive marker associated with such functions.
 
'''Stating whether arguments are passed by value or by reference'''
 
Arguments are in effect passed by value.
 
'''Is partial application possible and how'''
 
See [[Currying#jq]].
 
=={{header|LFE}}==
2,442

edits