de Rigo, D. (2012). Merging functional paradigm ideas with semantic array programming: the module "func_let" of the Mastrave modelling library. In: Semantic Array Programming with Mastrave - Introduction to Semantic Computational Modelling. http://mastrave.org/doc/mtv_m/func_let
Merging functional paradigm ideas with semantic array programming: the module "func_let" of the Mastrave modelling library
Copyright and license notice of the function func_let
Copyright © 2009,2010,2011,2012 Daniele de Rigo
The file func_let.m is part of Mastrave.
Mastrave is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Mastrave is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Mastrave. If not, see http://www.gnu.org/licenses/.
Function declaration
continuation = func_let( func , continuation = [] )
Description
Module for supporting functional semantic array programming and in particular continuation-passing style with optional semantic constraints (planned).
Data parallelism is inherently present in several large-scale (e.g. regional, continental scale) applications of environmental modelling. At large scale, environmental data are usually available as geographically referenced collections of atomic information.
For example, spatial raster layers are matrices of values each element of them referring to a certain spatial location. Also complex data such as time series of spatially distributed measures are often suitable for conceptually compact data-transformations, despite often those transformations are nontrivial to implement.
Multi-dimensional arrays, heterogeneous collections of uneven arrays might typically benefit from conciseness and reliability of semantic array programming as implemented by the Mastrave modelling library.
This module allows algorithms composed by trees of sub-algorithms to be expressed even more concisely by means of extended anonymous functions which can support internal array-based state transitions along with array continuation-passing style. This style is an array-based design pattern which is inspired by the classic one originated from the functional programming paradigm.
Functional semantic array programming codelets for array data-transformations can be assigned to function handles and also nestified for implementig complex functional behaviours.
Input arguments
func ::cellfunhandle:: Semantic function handle of cell-array of semantic function handles. Semantic function handles are not intended to be invokable function handles. Instead, they are meant as a concise way for expressing semantically enhanced relationships between variables and expressions, also suitable to be parsed and interpreted. Each semantic function handle must follow a schema, the easiest instantiation of which is: @( varname ) is ( expression ) which simply represents an assignment: varname = expression varname can be an arbitrary valid variable name, except for reserved names (i.e. variable names beginning with "_mtv_" and ending with "_" ) which are not allowed. expression can be an arbitrary valid expression possibly involving previously assigned variables. Alternatively, the operator "is" could be replaced with the equivalent operator "_" . Multiple assignments can be denoted as mutually independent so that they could in principle be executed in parallel (i.e. the order of execution of the assignments should not matter): @( var1 , var2 ) is ( expr1 , expr2 ) Dynamic assignments (i.e. execution-order dependent assignments) can be denoted with consecutive semantic function handles: { @( var1 ) is ( expr1 ), @( var2 ) is ( expr2 ) } Multiple output arguments of a given function can be assigned by using the notation: @( var1 , var2 ) are ( function_handle ) whose non-functional version would be: [ var1 , var2 ] = function_handle () continuation ::struct:: Structure whose fields represent variables in the form: continuation. var_name = var_value e.g. generated by using @vars2struct . If passed as empty array [] , it is converted in an empty struct. If omitted, its default value is [] .
Example of usage
% Basic usage c = func_let( @(x) is ( 3 ) ) c = func_let( @(x,y) is ( 3, pi ) ) c = func_let( @(x,y,foo) is ( 3, pi, 'bar' ) ) % Managing multiple output arguments of a function % Equivalent to: % maxval = max([0 pi -2 1]) c = func_let( @(maxval) is ( max([0 pi -2 1]) ) ) c = func_let( @(maxval) are ( @()max([0 pi -2 1]) ) ) % Equivalent to: % [ maxval, pos ] = max([0 pi -2 1]) c = func_let( @(maxval,pos) are ( @()max([0 pi -2 1]) ) ) % Wrong formats c = func_let( @(x,y) func( 3, pi ) ) c = func_let( @(x,y) is + 2 ) c = func_let( @(x,y) is ( 3, pi )+2 ) % Passing multiple assignments c = func_let( {@(x)is( 1+1), @(y)is(x*10) } ) % Using a better coding style c = func_let( { @(x)_( [1:5] + 1 ) @(y)_( x * 10 ) } ) % Passing a reified variable state (continuation-passing style) c2 = func_let( { @(z)_( y(end:-1:1) * 10 ) } ,c ) % Motivational example % Plotting remote datasets using an anonymous function plt = @(data1,data2)func_let( { @(grd )is( @get_reference_data ) @(d1,d2 )is( grd( data1 ), grd( data2 ) ) @(y )is( [ d1(:,1), d2(:,1) ] ) @(ret )is( mplot_vline( y ) ) }, vars2struct ); c12 = plt( 'Gauss1' , 'Gauss2' ); c23 = plt( 'Gauss2' , 'Gauss3' );
See also: mstream, mblk_fun Keywords: continuation-passing, functional, anonymous-function Version: 0.4.5
Support
The Mastrave modelling library is committed to provide reusable and general - but also robust and scalable - modules for research modellers dealing with computational science. You can help the Mastrave project by providing feedbacks on unexpected behaviours of this module. Despite all efforts, all of us - either developers or users - (should) know that errors are unavoidable. However, the free software paradigm successfully highlights that scientific knowledge freedom also implies an impressive opportunity for collectively evolve the tools and ideas upon which our daily work is based. Reporting a problem that you found using Mastrave may help the developer team to find a possible bug. Please, be aware that Mastrave is entirely based on voluntary efforts: in order for your help to be as effective as possible, please read carefully the section on reporting problems. Thank you for your collaboration.