StructSome

Description

Determines if at least one value of a key-value pair in a struct satisfies a given condition.

Returns

True if at least one value matches a condition; false, otherwise.

Syntax

structSome(struct, function(key, value [,struct]){} [, parallel] [, maxThreads])

Member function

structObj.Some(callback)

History

ColdFusion (2021 release): Introduced the following parameters:

  • parallel
  • maxThreadCount

ColdFusion (2018 release) Update 5: Added this function.

Parameters

Parameter

Required/Optional

Description

struct

Required

Struct in which at least one value is to be searched.

callback

Required

Function that encapsulates the criteria.

parallel

Optional

(Boolean)- True if you want to enable parallel programming.

maxThreads

Optional

(Int) 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

Example 1

<cfscript>
 mystruct = {a=1,b=2,c=3};
 doesValExist=(key,value,struct)=>return value==1
 writeoutput(structSome(mystruct,doesValExist))
</cfscript>

Output

YES

Example 2

<cfscript> 
 structEven={a=2,b=4,c=8,d=10,e=12} 
 isEven=(key,value)=>value%2==0 
 writeOutput(StructSome(structEven,isEven)) // Returns True 
</cfscript>

Output

YES

Member function

<cfscript> 
 structEven={a=2,b=4,c=8,d=10,e=12} 
 isEven=(key,value)=>return value%2==0 
 writeOutput(structEven.Some(isEven)) // Returns True 
</cfscript>

Member function - Using arrow operator

<cfscript> 
 ordstruct=["key1":"abc","key2":"def","key3":"sss"] 
    lambdaFunc= key => key.contains("key") 
    writeOutput(ordstruct.Some(lambdaFunc)) 
</cfscript>

Exampe 3 - Named parameters

<cfscript> 
 mystruct={"key1":"aval","key2":"bval","key3":"cval"} 
 result=StructSome(struct=mystruct,callback=function(key,val){ 
  if (key.contains("key1") && val.startswith("a")) 
              return 1 
  else 
              return 0 
   } 
 ) 
 writeOutput(result) 
</cfscript>

Output

YES

Using parallelization

<cfscript> 
mystruct= Structnew("ordered"); 
for(i=1;i<=10000;i++){ 
mystruct.insert("key#i#","val#i#") 
 
} 
function callback(key,val){ 
if(isObject(val)) 
 return true 
return false 
 } 
   
 t_start=GetTickCount() 
 writeoutput(mystruct.some(callback)) 
 t_end=GetTickCount() 
 writeoutput("<br>Time taken with no parallel:" &  t_end-t_start) 
 
 t_start=GetTickCount() 
 writeoutput(mystruct.some(callback,true,5)); 
 t_end=GetTickCount() 
 writeoutput("<br>Time taken with 5 threads:" &  t_end-t_start) 
  
  
 t_start=GetTickCount() 
 writeoutput(mystruct.some(callback,true,10)) 
 t_end=GetTickCount() 
 writeoutput("<br>Time taken with 10 threads:" &  t_end-t_start) 
  
 t_start=GetTickCount() 
 writeoutput(structsome(struct=mystruct,callback=callback,parallel=true,maxthreadcount=20)) 
 t_end=GetTickCount() 
 writeoutput("<br>Time taken with 20 threads:" &  t_end-t_start) 
  
 t_start=GetTickCount() 
 writeoutput(structsome(struct=mystruct,callback=callback,parallel=true,maxthreadcount=40)) 
 t_end=GetTickCount() 
 writeoutput("<br>Time taken with 40 threads:" &  t_end-t_start) 
  
</cfscript>

 Adobe

Get help faster and easier

New user?