Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Uncurry(?) in Groovy
// 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]]
@timyates

This comment has been minimized.

Copy link
Owner Author

@timyates timyates commented May 7, 2016

Definitely not uncurrying

@bitsnaps

This comment has been minimized.

Copy link

@bitsnaps bitsnaps commented Jul 23, 2018

The last one should be like so:
assert collate([1, 2, 3, 4, 5], 2) == [[1, 2], [3, 4],[5]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment