Location: A review of cardiac cellular electrophysiology models @ 1c3a018574af / dojo-presentation / js / dojo / dojox / lang / functional / listcomp.js

Author:
David Nickerson <david.nickerson@gmail.com>
Date:
2023-04-27 23:14:49-07:00
Desc:
Updates to get things working with OpenCOR 0.7.1
Permanent Source URI:
http://models.cellml.org/workspace/a1/rawfile/1c3a018574af68610e2b95973f32fa831ea3096f/dojo-presentation/js/dojo/dojox/lang/functional/listcomp.js

dojo.provide("dojox.lang.functional.listcomp");

// This module adds high-level functions and related constructs:
//	- list comprehensions similar to JavaScript 1.7

// Notes:
//	- listcomp() produces functions, which after the compilation step are 
//		as fast as regular JS functions (at least theoretically).

(function(){
	var g_re = /\bfor\b|\bif\b/gm;

	var listcomp = function(/*String*/ s){
		var frag = s.split(g_re), act = s.match(g_re),
			head = ["var r = [];"], tail = [];
		for(var i = 0; i < act.length;){
			var a = act[i], f = frag[++i];
			if(a == "for" && !/^\s*\(\s*(;|var)/.test(f)){
				f = f.replace(/^\s*\(/, "(var ");
			}
			head.push(a, f, "{");
			tail.push("}");
		}
		return head.join("") + "r.push(" + frag[0] + ");" + tail.join("") + "return r;";	// String
	};

	dojo.mixin(dojox.lang.functional, {
		buildListcomp: function(/*String*/ s){
			// summary: builds a function from a text snippet, which represents a valid
			//	JS 1.7 list comprehension, returns a string, which represents the function.
			// description: This method returns a textual representation of a function 
			//	built from the list comprehension text snippet (conformant to JS 1.7). 
			//	It is meant to be evaled in the proper context, so local variable can be 
			//	pulled from the environment.
			return "function(){" + listcomp(s) + "}";	// String
		},
		compileListcomp: function(/*String*/ s){
			// summary: builds a function from a text snippet, which represents a valid
			//	JS 1.7 list comprehension, returns a function object.
			// description: This method returns a function built from the list 
			//	comprehension text snippet (conformant to JS 1.7). It is meant to be 
			//	reused several times.
			return new Function([], listcomp(s));	// Function
		},
		listcomp: function(/*String*/ s){
			// summary: executes the list comprehension building an array.
			return (new Function([], listcomp(s)))();	// Array
		}
	});
})();