5.0

5.0 | 6 ratings Rate this file 26 Downloads (last 30 days) File Size: 52.2 KB File ID: #39735
image thumbnail

Functional Programming Constructs

by

 

08 Jan 2013 (Updated )

A set of files for treating many operations, like "if", "for", and even "()", as functions.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information
Description

This set of files enables working more easily with functions of functions for concise and powerful code.

A few highlights of this way of working:

* Map multiple inputs to multiple functions and assign multiple outputs with a single line.

* Use conditionals (like "if"), loops, and recursion in anonymous functions.

A document with derivations and examples of these functions is included. See the readme.txt. Also, each function contains a 'help' entry with examples.

This submission requires strong familiarity with function handles and anonymous functions. If those aren't familiar terms, check the documentation first!

Required Products MATLAB
MATLAB release MATLAB 8.0 (R2012b)
Other requirements Strong familiarity with function handles and anonymous functions
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (13)
07 Nov 2013 Felipe G. Nievinski

I guess instead of a separate iiff.m function this could be a patch for the original iif.m (backwards compatible):

isodd = @(x) logical(mod(x,2));
if isodd(numel(varargin))
varargin(end:end+1) = [{true}, varargin(end)];
end

07 Nov 2013 Felipe G. Nievinski

% inline if-else (special case of general inline if-elseif):
% (dispense with typing true as the penultimate input argument)
function varargout = iiff(varargin)
varargin(end:end+1) = [{true}, varargin(end)];
[varargout{1:nargout}] = iif(varargin{:});
end

02 Sep 2013 Felipe

output.m could reuse wrap.m, it seems:
function varargout = output(f, out_indices)
varargout = wrap(f, out_indices);
end

11 Aug 2013 Felipe

Thanks for taking the time to comment, Tucker.

09 Aug 2013 Tucker McClure

Hi Felipe,

You bring up a great point. There's no way to tweak |curly| so that it will expand all of the contents of a cell array as arguments into a function (or at least I haven't figured out a good way), because this is handled higher up by MATLAB, which is only looking for the form |f(x{:})|. Since |f(curly(x, ':'))| doesn't fit this form, it just won't work (the call to |curly(x, ':')| will only return the first output when passed directly into a function.

*However*, there are still ways to do the desired thing, such as your use of the |use| command; they just don't involve using |curly|. For instance, if |f| returns a cell array, and we want to call |f|, expanding all items from its cell array output into a new function, |g|, like this:

>> g(f(){:}) % <- generates a syntax error

we can do this instead:

>> use(f(), @(x) g(x{:}));

This says, "Run f() and pass its output to the next argument, which is a function handle. That function can now use that output (which we've called |x| here) however we want." Because |use| allows one to run a command and "use" its output multiple times without saving it to a variable, it's quickly useful in complex anonymous functions.

If this is a common pattern, you can also define:

>> expand_into_fcn = @(f, x) f(x{:});

And now we can use this expansion function to emulate what MATLAB does natively. E.g., to expand all elements in a cell array returned by |f| into the |cat| function, it's really quite compact:

>> expand_into_fcn(@cat, f())

This is exactly the same as:

>> x = f();
>> cat(x{:})

Let's go a step further. Let's take multiple outputs from a function and pass those into a new function. We'll need |wrap| here. We'll take both outputs from |min([5 2 3])|, |wrap| them into a cell array, and expand that array into the |cat| function to concatenate them along dimension 1.

>> use(wrap(@() min([5 2 3]), 1:2), @(x) cat(1, x{:}))
ans =
2
2

Hope this helps!

09 Aug 2013 Felipe

I'm wondering if it's possible to harmonize the behavior of the curly.m function with that of the built-in {} in this case:
x = {'a' 'b'}, {x{:}}, {curly(x,':')}
x =
'a' 'b'
ans =
'a' 'b'
ans =
'a'
I don't think it's a problem with curly.m, as these anonymous functions also exhibit the same discord:
curlycolon = @(x) x{:}; {curlycolon(x)}
ans =
'a'
curlycolon = @(x) subsref(x, substruct('{}',{':'})); {curlycolon(x)}
ans =
'a'
I seems to be a specificity of the colon operator; from doc colon: "The colon notation can be used with a cell array to produce a comma-separated list. ... The comma-separated list syntax is valid inside () for function calls, [] for concatenation and function return arguments, and inside {} to produce a cell array."

I came across this problem trying to define this function:
subvec2ind = @(siz, subvecnum) use(num2cell(subvecnum), @(subveccell) sub2ind(siz, subveccell{:}));
subvec2ind([5 5 5], [1 2 3])
sub2ind([5 5 5], 1, 2, 3)

07 Apr 2013 na ren

it's helpful!

13 Mar 2013 Tucker McClure

Hey, thanks for all the good feedback everyone. I appreciate your taking the time to leave a rating!

13 Mar 2013 Felipe G. Nievinski  
06 Mar 2013 YONGSUNG

good

13 Feb 2013 Eric Sampson

Great work Tucker! :)

31 Jan 2013 Ian

This is an excellent introduction to functional programming in Matlab, thanks Tucker!!!

10 Jan 2013 Matthew  
Updates
04 Feb 2013

Fixed iif to accept function handles for conditions.

23 Jul 2013

The 'wrap' function returned all output arguments up to the highest selected. It now "wraps up" only the specifically requested arguments. (Thanks Harald.)

Contact us