Utility for carving up collections (arrays and structs)
ColdFusion
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.gitignore
Balisong.cfc
BalisongTest.cfc
README.textile
example.cfm

README.textile

Balisong: Map, Reduce, Fold, and banana slicer for ColdFusion 9

Simple functional approach for recursively applying a given function to each element of a collection (array, list, or struct). One common view is to think of passing a function to some data, rather than passing the data to a function. Example (included in source):

 
/**
* Recursively increments each numeric value in the collection (array,list,or struct). 
*/
public function inc(any collection){
  return balisong.apply(_inc,collection);
}
...

/**
* The function to apply to each element of the collection. If the value is not numeric, 
* inc() just returns the value. Note that in languages that support lambda functions ,
* the definition of the function is frequently specified in-line with the call to apply. However,  
* with ColdFusion, you need to write an _external_ function. This is not necessarily a bad thing,
* as it allows the function being applied to the data to be tested directly.
*/
public function _inc(any val){
 if( isNumeric(val) ) return ++val;
 return val;
}

...

//Usage:
function incrementEachNumericInAnArray(){
	var original = [ 1,2,3 ];
	var new_data = mr.inc(a);
	assert( new_data[1] ==2 && new_data[2] ==3 && new_data[3] ==4  );
}


Goals

  1. Functions do not alter any arguments
  2. Functions return new objects (see #1)
  3. Functions act recursively on all collections
  4. Functions do not depend on instance data and can be wired or mixed in
  5. Functions use as few possible local variables in functions to minimize use of state

Known Issues

  1. Ordering of resultant new collections is non-deterministic
  2. Lists are still returned as an array
  3. Some functions that could work on structs, too, still only work on arrays

Pull requests welcome!

Function summary (alphabetical):

See the Balisong Wiki Page for examples

[boolean] all(string predicate, any collection)

Returns true if every element in the collection satisfies the predicate.


[boolean] any(string predicate, any collection)

Returns true if any element in the collection satisfies the predicate.


[array|struct] apply(any a_function, any collection)

Applies a_function to every element in the collection and returns a new collection


[boolean] exists(string predicate, any collection)

Returns true if at least one element in the collection satisfies the predicate


[numeric] count(string predicate, any collection)

Returns the number of elements in this collection that satisfies the predicate


[void] foreach(any a_function, any collection)

Executes a_function for every element in the collection. Does not return a value. WARNING: This can mutate the original collections argument.


[numeric] foldLeft(array a, string operator, numeric start)

Folds all elements of this array starting at the left into a new array using operator (+ or *) and start as the initial value


[numeric] foldRight(array a, string operator, numeric start)

Folds all elements of this array starting at the right into a new array using operator (+ or *) and start as the initial value


[numeric] reduceLeft(array a, string operator, numeric start)

Reduces all elements of this array starting at the left into a new array using operator (+ or *) and start as the initial value


[numeric] reduceRight(array a, string operator, numeric start)

Reduces all elements of this array starting at the right into a new array using operator (+ or *) and start as the initial value


[array] flatten(array collection)

Flattens all elements of this array into a new 1 dimensional array


[array|struct] concat(any a1, any a2, ..any aN)

Concatenates N collections into a single collection (arrays and structs)


[array|struct] filter(string predicate, any collection)

Creates new collection based on all elements in the collection that satisfy the predicate