See two functions below. They lead to the same output [4,6], but are different in set up. Is it correct that only the first function makes use of a callback function? Is the first function preferred(more elegant?) over the other one? And is it correct that 'map' is the higher-order function in the second example as it makes use of the callback function that is within its brackets?
Thanks!
function processArray(arr,callback){
return arr.map(callback)
}
processArray([2,3], function(number){return number*2})
AND
function processArray(arr){
return arr.map(function(element){
return otherFunction(element)})
}
function otherFunction(number){
return number*2}
processArray([2,3])
Is it correct that only the first function makes use of a callback function?
No. arr.map takes a callback. You just don't see its definition here.
Is the first function preferred(more elegant?) over the other one?
Yes, but (probably) not for the reason you think. The anonymous function wrapping otherFunction is pointless. You can also write
function processArray(arr){
return arr.map(otherFunction)
}
function otherFunction(number){
return number*2
}
processArray([2,3])
And is it correct that 'map' is the higher-order function in the second example as it makes use of the callback function that is within its brackets?
In the first example, both processArray and map are higher-order functions. In the second, yes, only map is higher-order.
It's easy to inspect a function to see if it is a higher-order function: is one (or more) of its parameters invoked?
Related
METH_VARARGS requires at least one argument; METH_NOARGS doesn't seem to let me pass any at all.
How can I define a function build() that can be called as either build() or build(True)/build(False)?
Calling a METH_VARARGS function with no arguments results in:
TypeError: function takes exactly 1 argument (0 given)
I was thinking about the problem wrong. It's not the definition, but rather the parsing that rose my TypeError.
To prevent it, I just had to use "|O" instead of "O" in PyArg_ParseTuple!
I got to know recently that Anonymous function
is given in the form of: function body that follows argument Lambda
(=>)
and it has no name
is used once in the code
So, you don't have to add a complete function to do this but rather, you can put the function body in use directly in your code (main function).
I read that in spark, "println" statement is always considered anonymous method for the following reasons:
println statement is considered a function body
Moreover, it wasn't added to a method with a name but was rather
used directly in the main class.
Also, it was used once in the code.
Ex:
Before converting to anonymous
click to view pic1
After converting to anonymous
click to view pic2
However, my question is what if the Lambda function wasn't used.. will println be considered as anonymous function still as shown in the ex below?
main question Ex:
click to view pic3
println is a function, but it is not an anonymous function since it has a name.
There are two ways to call functions in Matlab, the command syntax and function syntax.
I am viewing a code written by someone else in which there's a statement as follows in one .m file:
params=sys_params;
while sys_params is defined as a function in another .m file as:
function params=sys_params()
params happens to be a structure.
What I wish to know is, if according to Matlab documentation, a command syntax cannot be used to output from a function, then how is the first statement working perfectly well?
Two things:
The distinction between command and function syntax comes into play when arguments are passed.
The parentheses for calling a function in MATLAB are optional when calling with no arguments. MATLAB will call the function without an invoking () unlike some other languages.
One exception to this that comes to mind is that () is required to invoke a function handle/anonymous function.
From Calling Functions:
To call a function that does not require any inputs and does not return any outputs, type only the function name
The one ambiguous thing not explicitly told there is that assigning output of such a function call is perfectly valid.
I'll note that I don't really like that () is optional as it hides function calls at-first-glance. Therefore, I try to use () as often as possible to make it clear I am invoking a function, so nearly all of my scripts start with clc();clear();.
I know that this is not what anonymous functions are made for, but just as a puzzle I tried to make a recursive function via anonymous functions. The prototype of recursive functions obviously is the factorial function. The problem is that it is difficult to make a case distinction within the anonymous functions. What I managed to do so far is following:
f=#(cn,n,f)eval('if n>1; f(cn*n,n-1,f);else;ans=cn;end');
f=#(n)f(1,n,f);
Or alternatively:
f=#(cn,n,f)eval('if n>1; f(cn*n,n-1,f);else;disp(cn);end');
f=#(n)f(1,n,f);
What is not very satisfactory is that you still cannot use this function when directly assigning, a=f(3) still produces an error, since eval does not get a value.
So my question is, can you actually do a recursive function via anonymous functions that e.g. calculates factorial in a way that allows e.g. a=f(3) with relying only on native matlab functions (or functions you can create in the command line, as I did in my example)?
PS: I know this does not have any practical use, it is just a challenge on how much you can bend and abuse Matlab's syntax.
We found two possibilites now, both rely on the use of cell arrays. Note that this might not work in Octave.
The key was an implementation of a case distinction. The first one that I found, can be found here.
This method makes use of matlabs boolean values, true can be evaluated as 1 while false can be evaluated as 0.
if_ = #( pred_, cond_ ) cond_{ 2 - pred_ }();
Here we have to provide a condition as first argument, and a 2 element cell array as second argument. Each cell element should be a function handle that is called if the condition is true/not true. Our factorial function would look like this:
fac = #(n,f)if_(n>1,{#()n*f(n-1,f),#()1})
factorial_=#(n)fac(n,fac);
factorial_(10)
As #AndrasDeak commented below: The important part here is that we have a cell array of functions and not of values. This provides the short circuiting, as n*f(n-1,f) is not evaluated unless we call the corresponding function #()n*f(n-1,f).
The second method was found by #beaker and is somewhat more flexible:
iif = #(varargin) varargin{2*find([varargin{1:2:end}], 1, 'first')}();
This makes use of the fact that you can use varargin (variable amount of arguments) even in anonymous functions. When you call this function you have to alternate conditions and what should be executed if the condition is true. This one even allows a switch construct, or a if ... else if ... else if ... (...) else ... construct. When called, it will look for the first condition that is true ( find([varargin{1:2:end}], 1, 'first') ) and call the corresponding function. Our example of the factorial function looks like this:
fac = #(n,f)iif(n>1,#()n * f(n-1,f),true,#()1);
factorial_=#(n)fac(n,fac);
factorial_(10)
EDIT: Fun fact: What we are doing with the line
factorial_=#(n)fac(n,fac);
is also known as applying the Y-combinator. In fact we can write that as
Y = #(f)#(x)f(x,f);
factorial_=Y(f);
I have a function that returns two values, like so:
[a b] = myfunc(x)
Is there a way to get the second return value without using a temporary variable, and without altering the function?
What I'm looking for is something like this:
abs(secondreturnvalue(myfunc(x)))
not that i know of. subsref doesn't seem to work in this case, possibly because the second variable isn't even returned from the function.
since matlab 2009b it is possible to use the notation
[~, b] = function(x)
if you don't need the first argument, but this still uses a temporary variable for b.
Unless there is some pressing need to do this, I would probably advise against it. The clarity of your code will suffer. Storing the outputs in temporary variables and then passing these variables to another function will make your code cleaner, and the different ways you could do this are outlined here: How to elegantly ignore some return values of a MATLAB function?.
However, if you really want or need to do this, the only feasible way I can think of would be to create your own function secondreturnvalue. Here's a more general example called nth_output:
function value = nth_output(N,fcn,varargin)
[value{1:N}] = fcn(varargin{:});
value = value{N};
end
And you would call it by passing as inputs 1) the output argument number you want, 2) a function handle to myfunc, and 3) whatever input arguments you need to pass to myfunc:
abs(nth_output(2,#myfunc,x))