This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Change a method reference into a closure that takes the delegate as the first parameter | |
import org.codehaus.groovy.runtime.MethodClosure | |
def uncurry(MethodClosure c) { | |
{a, ...b -> a."$c.method"(*b) } | |
} | |
// So uncurrying Number.plus gives us a closure that will take {x, y -> x.plus(y)} | |
def plus = uncurry(Number.&plus) | |
assert plus(1, 2) == 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Takes a camel cased identifier name and returns an underscore separated | |
* name | |
* | |
* Example: | |
* camelToUnderscores("thisIsA1Test") == "this_is_a_1_test" | |
*/ | |
def camelToUnderscores(name: String) = "[A-Z\\d]".r.replaceAllIn(name, {m => | |
"_" + m.group(0).toLowerCase() | |
}) |