Skip to content

Instantly share code, notes, and snippets.

@erochest
Created April 7, 2015 17:13
Show Gist options
  • Save erochest/2f832f9a8583e4713918 to your computer and use it in GitHub Desktop.
Save erochest/2f832f9a8583e4713918 to your computer and use it in GitHub Desktop.
Using an object/map/set instead of a switch statement.
inputs = [
"hello"
"howdy"
"how's it going?"
"what's up?"
"yo"
]
output = "Hey!"
# Doing this will make searching faster, especially if the code will be run
# more than just a few times.
inputSet = {}
inputSet[i] = true for i in inputs
# This function tests whether i is a member of `inputs` (and `inputSet`) above
# and if so, prints a message containing both the input and output.
reply = (i) ->
if inputSet[i]
console.log "#{i} => #{output}"
# process.argv is node's way of getting at the command line. This just runs
# reply on each item on the command line.
reply input.toLowerCase() for input in process.argv
# So running it looks like this:
# > coffee reply.coffee "Today?" "Yo" "Tomorrow?" "Howdy" "What's UP?"
# yo => Hey!
# howdy => Hey!
# what's up? => Hey!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment