Skip to content

Instantly share code, notes, and snippets.

@pradhyu
Created February 8, 2019 03:41
Show Gist options
  • Save pradhyu/7687aa66c276ba0ddcbea8e208fe0fd7 to your computer and use it in GitHub Desktop.
Save pradhyu/7687aa66c276ba0ddcbea8e208fe0fd7 to your computer and use it in GitHub Desktop.
Curry functions in groovy
@Grab('commons-lang:commons-lang:2.5')
import org.apache.commons.lang.RandomStringUtils as RSU
def randomClosure = { size, letters, numbers ->
// Invoke RandomStringUtils.random() method
RSU.random size, letters, numbers
}
def randomNumbers = randomClosure.rcurry(false, true) // letters = false, numbers = true
def randomLetters = randomClosure.ncurry(1, true, false) // letters = true, numbers = false
println randomClosure(10, true, true) // Sample output: VG7mffNAdA
println randomNumbers(10) // Sample output: 8099670444
println randomLetters(10) // Sample output: ZOHlHewEPU
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment