Last active
February 1, 2016 11:20
-
-
Save robotlolita/3072746 to your computer and use it in GitHub Desktop.
Guards as nested ternaries vs If/else vs Switch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.') | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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