Recently a short idea on how function closures can be build in R is shown [link]. It is also discussed in detail by Darren Wilkinson's blog [link]. Here we will mimic the approach presented there. Recall that function
closures are basically a reference to a function with the attachment of
its lexical environment. Note that, it is not a function pointer
while it also return the values of its arguments if they are defined in
the given scope. This sort of language construction can be very handy
when some of the inner details need to be parametrized, so to speak to
prevent re-writing the function over and over again.

A trivial example of adding a number to a given number can be formulated. Let's say x is a number and we need to add 4 to x.

function add4(x)

x+4

end

So
far so good, but do you really want to re-write an other function to
add 7. Not really! So instead we can generate a function that generates
addition function for 7 (Sounds like an onion.). This is quite standard
in functional languages and noting new actually!

>> addx = @(x) @(y) x + y ;

>> add4=addx(4);

>> add7=addx(7);

>> add4(5)

9

>> add7(5)

12

So function add4 and add7 are generated by the function addx. We just utilized function handles.
This might be quite trivial. Let's try the technique in a little more
realistic setting. Imagine we need to take a median of each vector,
which has different sizes stored in a structure. Let's generate one

>> rng(0.42,'v4'); % reproduce numbers

>> myStr.vec1 = rand(11,1);

>> myStr.vec2 = rand(5,1);

>> myStr.vec3 = rand(20,1);

>> myStr.vec4 = rand(8,1);

So a function that takes a structure like this and returns the median value of the named member

>> medianVec= @(str, memName) median(str.(memName));

For example for vec1

>> medianVec(myStr, 'vec1');

0.5194

If we need this procedure for vec1 repetitively, we don't need to re-write the second argument all the time. So

>> medianVecN = @(memName) @(str) median(str.(memName));

>> medianVec1 = medianVecN('vec1');

>> medianVec1(myStr); % Vala!

0.5194

This might still look trivial but hopefully it would give you an idea of the technique.

## No comments:

Post a comment