From c817e0410543d00e26b346ac7588ee5ed531019d Mon Sep 17 00:00:00 2001 From: colinleach Date: Thu, 13 Jul 2023 12:12:30 -0700 Subject: [PATCH 1/2] switch concept --- concepts/switch/.meta/config.json | 5 +++++ concepts/switch/about.md | 24 ++++++++++++++++++++++++ concepts/switch/introduction.md | 24 ++++++++++++++++++++++++ concepts/switch/links.json | 6 ++++++ config.json | 5 +++++ 5 files changed, 64 insertions(+) create mode 100644 concepts/switch/.meta/config.json create mode 100644 concepts/switch/about.md create mode 100644 concepts/switch/introduction.md create mode 100644 concepts/switch/links.json diff --git a/concepts/switch/.meta/config.json b/concepts/switch/.meta/config.json new file mode 100644 index 00000000..19c01f8c --- /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." +} \ No newline at end of file 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..79cce9cb --- /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" + } + ] \ No newline at end of file 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": [ From 7be6c0bbaab9aa9e0bf43664d75267bcb9ce3050 Mon Sep 17 00:00:00 2001 From: colinleach Date: Thu, 13 Jul 2023 12:14:54 -0700 Subject: [PATCH 2/2] minor formatting changes --- concepts/switch/.meta/config.json | 2 +- concepts/switch/links.json | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/concepts/switch/.meta/config.json b/concepts/switch/.meta/config.json index 19c01f8c..5fd269af 100644 --- a/concepts/switch/.meta/config.json +++ b/concepts/switch/.meta/config.json @@ -2,4 +2,4 @@ "authors": ["colinleach"], "contributors": [], "blurb": "If matching on a value, especially a string, switch() can be more concise than if-else." -} \ No newline at end of file +} diff --git a/concepts/switch/links.json b/concepts/switch/links.json index 79cce9cb..cc123c35 100644 --- a/concepts/switch/links.json +++ b/concepts/switch/links.json @@ -1,6 +1,6 @@ [ - { - "url": "https://adv-r.hadley.nz/control-flow.html#switch", - "description": "Advanced R, 2nd ed: switch" - } - ] \ No newline at end of file + { + "url": "https://adv-r.hadley.nz/control-flow.html#switch", + "description": "Advanced R, 2nd ed: switch" + } +]