StructMap

Note:

You can also set the maximum thread count in ColdFusion Administrator. Click Server Settings > Settings and specify the number of threads in Default Maximum Thread Count For Parallel Functions.

Description

Iterates over every entry of the Struct and calls the closure function to work on the key value pair of the struct. The returned value will be set for the same key in a new struct and the new struct will be returned.

Returns

Struct

Syntax

structMap(struct, function(key, value [,struct]){} [, parallel] [, maxThreadCount])

History

ColdFusion (2021 release): Introduced the following parameters:

  • parallel
  • maxThreadCount

ColdFusion 11: Added this function.

Parameters

Parameter

Req/Opt

Description

struct

Required

The input struct.

function

Required

Closure or a function reference that will be called for each of the iteration. The arguments passed to the callback are

  • key: key for the current iteration
  • value: the value for the current iteration
  • struct: a reference of the original struct

parallel

Optional

True if you want to enable parallel programming.

maxThreadCount

Optional

The number of threads the function can execute. The number of threads must be between 1-50. If the value exceeds 50, there is an exception.

Example

<cfscript>
       myStruct=StructNew();
       myStruct = {a:1,b=2,c=3,d=4,e=5,f=6};
       WriteOutput("The input struct is:");
       WriteDump(myStruct);
       myMap=StructMap(myStruct,function(key,value){
             return #value#^2;// Return squared values of struct members
       });
       WriteOutput("The output struct is:");
       WriteDump(myMap); // Writes a new struct with squared values
</cfscript>

Output

StructMap output
StructMap output

Using member function

<cfscript>
       myStruct = {a:1,b=2,c=3,d=4,e=5,f=6};
       myNewMap=myStruct.map(function(key,value){
             return value*5;
       });
       WriteDump(myNewMap);
</cfscript>

Using parallelization

<cfscript> 
mystruct= Structnew("ordered"); 
for(i=1;i<=10000;i++){ 
mystruct.insert("key#i#","val#i#") 
 
} 
function callback(key,val){ 
return val = val & "struct123" 
 } 
   
 
 s = mystruct.map(callback,false) 
 writeoutput(s['key1'] & "<br>") 
 s = mystruct.map(callback,true); 
 writeoutput(s['key1']  & "<br>") 
  s= mystruct.map(callback,true,10) 
 writeoutput(s['key1']  & "<br>") 
  s= structmap(struct=mystruct,callback=callback,parallel=true,maxthreadcount=20) 
 writeoutput(s['key1']  & "<br>") 
 s = structmap(struct=mystruct,callback=callback,parallel=true,maxthreadcount=40) 
 writeoutput(s['key1']  & "<br>") 
 try { 
 structmap(struct=mystruct,callback=callback,parallel=true,maxthreadcount=-20) 
 } 
 catch(any e){ 
  writeoutput("<br>Type: " & e.type & " Message:" &  e.message ) 
} 
 try{ 
 structmap(struct=mystruct,callback=callback,parallel=true,maxthreadcount=200) 
 } 
 catch(any e){ 
  writeoutput("<br>Type: " & e.type & " Message:" &  e.message ) 
} 
  
</cfscript>
Adobe logo

Sign in to your account