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
MFR: a JSL Functional Programming Library for List/Associative Arrays Manipulation
nascif_jmp
Level VI

I developed this helper library while I was working on a large JSL framework. After coding the n-th For() loop to traverse a list (and fixing that many bugs related to unintended clobbering of the index variable) I realized that the code would look a lot cleaner (and be easier to maintain) if I used higher-level abstractions like the ones commonly used in Functional Programming.

The library implements the trio map/filter/reduce, available in languages like Python and JavaScript, plus a few more like apply, reduce and compare. It was inspired by http://book.pythontips.com/en/latest/map_filter.html.

Check the tests for usage examples and let me know if you find a bug or a way to improve it!

Comments
nascif_jmp

Update (8/2019): the new version (MFR2.zip) expands on the original functionality with new commands. This rewrite uses classes instead of namespaces for cleaner code (and to support mixins). Expression manipulation is used instead of passing anonymous functions for the behavior customization (what to do for each container element). The .zip file includes tests and a real application that implements a composite (multi-level key) dictionary, useful to implement caches.

Here is a list of the available methods:

Map: Returns a list of results of the evaluation of the expression after the substitution of input values.
Map2dict: Returns a dictionary (Associative Array) of results of the evaluation of the two expressions after substitution of input values.
Reduce: Returns the accumulation of results of the evaluation of the expression after the substitution of input values.
Filter: Returns the elements in the given list for which the evaluation of the expression after the substitution of input values is true
Any: Returns 1 if any of the elements in the given list when used in the evaluation of the expression results in a true value.
All: Returns 1 if any of the elements in the given list when used in the evaluation of the expression results in a true value.
Zip: Combines two lists of the same length into a single list of pairs of corresponding elements. Not supported/defined for associative arrays
Apply: Applies the expression over the inputs for side effects.
Flat: Takes a nested list and returns a list where all elements are in the same level, keeping the original order

This is great both for what it inherently provides as well as a template for abstraction in JSL. Thank you for posting this. 

nascif_jmp

Thanks, @jcperkins12 . Glad you find it useful.

 
hogi

Since Jmp 16 most of the functionality is now available officially as Filter Each, Transfrom Each ...

very good.

 

Is there also a flatten Function?