Skip to content

Instantly share code, notes, and snippets.

@robotlolita
Last active February 1, 2016 11:20
Show Gist options
  • Save robotlolita/3072746 to your computer and use it in GitHub Desktop.
Save robotlolita/3072746 to your computer and use it in GitHub Desktop.
Guards as nested ternaries vs If/else vs Switch
function attributes(xs, name) {
return is_element(xs)? xs.getAttribute(name)
: is_array(xs)? xs.map(attributes)
: is_sequence(xs)? map(xs, attributes)
: /* otherwise */ raise(TypeError('Not supported.'))
}
// vs
function attributes(xs, name) {
if (is_element(xs)) return xs.getAttribute(name)
if (is_array(xs)) return xs.map(attributes)
if (is_sequence(xs)) return map(xs, attributes)
else
throw TypeError('Not supported.')
}
// vs
function attributes(xs, name){
switch (true) {
case is_element(xs): return xs.getAttribute(name)
case is_array(xs): return xs.map(attributes)
case is_sequence(xs): return map(xs, attributes)
default: throw TypeError('Not supported.')
}
}
attributes = (xs, name) ->
| is-element xs => xs.get-attribute name
| is-array xs => xs.map attributes
| is-sequence xs => map xs, attributes
| otherwise => throw TypeError 'Not supported.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment