Subscribe Bookmark RSS Feed

Re: Functional Programming with JSL, Anyone?

ih

Community Trekker

Joined:

Sep 30, 2016

Does anyone write JSL using functional programming, and if so do you have a standard set of functions that you use?

 

As an example, the simplified map function below does all of the work of a for loop so you only need to specify an expression and a list and the function returns a list with the function evaluted for each element. In R, a similar function and variants of it can be found in the package 'purrr' by Lionel Henry and Hadley Wickham.

 

// A function to evaluate an expression for each item in a list
// 
// x__   A list
// fn__  An expression containing ___ (three underscores) that
//       is replaced with each element of x__
//
// Returns a list the same length as x__.
//
map = function( {x__, fn__},
	r__ = {};
	for(i__ = 1, i__ <= N items( x__ ), i__++,
		r__ = Insert( r__, Eval( substitute(Name Expr( fn__ ), Expr(___), Expr( x__[i__] ) ) ) );
	);
	return(r__);
);

//Data to evaluate
vals = { 1, 2, 3, -3, -2, 0.5, -1.1, 5.3, 2.7, 0 };

//number of entries above zero
map( vals, Expr( ___ > 0 ) ); 

//sum of fractional parts
sum( map( vals, Expr( Mod( ___, 1 ) ) ) );

 

 

 

5 REPLIES
Highlighted
stephen_pearson

Community Trekker

Joined:

Oct 6, 2014

I have used this approach, and have functions for generating dynamic journals from a data table, converting a table to nested lists and back again and generating UI dialogue windows.

I've also used JSL to write complex if functions I use a lot (list the conditions,state changes on true and state changes when no longer true (optional)), as I was getting sick of debugging my typos.
ih

Community Trekker

Joined:

Sep 30, 2016

How do you pass the code to be evaluated to your stored if function?  Today I use both expressions or functions but I plan to standardize on one set going forward.

 

An example map function that evalutes another function:

map = function( {x, fn},
	r = {};
	for(i = 1, i <= N items( x ), i++,
		r = Insert( r, fn( x[i] ) );
	);
	return(r);
);

//Anonymous function
map({1, 2, 3}, function({n}, n/2));

//Named function
foo = function( {v}, v * 2 );
map({1, 2, 3}, Expr( foo ) );
stephen_pearson

Community Trekker

Joined:

Oct 6, 2014

The stored if function is a bit of a cheat which is why I listed it second. The function parses the list to generate the if statement and then writes that JSL to a file which is then included in the program.

I tried a function creating the if statements on the fly but the performance hit was massive and I realised the code was pretty static, so was easier to store the generated if function and then use it.
nascif_jmp

Staff

Joined:

Jul 30, 2015

Funny you asked, I have implemented something just like that a few months ago and thought at the time it might be useful to others. I just published it here: https://community.jmp.com/t5/JMP-Scripts/MFR-a-Functional-Programming-Library-for-JSL/ta-p/47803

I hope you find it useful.

BTW I prefer to use anonymous functions over expressions though I had to switch between them depending on the problem I was trying to solve.

ih

Community Trekker

Joined:

Sep 30, 2016

I need to spend some time with your library as these function work a little different than I am used to but this is great and thank you for sharing!  No doubt you just saved me a bunch of time!