Created
May 5, 2016 20:42
-
-
Save timyates/2f703d1fb0e5d663556e74443ab86ce3 to your computer and use it in GitHub Desktop.
Revisions
-
timyates created this gist
May 5, 2016 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ // 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 // Also works with other methods with multiple parameters def collate = uncurry(List.&collate) assert collate([1, 2, 3, 4, 5], 2) == [[1, 2], [3, 4]]