Writing my presentation for cf.Objective(), Getting closure on Closures, I found I was writing a few common utility functions for use with Closures to use and show in my talk, and as general utilities for coding.
I wanted to bundle these functions into a project, and put it on Github so that we could all share various utility functions that would be useful now that ColdFusion has got Closures available to us. I went looking for an existing project that was already doing this, but since I couldn't find one, I figured I would start one, and see what happens from there!
The project's name is Sesame (much thanks to Ben Koshy for the name), and currently only has a few functions which can be cfincluded into your code, but they were the ones I found most useful when doing some recent work with Groovy, and wanted to have them on hand when doing similar things in ColdFusion.
Here are a few examples:
From the collections include, the _collect functions allows you collect values from one array or struct, by iterating a closure over the passed in collection, and collecting the results returned from that closure.
For example, if I wanted to get all the names from an array of Employees, I could do the following:
var employees = listEmployees();
var names = _collect(employees, function(employee) { return employee.getName(); } );
This would then give me an array of all the employees name in the list.
My other favourite, is _groupBy(), which gives you an easy way to group an array or struct of items into a structure of items that have been grouped by a value returned from a closure.
For example, I could easily group an array of employees by their salary, like so:
var employees = listEmployees();
var grouped = _groupBy(employees, function(employee)
{
if(employee.getSalary() < 100)
{ return "Less Than 100"; }
else if(employee.getSalary() > 100 && employee.getSalary() < 200)
{ return "Greater than 100, Less than 200"; }
else
{ return "Greater than 200"; }
});
From there, I can now get at all the employees that earn less than 100, by referenceing them as grouped["Less Than 100"], and so on.
From the numbers include, I really like the _times() function. Simply put, it executes a closure a certain number of times!
_times(6, function() { writeOutput("Write me 6 times!"); });
Nothing too complicated there, but it can be quite useful.
From the concurrency include, there is a function that was directly inspired by the GPars Groovy library (and awesome concurrency library for Groovy). _eachParrallel() will execute the closure for each item in an array or struct, but execute the closure in its own cfthread implementation, allowing for an easy way to parallel process data. When it is done, it joins all the threads back up again, so there is no need for you to do deal with managing the threads yourself.
For example, summing up each item in an array, each in it's own thread:
var values = [1, 2, 3, 4, 5];
var total = 0;
_eachParallel(values, function(it) { total += it; });
Gives us a total of 15.
If you find this interesting, please feel free to download, use and also contribute. I know there are a million more functions that could yet be implemented, and I've already implemented the automatic documentation generator - so as long as your functions have appropriate hinting, it becomes super easy to regenerate the README.md file! So no hard work there.
I'll also be showing this library off as part of my cf.Objective() talk on Closures - so please pop by if you want to see more about closures in ColdFusion 10, or just about closures in general.
A couple of new items in this release of JavaLoader:
- A few bug fixes in the NetworkClassLoader that allowed the Tika library to work.
- A new function for switching out the ThreadContextClassLoader.
The new function, swithThreadContextClassLoader() is useful as it is often required when dealing with libraries, such as dom4j, that use the ClassLoader to define singletons or search for implementing classes, and don't allow you to overwrite what ClassLoader they use. Check out the wiki for more details on why this is neccessary, and how easy it is to now do, thanks to our new function!
Also, JavaLoader has been move to GitHub, along with the all the documentation. This should make collaboration much easier moving forward!
Otherwise, make sure to sign up to the mailing list, and enjoy loading your Java.
I'm delighted to say that I'm speaking at cf.Objective() again this year!
I've got three sessions I'm presenting this year, two of which on my own, and one in which I'm paired with with Luis Majano.
I'll be talking about:
A/B Testing with Squabble
In which I extol the virtue of A/B testing, and why you should be doing it in your business, and introduce people to the A/B testing framework Squabble that we developed to use in the team I work with.
Getting closure on Closures
Where we will look at Closures, which are coming in ColdFusion Zeus. You probably already use these, and don't even know it. I'll explain what closures are, and how you can use them in simple ways, as well as more interesting use cases as well.
AOP Demystified!
This is the session I will be doing with Luis Majano, where we will talk about Aspect Oriented Programming, and give you examples with the two primary AOP frameworks - ColdSpring and Wirebox. Should be good stuff!
All up, it looks like it's shaping up to be a great conference program, so I look forward to seeing everyone there!
One of the most powerful new features in Coldspring 2 is the ability to create custom xml schemas that
can be used within your XML configuration.
Rather than go into the details of how to write and configure custom schemas, here is an example of one that comes
bundled with CS2 out of the box.
In Coldspring 1.x, if you ever wanted to create a standalone structure inside your XML configuration, you would quite
commonly create it through a MapFactoryBean like
so:
<bean id="myMap" class="coldspring.beans.factory.config.MapFactoryBean">
<property name="sourceMap">
<map>
<entry key="keyA" value="keyA value"/>
<entry key="keyB" value="keyB value"/>
<entry key="keyC" value="keyC value"/>
</map>
</property>
</bean>
This is all well and good, but the issue here is that you specifically need to know the api of the MapFactoryBean.
If you get any part if the configuration of the MapFactoryBean wrong, you tend to get errors that may not be
clear, or potentially no error at all, simply a result that is not desirable.
For example, if you misspell the sourceMap property, Coldspring will attempt to set the property, but it doesn't
throw an error when it fails.
<bean id="myMap" class="coldspring.beans.factory.config.MapFactoryBean">
<property name="sourcMap">
<map>
<entry key="keyA" value="keyA value"/>
<entry key="keyB" value="keyB value"/>
<entry key="keyC" value="keyC value"/>
</map>
</property>
</bean>
Therefore, all you will get back is an empty struct, and quite possibly a headache trying to debug exactly why this
is happening in your code.
In ColdSpring 2, there is a nice, convenient util
custom schema, that exists to do things like create arrays, lists and other data structures for you.
It essentially is a simple wrapper around things like the MapFactoryBean, but the custom schema gives you a
code completion and hinting when using an XML editor (that support XML Schemas), as well as better error messages
when things go wrong.
So, for example, if I wanted to do exactly the same configuration as above, I would add the xml namespace for util in
the head of my xml file, like so:
<beans xmlns="http://www.coldspringframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:util="http://www.coldspringframework.org/schema/util"
xsi:schemaLocation="http://www.coldspringframework.org/schema/beanshttp://coldspringframework.org/schema/coldspring-beans-2.0.xsd
http://www.coldspringframework.org/schema/utilhttp://coldspringframework.org/schema/coldspring-util-2.0.xsd ">
Which would enable the <util:> namespace in my XML editor, and I could quickly type out:
<util:map id="myMap">
<entry key="keyA" value="keyA value"/>
<entry key="keyB" value="keyB value"/>
<entry key="keyC" value="keyC value"/>
</util:map>
And we would have the same result as above, but if something went wrong with your syntax, the IDE should show you
what it was, and if it didn’t, you would get a nice error from the XML validation letting you know exactly
what it was.
Of course, the whole point of this post, is due to the extensible nature of ColdSpring, you can write your own custom
XML namespaces, and register them with your XmlBeanFactory, to do things in very concise and easy to use manner
inside your IDEs as well.
This is a very simple example of what can be done with Custom xml schemas. You can drastically change the nature of
the BeanFactory as well as the contained beans through this mechanism, but this should hopefully give you a little
bit of a taste of what can be done with this new functionality.
This is something I did a while ago, but for whatever reason, totally forgot to announce publicly.
Thanks to the power of distributed version control, there is a copy of the Git repository for ColdSpring 2 on
Sourceforge, as well as one on
GitHub.
It goes without saying that GitHub really is the best place for Open Source projects to manage and allow for collaboration with other developers, so it makes sense to have a copy of ColdSpring 2 on GitHub as well.
So get your forks ready, and make some pull requests! :)