StructEach

Used to iterate over a struct and run the function closure for each key/value pair.

The function will be passed 3 arguments: the key, the value, and the struct. You can alternatively pass a Java BiConsumer which will only receive the first 2 args (key and value).

This BIF is useful for performing side effects on each item in the struct, such as logging or modifying external state.

Parallel Execution

If the parallel argument is set to true, and no max_threads are sent, the filter will be executed in parallel using a ForkJoinPool with parallel streams. If max_threads is specified, it will create a new ForkJoinPool with the specified number of threads to run the filter in parallel, and destroy it after the operation is complete. Please note that this may not be the most efficient way to iterate, as it will create a new ForkJoinPool for each invocation of the BIF. You may want to consider using a shared ForkJoinPool for better performance.

Method Signature

StructEach(struct=[structloose], callback=[function:BiConsumer], parallel=[boolean], maxThreads=[integer], ordered=[boolean])

Arguments

Argument
Type
Required
Description
Default

struct

struct

true

The target struct to iterate

callback

function:BiConsumer

true

The function to invoke for each item. The function will be passed 3 arguments: the key, the value, the struct. You can alternatively pass a Java BiConsumer which will only receive the first 2 args.

parallel

boolean

false

Whether to run the filter in parallel. Defaults to false. If true, the filter will be run in parallel using a ForkJoinPool.

false

maxThreads

integer

false

The maximum number of threads to use when running the filter in parallel. If not passed it will use the default number of threads for the ForkJoinPool. If parallel is false, this argument is ignored.

ordered

boolean

false

(BoxLang only) whether parallel operations should execute and maintain order

false

Examples

structEach() with an inline function (closure)

Use a function to write out the keys in a structure to the screen

Run Example

someStruct = { 
	A : 1,
	B : 2,
	C : 3
};
structEach( someStruct, ( Any key, Any value ) => {
	writeOutput( "Key " & key & " is " & value & "; " );
} );

Result: Key a is 1; Key b is 2; Key c is 3;

Using a function reference

Run Example

favs = { 
	COLOR : "blue",
	FOOD : "pizza",
	SPORT : "basketball"
};

// named function
// notice that the function takes two arguments, the key and value pair of the current iteration of the structure's key-value pairs
function getFavorites( Any key, Any value ) {
	writeOutput( "My favorite " & key & " is " & value );
}
// run structEach() with a named function
structEach( favs, getFavorites );
// run structEach() with an inline function
structEach( favs, ( Any key, Any value ) => {
	writeOutput( "My favorite " & key & " is " & value );
} );

Using the member function

Run Example

statusCodes = { 
	OK : 200,
	CREATED : 201,
	NOT_MODIFIED : 304,
	BAD_REQUEST : 400,
	NOT_FOUND : 404
};
statusCodes.each( ( Any key, Any value ) => {
	writeOutput( "#key# => #value#<br />" );
} );

Result: NOT_FOUND => 404 BAD_REQUEST => 400 CREATED => 201 OK => 200 NOT_MODIFIED => 304

Accessing a reference to the looping struct in the callback

Run Example

statusCodes = { 
	OK : 200,
	CREATED : 201,
	NOT_MODIFIED : 304,
	BAD_REQUEST : 400,
	NOT_FOUND : 404
};
statusCodes.each( ( Any key, Any value, Any struct ) => {
	writeDump( struct );
} );

Additional Examples

Run Example

animals = { 
	COW : "moo",
	PIG : "oink",
	CAT : "meow"
};
StructEach( animals, ( Any key ) => {
	// Show key 'arguments.key'
	Dump( label="Key", var=arguments.KEY );
	// Show key's value 'animals[arguments.key]'
	Dump( label=arguments.KEY & "'s value", var=animals[ arguments.KEY ] );
} );

Last updated

Was this helpful?