One improvement that I’d like to see in Clojure is more examples in the doc strings (or maybe in a separate :example metadata item). Still, nothing to stop me building up a set of my own. So, here are some simple examples of working with Java arrays in Clojure… Given some sample data:
1 2 3 | |
To convert to Java arrays:
1 2 3 4 5 6 | |
Note that this always returns Object[] regardless of the contents of the collection. Note also that the map isn’t flattened (the pp function used here is in clojure.contrib.pprint):
1 2 | |
If the array is 2-dimensional there is a corresponding function:
1 2 3 4 5 6 7 | |
If you need to use a specific type of array (e.g. to pass a String[] into a Java method) or need to use an array with more than 3 dimensions it’s a little trickier:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
There, that should serve as a handy reference for myself for when I’m feeling forgetful…