Skip to content

Instantly share code, notes, and snippets.

@wrathematics
Created September 28, 2014 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wrathematics/ff32004ace5fdee2cf28 to your computer and use it in GitHub Desktop.
Save wrathematics/ff32004ace5fdee2cf28 to your computer and use it in GitHub Desktop.
ifelse sugar
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version 2, December 2004
#
# Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
#
# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document, and changing it is allowed as long
# as the name is changed.
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
#
# 0. You just DO WHAT THE FUCK YOU WANT TO.
### DEAR ORACLE, PLS DONT SUE
decode <- function(x, ...)
{
l <- list(...)
evenodd <- length(l) %% 2
pos <- 1L
while (pos < (length(l) - evenodd))
{
if (x == l[[pos]])
return(l[[pos+1L]])
pos <- pos + 2L
}
if (evenodd)
return(l[[pos]])
else
return(NULL)
}
decode(2, 1, "thing", 2, "other thing", "default")
decode(3, 1, "thing", 2, "other thing", "default")
decode(2, 1, "thing", 2, "other thing")
decode(3, 1, "thing", 2, "other thing")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment