Skip to content

Instantly share code, notes, and snippets.

@brunkb
Created June 2, 2017 16:49
Show Gist options
  • Save brunkb/ae47068d88ec380523255bb84cfab386 to your computer and use it in GitHub Desktop.
Save brunkb/ae47068d88ec380523255bb84cfab386 to your computer and use it in GitHub Desktop.
Removing conditional logic
def addOne = { a ->
return a + 1
}
def addTwo = { b ->
return b + 2
}
def decideWhatToRun = [0: addOne, 1: addTwo]
def runSomethingBasedOnInput = { val, input ->
def fn = decideWhatToRun[val]
def result = fn.call(input)
result
}
println runSomethingBasedOnInput(0, 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment