diff --git a/concepts/switch/.meta/config.json b/concepts/switch/.meta/config.json new file mode 100644 index 00000000..5fd269af --- /dev/null +++ b/concepts/switch/.meta/config.json @@ -0,0 +1,5 @@ +{ + "authors": ["colinleach"], + "contributors": [], + "blurb": "If matching on a value, especially a string, switch() can be more concise than if-else." +} diff --git a/concepts/switch/about.md b/concepts/switch/about.md new file mode 100644 index 00000000..614e6a98 --- /dev/null +++ b/concepts/switch/about.md @@ -0,0 +1,24 @@ +# About + +The `switch()` function can be a concise replacement for a long series of `if` ... `else if` tests. +The variable being switched on is most commonly a string, and if so the quotes can be omitted from the selector. + +```R +star <- function(type) { + switch(type, + M = , # will "fall through" if no value given + K = "red dwarf", + G = "Earth-like", + "bigger star" # only correct for O,B,A,F + ) +} + +> star("M") +[1] "red dwarf" +``` + +Note that options will only fall through if the value is left blank, as with `M` in the example above. +There is no need to include `break` statements as with some other languages. + +The final value can be a default, as here, or a `stop()` to throw an error if the conditions are intended to be exhaustive. +Switching on an integer is slightly different: for these the default is always `NULL`. diff --git a/concepts/switch/introduction.md b/concepts/switch/introduction.md new file mode 100644 index 00000000..76e30975 --- /dev/null +++ b/concepts/switch/introduction.md @@ -0,0 +1,24 @@ +# Introduction + +The `switch()` function can be a concise replacement for a long series of `if` ... `else if` tests. +The variable being switched on is most commonly a string, and if so the quotes can be omitted from the selector. + +```R +star <- function(type) { + switch(type, + M = , # will "fall through" if no value given + K = "red dwarf", + G = "Earth-like", + "bigger star" # only correct for O,B,A,F + ) +} + +> star("M") +[1] "red dwarf" +``` + +Note that options will only fall through if the value is left blank, as with `M` in the example above. +There is no need to include `break` statements as with some other languages. + +With character types, the final value can be a default, as here, or a `stop()` to throw an error if the conditions are intended to be exhaustive. +Switching on an integer is slightly different: for these the default is always `NULL`. diff --git a/concepts/switch/links.json b/concepts/switch/links.json new file mode 100644 index 00000000..cc123c35 --- /dev/null +++ b/concepts/switch/links.json @@ -0,0 +1,6 @@ +[ + { + "url": "https://adv-r.hadley.nz/control-flow.html#switch", + "description": "Advanced R, 2nd ed: switch" + } +] diff --git a/config.json b/config.json index 01ec79ac..62696f17 100644 --- a/config.json +++ b/config.json @@ -569,6 +569,11 @@ "uuid": "2751b6f2-7d71-4397-b063-9bf927a57756", "slug": "booleans", "name": "Booleans" + }, + { + "uuid": "e4c75b26-aacf-4598-a12b-88e0257bd3f5", + "slug": "switch", + "name": "Switch" } ], "key_features": [