Skip to content

Instantly share code, notes, and snippets.

@ejhayes
Created July 14, 2010 23:59
Show Gist options
  • Save ejhayes/476296 to your computer and use it in GitHub Desktop.
Save ejhayes/476296 to your computer and use it in GitHub Desktop.
<cfscript>
/*
* This function performs a map operation similar to that seen in functional programming:
* Arguments can be specified as follows:
* - Arrays: [arg1,arg2,arg3]
* - Structures: {"arg1"="value1","arg3"="value3"}
* - Singletons: "arg1"
*/
function map(required any func, required any args){
var ret = ArrayNew(1);
var index=1;
var isFunction = isSimpleValue(arguments.func) AND ListFindNoCase(StructKeyList(GetFunctionList()),arguments.func);
for(; ;){
if(index GT ArrayLen(args)) break;
if( isSimpleValue(arguments.args[index]) ){
ArrayAppend(ret, isFunction ? evaluate("#arguments.func#(arguments.args[index])") : arguments.func(arguments.args[index]));
} else if( isStruct(arguments.args[index]) ){
ArrayAppend(ret, isFunction ? evaluate("#arguments.func#(args[index])") : arguments.func(argumentCollection=args[index]));
} else if( isArray(arguments.args[index]) ){
index1=1;
a = "";
for(; ;){
if(index1 GT ArrayLen(args[index])) break;
if(isSimpleValue(args[index][index1])){
a += args[index][index1]
} else {
}
evaluate("arguments.func("")"));
index1++;
}
} else {
// User tried to do something bad!
Throw(type="BadFunction",message="Only system functions may be passed as strings.");
}
index++;
}
return ret;
}
w="string";
a=ArrayNew(1);
//a[1]='hey';
//a[2]=[w,1.04];
a[1]=[{"d"="glee"},{"c"="me"},"tofu"];
//WriteDump(map("ucase",map(y,a)));
WriteDump(map(y,a));
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment