Library of functions to be used with ColdFusion Closures.
"Then he thought within himself: "I too will try the virtue of those magical words and see if at my bidding the door will open and close." So he called out aloud, "Open, Sesame!" And no sooner had he spoken than straightway the portal flew open and he entered within."
Sesame is a library of functions for use with closures.
All functions start with an underscore, so as to not collide with already existing ColdFusion or functions that you may have written in your page or component.
It currently has 4 sections:
Functions that allow you manipulate and use structs and arrays much easier
collect data from a array, structure or query into a whole new array. Transform closure should return the item that i to be added to the new array.
collect data from an array, struct or query into a new struct. The transform function can return either a Map, with a single key to be used as the key, and a 2nd level struct, or an array, where the 1st index in the key, and the 2nd is the value.
Take an array/struct and group the data into a grouped struct. The closure should return the key that the data should be grouped by. For arrays, this will return a struct of arrays. For structs, this will return a struct of structures.
Iterates over a query and executes the closure on each row.
Returns an array with all the duplicates removed (i.e. unique). For structs, this iterates through all the values, and returns an array from that, with duplicates removed.
Functions that allow you to manipulate other functions / closures
Curry a function from left to right. Currying is creating a wrapper function where you set a constant value for one or more of the arguments of the function being wrapped. If you curry the function: function (a,b,c)
with values [4,2]
, then your wrapper will accept one argument (c
), and always pass 4 for a
and 2 for b
.
Functions for working with numbers and general looping
Iterates from a given number, up to a given number, using a step increment. Each number is passed through to the closure.
Executes the closure this many times, starting from zero. The current index is passed to the closure each time
Iterates from this number up to the given number, inclusive, incrementing by one each time. Each number is passed through to the closure.
Functions for working with input/output
Read a file and call the closure on each line. Works on either a file ob or path
Functions for working with threads and concurrent programmings
Please Note: To use this concurrency library, there will need to be a mapping to /sesame, or /sesame will need to be in the root of your project, as there are components that need to be instantiated to interact with the Java concurrency libraries.
Do each iteration in it's own thread, and then join it all back again at the end.
Run the closure in a thread. Must be run inside a _withPool() block to set up the ExecutorService, and close it off at the end. For example:
_withPool( 10, function() {
_thread(function() { ... });
_thread(function() { ... });
});
Return an instance of java.util.concurrent.Future to give you control over the closure, and/or retrieve the value returned from the closure.
This function allows you to share the underlying ExecutorService with multiple concurrent methods.
For example, this shares a threadpool of 10 threads across multiple _eachParrallel calls:
_withPool( 10, function() {
_eachParrallel(array, function() { ... });
_eachParrallel(array, function() { ... });
_eachParrallel(array, function() { ... });
});
Please feel free to fork this project and contribute.
Do note that the readme is generated by the generate.cfm
file, which creates the documentation from the function hint meta data.
So if your functions are properly commented, the documentation can be updated automatically.
Therefore, if you want to contribute to the documentation, please do so in the relevent functions comments, so that it ends up in the README.md on regeneration.
Thanks!!!