cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
JMP is taking Discovery online, April 16 and 18. Register today and join us for interactive sessions featuring popular presentation topics, networking, and discussions with the experts.
Choose Language Hide Translation Bar
ih
Super User (Alumni) ih
Super User (Alumni)

Functional Programming with JSL, Anyone?

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 ) ) ) );

 

 

 

2 ACCEPTED SOLUTIONS

Accepted Solutions
nascif_jmp
Level VI

Re: Functional Programming with JSL, Anyone?

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.

View solution in original post

ih
Super User (Alumni) ih
Super User (Alumni)

Re: Functional Programming with JSL, Anyone?

In JMP 16 three functions were introduced that facilitate this with native functions. They feel different than other languages but are just as 'functional' (laughing at my own pun):

 

For Each()	     //Do something for each member - similar to apply
Transform Each() //Calculate something for each member - similar to map
Filter Each()    //Return members for which a condition is true - similar to filter

Find more info about these functions in this help file:

https://www.jmp.com/support/help/en/17.1/index.shtml#page/jmp/conditional-and-logical-functions.shtm...

 

View solution in original post

8 REPLIES 8

Re: Functional Programming with JSL, Anyone?

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
Super User (Alumni) ih
Super User (Alumni)

Re: Functional Programming with JSL, Anyone?

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 ) );

Re: Functional Programming with JSL, Anyone?

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
Level VI

Re: Functional Programming with JSL, Anyone?

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
Super User (Alumni) ih
Super User (Alumni)

Re: Functional Programming with JSL, Anyone?

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!

hogi
Level XI

Re: Functional Programming with JSL, Anyone?

In the meantime, is there a map function now directly available in Jmp?

 

Sooo useful!
https://reference.wolfram.com/language/ref/Map.html

ih
Super User (Alumni) ih
Super User (Alumni)

Re: Functional Programming with JSL, Anyone?

In JMP 16 three functions were introduced that facilitate this with native functions. They feel different than other languages but are just as 'functional' (laughing at my own pun):

 

For Each()	     //Do something for each member - similar to apply
Transform Each() //Calculate something for each member - similar to map
Filter Each()    //Return members for which a condition is true - similar to filter

Find more info about these functions in this help file:

https://www.jmp.com/support/help/en/17.1/index.shtml#page/jmp/conditional-and-logical-functions.shtm...

 

hogi
Level XI

Re: Functional Programming with JSL, Anyone?

Many thanks :)

Transform Each.

... similar to Table[] in Mathematica:
https://reference.wolfram.com/language/ref/Table.html