

{"id":12447,"date":"2019-08-23T13:04:34","date_gmt":"2019-08-23T18:04:34","guid":{"rendered":"https:\/\/rud.is\/b\/?p=12447"},"modified":"2019-08-23T13:04:34","modified_gmt":"2019-08-23T18:04:34","slug":"polyglot-fizzbuzz-in-r-plus-why-cant-johnny-code","status":"publish","type":"post","link":"https:\/\/rud.is\/b\/2019\/08\/23\/polyglot-fizzbuzz-in-r-plus-why-cant-johnny-code\/","title":{"rendered":"Polyglot FizzBuzz in R (Plus: &#8220;Why Can&#8217;t Johnny Code?&#8221;)"},"content":{"rendered":"<p>I caught this post on the <a href=\"https:\/\/letterstoanewdeveloper.com\/2019\/08\/23\/the-surprising-number-of-programmers-who-cant-program\/\">The Surprising Number Of Programmers Who Can\u2019t Program<\/a> from the Hacker News RSS feed. Said post links to another, classic post on the same subject and you should read both before continuing.<\/p>\n<p>Back? Great! Let&#8217;s dig in.<\/p>\n<h3>Why does hrbrmstr care about this?<\/h3>\n<p>Offspring #3 completed his Freshman year at UMaine Orono last year but wanted to stay academically active over the summer (he&#8217;s majoring in astrophysics and knows he&#8217;ll need some programming skills to excel in his field) and took an introductory C++ course from UMaine that was held virtually, with 1 lecture per week (14 weeks IIRC) and 1 assignment due per week with no other grading.<\/p>\n<p>After seeing what passes for a standard (UMaine is not exactly on the top list of institutions to attend if one wants to be a computer scientist) intro C++ course, I&#8217;m not really surprised &#8220;Johnny can&#8217;t code&#8221;. Thirteen weeks in the the class finally started covering OO concepts, and the course is ending with a scant intro to polymorphism. Prior to this, most of the assignments were just variations on each other (read from stdin, loop with conditionals, print output) with no program going over 100 LoC (that includes comments and spacing). This wasn&#8217;t a &#8220;compsci for non-compsci majors&#8221; course, either. Anyone majoring in an area of study that requires programming could have taken this course to fulfill one of the requirements, and they&#8217;d be set on a path of forever using StackOverflow copypasta to try to get their future work done.<\/p>\n<p>I&#8217;m fairly certain most of #3&#8217;s classmates could not program fizzbuzz without googling and even more certain most have no idea they weren&#8217;t really &#8220;coding in C++&#8221; most of the course.<\/p>\n<p>If this is how most other middling colleges are teaching the basics of computer programming, it&#8217;s no wonder employers are having a difficult time finding qualified talent.<\/p>\n<h3>You have an &#8220;R&#8221; tag &#8212; actually, a few language tags &#8212; on this post, so where&#8217;s the code?<\/h3>\n<p>After the article triggered the lament in the previous section, a crazy, @coolbutuseless-esque thought came into my head: &#8220;I wonder how many different language FizzBuz solutions can be created from within R?&#8221;.<\/p>\n<p>The criteria for that notion is\/was that there needed to be some <code>Rcpp::cppFunction()<\/code>, <code>reticulate::py_run_string()<\/code>, V8 context <code>eval()<\/code>-type way to have the code in-R but then run through those far-super-to-any-other-language&#8217;s polyglot extensibility constructs.<\/p>\n<p>Before getting lost in the weeds, there were some other thoughts on language inclusion:<\/p>\n<ul>\n<li>Should Java be included? I :heart: {rJava}, but <code>cat()<\/code>-ing Java code out and running <code>system()<\/code> to compile it first seemed like cheating (even though that&#8217;s kinda just what <code>cppFunction()<\/code> does). Toss a note into a comment if you think a Java example should be added (or add said Java example in a comment or link to it in one!).<\/li>\n<li>I think Julia should be in this example list but do not care enough about it to load <a href=\"https:\/\/github.com\/Non-Contradiction\/JuliaCall\">{JuliaCall}<\/a> and craft an example (again, link or post one if you can crank it out quickly).<\/li>\n<li>I think Lua could be in this example given the existence of <a href=\"https:\/\/github.com\/noamross\/luar\">{luar}<\/a>. If you agree, give it a go!<\/li>\n<li>Go &amp; Rust compiled code can also be called in R (thanks to Romain &amp; Jeroen) once they&#8217;re turned into C-compatible libraries. Should this polyglot example show this as well?<\/li>\n<li>What other languages am I missing?<\/li>\n<\/ul>\n<h3>The aforementioned &#8220;weeds&#8221;<\/h3>\n<p>One criteria for each language fizzbuzz example is that they need to be readable, not hacky-cool. That doesn&#8217;t mean the solutions still can&#8217;t be a bit creative. We&#8217;ll lightly go through each one I managed to code up. First we&#8217;ll need some helpers:<\/p>\n<pre><code class=\"language-r\">suppressPackageStartupMessages({\n  library(purrr)\n  library(dplyr)\n  library(reticulate)\n  library(V8)\n  library(Rcpp)\n})\n<\/code><\/pre>\n<p>The R, JavaScript, and Python implementations are all in the <code>microbenchmark()<\/code> call way down below. Up here are C and C++ versions. The C implementation is boring and straightforward, but we&#8217;re using <code>Rprintf()<\/code> so we can capture the output vs have any output buffering woes impact the timings.<\/p>\n<pre><code class=\"language-r\">cppFunction('\nvoid cbuzz() {\n\n  \/\/ super fast plain C\n\n  for (unsigned int i=1; i&lt;=100; i++) {\n    if      (i % 15 == 0) Rprintf(\"FizzBuzz\\\\n\");\n    else if (i %  3 == 0) Rprintf(\"Fizz\\\\n\");\n    else if (i %  5 == 0) Rprintf(\"Buzz\\\\n\");\n    else Rprintf(\"%d\\\\n\", i);\n  }\n\n}\n')\n<\/code><\/pre>\n<p>The <code>cbuzz()<\/code> example is just fine even in C++ land, but we can take advantage of some C++11 vectorization features to stay formally in C++-land and play with some fun features like lambdas. This will be a bit slower than the C version plus consume more memory,  but shows off some features some folks might not be familiar with:<\/p>\n<pre><code class=\"language-r\">cppFunction('\nvoid cppbuzz() {\n\n  std::vector&lt;int&gt; numbers(100); \/\/ will eventually be 1:100\n  std::iota(numbers.begin(), numbers.end(), 1); \/\/ kinda sorta equiva of our R 1:100 but not exactly true\n\n  std::vector&lt;std::string&gt; fb(100); \/\/ fizzbuzz strings holder\n\n  \/\/ transform said 1..100 into fizbuzz strings\n  std::transform(\n    numbers.begin(), numbers.end(), \n    fb.begin(),\n    [](int i) -&gt; std::string { \/\/ lambda expression are cool like a fez\n        if      (i % 15 == 0) return(\"FizzBuzz\");\n        else if (i %  3 == 0) return(\"Fizz\");\n        else if (i %  5 == 0) return(\"Buzz\");\n        else return(std::to_string(i));\n    }\n  );\n\n  \/\/ round it out with use of for_each and another lambda\n  \/\/ this turns out to be slightly faster than range-based for-loop\n  \/\/ collection iteration syntax.\n  std::for_each(\n    fb.begin(), fb.end(), \n    [](std::string s) { Rcout &lt;&lt; s &lt;&lt; std::endl; }\n  );\n\n}\n', \nplugins = c('cpp11'))\n<\/code><\/pre>\n<p>Both of those functions are now available to R.<\/p>\n<p>Next, we need to prepare to run JavaScript and Python code, so we&#8217;ll initialize both of those environments:<\/p>\n<pre><code class=\"language-r\">ctx &lt;- v8()\n\npy_config() # not 100% necessary but I keep my needed {reticulate} options in env vars for reproducibility\n<\/code><\/pre>\n<p>Then, we tell R to capture all the output. Using <code>sink()<\/code> is a bit better than <code>capture.output()<\/code> in this use-case since to avoid nesting calls, and we need to handle Python stdout the same way <code>py_capture_output()<\/code> does to be fair in our measurements:<\/p>\n<pre><code class=\"language-r\">output_tools &lt;- import(\"rpytools.output\")\nrestore_stdout &lt;- output_tools$start_stdout_capture()\n\ncap &lt;- rawConnection(raw(0), \"r+\")\nsink(cap)\n<\/code><\/pre>\n<p>There are a few implementations below across the tidy and base R multiverse. Some use vectorization; some do not. This will let us compare overall &#8220;speed&#8221; of solution. If you have another suggestion for a <em>readable<\/em> solution in R, drop a note in the comments:<\/p>\n<pre><code class=\"language-r\">microbenchmark::microbenchmark(\n\n  # tidy_vectors_case() is slowest but you get all sorts of type safety \n  # for free along with very readable idioms.\n\n  tidy_vectors_case = map_chr(1:100, ~{ \n    case_when(\n      (.x %% 15 == 0) ~ \"FizzBuzz\",\n      (.x %%  3 == 0) ~ \"Fizz\",\n      (.x %%  5 == 0) ~ \"Buzz\",\n      TRUE ~ as.character(.x)\n    )\n  }) %&gt;% \n    cat(sep=\"\\n\"),\n\n  # tidy_vectors_if() has old-school if\/else syntax but still\n  # forces us to ensure type safety which is cool.\n\n  tidy_vectors_if = map_chr(1:100, ~{ \n    if (.x %% 15 == 0) return(\"FizzBuzz\")\n    if (.x %%  3 == 0) return(\"Fizz\")\n    if (.x %%  5 == 0) return(\"Buzz\")\n    return(as.character(.x))\n  }) %&gt;% \n    cat(sep=\"\\n\"),\n\n  # walk() just replaces `for` but stays in vector-land which is cool\n\n  tidy_walk = walk(1:100, ~{\n    if (.x %% 15 == 0) cat(\"FizzBuzz\\n\")\n    if (.x %%  3 == 0) cat(\"Fizz\\n\")\n    if (.x %%  5 == 0) cat(\"Buzz\\n\")\n    cat(.x, \"\\n\", sep=\"\")\n  }),\n\n  # vapply() gets us some similiar type assurance, albeit with arcane syntax\n\n  base_proper = vapply(1:100, function(.x) {\n    if (.x %% 15 == 0) return(\"FizzBuzz\")\n    if (.x %%  3 == 0) return(\"Fizz\")\n    if (.x %%  5 == 0) return(\"Buzz\")\n    return(as.character(.x))\n  }, character(1), USE.NAMES = FALSE) %&gt;% \n    cat(sep=\"\\n\"),\n\n  # sapply() is def lazy but this can outperform vapply() in some\n  # circumstances (like this one) and is a bit less arcane.\n\n  base_lazy = sapply(1:100, function(.x) {\n    if (.x %% 15 == 0)  return(\"FizzBuzz\")\n    if (.x %%  3 == 0) return(\"Fizz\")\n    if (.x %%  5 == 0) return(\"Buzz\")\n    return(.x)\n  }, USE.NAMES = FALSE) %&gt;% \n    cat(sep=\"\\n\"),\n\n  # for loops...ugh. might as well just use C\n\n  base_for = for(.x in 1:100) {\n    if      (.x %% 15 == 0) cat(\"FizzBuzz\\n\")\n    else if (.x %%  3 == 0) cat(\"Fizz\\n\")\n    else if (.x %%  5 == 0) cat(\"Buzz\\n\")\n    else cat(.x, \"\\n\", sep=\"\")\n  },\n\n  # ok, we'll just use C!\n\n  c_buzz = cbuzz(),\n\n  # we can go back to vector-land in C++\n\n  cpp_buzz = cppbuzz(),\n\n  # some &lt;3 for javascript\n\n  js_readable = ctx$eval('\nfor (var i=1; i &lt;101; i++){\n  if      (i % 15 == 0) console.log(\"FizzBuzz\")\n  else if (i %  3 == 0) console.log(\"Fizz\")\n  else if (i %  5 == 0) console.log(\"Buzz\")\n  else console.log(i)\n}\n'),\n\n  # icky readable, non-vectorized python\n\n  python = reticulate::py_run_string('\nfor x in range(1, 101):\n  if (x % 15 == 0):\n    print(\"Fizz Buzz\")\n  elif (x % 5 == 0):\n    print(\"Buzz\")\n  elif (x % 3 == 0):\n    print(\"Fizz\")\n  else:\n    print(x)\n')\n\n) -&gt; res\n<\/code><\/pre>\n<p>Turn off output capturing:<\/p>\n<pre><code class=\"language-r\">sink()\nif (!is.null(restore_stdout)) invisible(output_tools$end_stdout_capture(restore_stdout))\n<\/code><\/pre>\n<p>We used <code>microbenchmark()<\/code>, so here are the results:<\/p>\n<pre><code class=\"language-r\">res\n## Unit: microseconds\n##               expr       min         lq        mean     median         uq       max neval   cld\n##  tidy_vectors_case 20290.749 21266.3680 22717.80292 22231.5960 23044.5690 33005.960   100     e\n##    tidy_vectors_if   457.426   493.6270   540.68182   518.8785   577.1195   797.869   100  b   \n##          tidy_walk   970.455  1026.2725  1150.77797  1065.4805  1109.9705  8392.916   100   c  \n##        base_proper   357.385   375.3910   554.13973   406.8050   450.7490 13907.581   100  b   \n##          base_lazy   365.553   395.5790   422.93719   418.1790   444.8225   587.718   100 ab   \n##           base_for   521.674   545.9155   576.79214   559.0185   584.5250   968.814   100  b   \n##             c_buzz    13.538    16.3335    18.18795    17.6010    19.4340    33.134   100 a    \n##           cpp_buzz    39.405    45.1505    63.29352    49.1280    52.9605  1265.359   100 a    \n##        js_readable   107.015   123.7015   162.32442   174.7860   187.1215   270.012   100 ab   \n##             python  1581.661  1743.4490  2072.04777  1884.1585  1985.8100 12092.325   100    d \n<\/code><\/pre>\n<p>Said results are ??\u200d\u2640\ufe0f since this is a toy example, but I wanted to show that Jeroen&#8217;s {V8} can be super fast, especially when there&#8217;s no value marshaling to be done and that some things you may have thought should be faster, aren&#8217;t.<\/p>\n<h3>FIN<\/h3>\n<p>Definitely add links or code for changes or additions (especially the aforementioned other languages). Hopefully my lament about the computer science program at UMaine is not universally true for all the programming courses there.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I caught this post on the The Surprising Number Of Programmers Who Can\u2019t Program from the Hacker News RSS feed. Said post links to another, classic post on the same subject and you should read both before continuing. Back? Great! Let&#8217;s dig in. Why does hrbrmstr care about this? Offspring #3 completed his Freshman year [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":3,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":""},"categories":[829,15,640,91],"tags":[],"class_list":["post-12447","post","type-post","status-publish","format-standard","hentry","category-c","category-javascript","category-python-2","category-r"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Polyglot FizzBuzz in R (Plus: &quot;Why Can&#039;t Johnny Code?&quot;) - rud.is<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/rud.is\/b\/2019\/08\/23\/polyglot-fizzbuzz-in-r-plus-why-cant-johnny-code\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Polyglot FizzBuzz in R (Plus: &quot;Why Can&#039;t Johnny Code?&quot;) - rud.is\" \/>\n<meta property=\"og:description\" content=\"I caught this post on the The Surprising Number Of Programmers Who Can\u2019t Program from the Hacker News RSS feed. Said post links to another, classic post on the same subject and you should read both before continuing. Back? Great! Let&#8217;s dig in. Why does hrbrmstr care about this? Offspring #3 completed his Freshman year [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/rud.is\/b\/2019\/08\/23\/polyglot-fizzbuzz-in-r-plus-why-cant-johnny-code\/\" \/>\n<meta property=\"og:site_name\" content=\"rud.is\" \/>\n<meta property=\"article:published_time\" content=\"2019-08-23T18:04:34+00:00\" \/>\n<meta name=\"author\" content=\"hrbrmstr\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"hrbrmstr\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/rud.is\/b\/2019\/08\/23\/polyglot-fizzbuzz-in-r-plus-why-cant-johnny-code\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/rud.is\/b\/2019\/08\/23\/polyglot-fizzbuzz-in-r-plus-why-cant-johnny-code\/\"},\"author\":{\"name\":\"hrbrmstr\",\"@id\":\"https:\/\/rud.is\/b\/#\/schema\/person\/d7cb7487ab0527447f7fda5c423ff886\"},\"headline\":\"Polyglot FizzBuzz in R (Plus: &#8220;Why Can&#8217;t Johnny Code?&#8221;)\",\"datePublished\":\"2019-08-23T18:04:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/rud.is\/b\/2019\/08\/23\/polyglot-fizzbuzz-in-r-plus-why-cant-johnny-code\/\"},\"wordCount\":939,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\/\/rud.is\/b\/#\/schema\/person\/d7cb7487ab0527447f7fda5c423ff886\"},\"articleSection\":[\"C++\",\"Javascript\",\"Python\",\"R\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/rud.is\/b\/2019\/08\/23\/polyglot-fizzbuzz-in-r-plus-why-cant-johnny-code\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/rud.is\/b\/2019\/08\/23\/polyglot-fizzbuzz-in-r-plus-why-cant-johnny-code\/\",\"url\":\"https:\/\/rud.is\/b\/2019\/08\/23\/polyglot-fizzbuzz-in-r-plus-why-cant-johnny-code\/\",\"name\":\"Polyglot FizzBuzz in R (Plus: \\\"Why Can't Johnny Code?\\\") - rud.is\",\"isPartOf\":{\"@id\":\"https:\/\/rud.is\/b\/#website\"},\"datePublished\":\"2019-08-23T18:04:34+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/rud.is\/b\/2019\/08\/23\/polyglot-fizzbuzz-in-r-plus-why-cant-johnny-code\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/rud.is\/b\/2019\/08\/23\/polyglot-fizzbuzz-in-r-plus-why-cant-johnny-code\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/rud.is\/b\/2019\/08\/23\/polyglot-fizzbuzz-in-r-plus-why-cant-johnny-code\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/rud.is\/b\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Polyglot FizzBuzz in R (Plus: &#8220;Why Can&#8217;t Johnny Code?&#8221;)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/rud.is\/b\/#website\",\"url\":\"https:\/\/rud.is\/b\/\",\"name\":\"rud.is\",\"description\":\"&quot;In God we trust. All others must bring data&quot;\",\"publisher\":{\"@id\":\"https:\/\/rud.is\/b\/#\/schema\/person\/d7cb7487ab0527447f7fda5c423ff886\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/rud.is\/b\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/rud.is\/b\/#\/schema\/person\/d7cb7487ab0527447f7fda5c423ff886\",\"name\":\"hrbrmstr\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2023\/10\/ukr-shield.png?fit=460%2C460&ssl=1\",\"url\":\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2023\/10\/ukr-shield.png?fit=460%2C460&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2023\/10\/ukr-shield.png?fit=460%2C460&ssl=1\",\"width\":460,\"height\":460,\"caption\":\"hrbrmstr\"},\"logo\":{\"@id\":\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2023\/10\/ukr-shield.png?fit=460%2C460&ssl=1\"},\"description\":\"Don't look at me\u2026I do what he does \u2014 just slower. #rstats avuncular \u2022 ?Resistance Fighter \u2022 Cook \u2022 Christian \u2022 [Master] Chef des Donn\u00e9es de S\u00e9curit\u00e9 @ @rapid7\",\"sameAs\":[\"http:\/\/rud.is\"],\"url\":\"https:\/\/rud.is\/b\/author\/hrbrmstr\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Polyglot FizzBuzz in R (Plus: \"Why Can't Johnny Code?\") - rud.is","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/rud.is\/b\/2019\/08\/23\/polyglot-fizzbuzz-in-r-plus-why-cant-johnny-code\/","og_locale":"en_US","og_type":"article","og_title":"Polyglot FizzBuzz in R (Plus: \"Why Can't Johnny Code?\") - rud.is","og_description":"I caught this post on the The Surprising Number Of Programmers Who Can\u2019t Program from the Hacker News RSS feed. Said post links to another, classic post on the same subject and you should read both before continuing. Back? Great! Let&#8217;s dig in. Why does hrbrmstr care about this? Offspring #3 completed his Freshman year [&hellip;]","og_url":"https:\/\/rud.is\/b\/2019\/08\/23\/polyglot-fizzbuzz-in-r-plus-why-cant-johnny-code\/","og_site_name":"rud.is","article_published_time":"2019-08-23T18:04:34+00:00","author":"hrbrmstr","twitter_card":"summary_large_image","twitter_misc":{"Written by":"hrbrmstr","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/rud.is\/b\/2019\/08\/23\/polyglot-fizzbuzz-in-r-plus-why-cant-johnny-code\/#article","isPartOf":{"@id":"https:\/\/rud.is\/b\/2019\/08\/23\/polyglot-fizzbuzz-in-r-plus-why-cant-johnny-code\/"},"author":{"name":"hrbrmstr","@id":"https:\/\/rud.is\/b\/#\/schema\/person\/d7cb7487ab0527447f7fda5c423ff886"},"headline":"Polyglot FizzBuzz in R (Plus: &#8220;Why Can&#8217;t Johnny Code?&#8221;)","datePublished":"2019-08-23T18:04:34+00:00","mainEntityOfPage":{"@id":"https:\/\/rud.is\/b\/2019\/08\/23\/polyglot-fizzbuzz-in-r-plus-why-cant-johnny-code\/"},"wordCount":939,"commentCount":2,"publisher":{"@id":"https:\/\/rud.is\/b\/#\/schema\/person\/d7cb7487ab0527447f7fda5c423ff886"},"articleSection":["C++","Javascript","Python","R"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/rud.is\/b\/2019\/08\/23\/polyglot-fizzbuzz-in-r-plus-why-cant-johnny-code\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/rud.is\/b\/2019\/08\/23\/polyglot-fizzbuzz-in-r-plus-why-cant-johnny-code\/","url":"https:\/\/rud.is\/b\/2019\/08\/23\/polyglot-fizzbuzz-in-r-plus-why-cant-johnny-code\/","name":"Polyglot FizzBuzz in R (Plus: \"Why Can't Johnny Code?\") - rud.is","isPartOf":{"@id":"https:\/\/rud.is\/b\/#website"},"datePublished":"2019-08-23T18:04:34+00:00","breadcrumb":{"@id":"https:\/\/rud.is\/b\/2019\/08\/23\/polyglot-fizzbuzz-in-r-plus-why-cant-johnny-code\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/rud.is\/b\/2019\/08\/23\/polyglot-fizzbuzz-in-r-plus-why-cant-johnny-code\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/rud.is\/b\/2019\/08\/23\/polyglot-fizzbuzz-in-r-plus-why-cant-johnny-code\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/rud.is\/b\/"},{"@type":"ListItem","position":2,"name":"Polyglot FizzBuzz in R (Plus: &#8220;Why Can&#8217;t Johnny Code?&#8221;)"}]},{"@type":"WebSite","@id":"https:\/\/rud.is\/b\/#website","url":"https:\/\/rud.is\/b\/","name":"rud.is","description":"&quot;In God we trust. All others must bring data&quot;","publisher":{"@id":"https:\/\/rud.is\/b\/#\/schema\/person\/d7cb7487ab0527447f7fda5c423ff886"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/rud.is\/b\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/rud.is\/b\/#\/schema\/person\/d7cb7487ab0527447f7fda5c423ff886","name":"hrbrmstr","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2023\/10\/ukr-shield.png?fit=460%2C460&ssl=1","url":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2023\/10\/ukr-shield.png?fit=460%2C460&ssl=1","contentUrl":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2023\/10\/ukr-shield.png?fit=460%2C460&ssl=1","width":460,"height":460,"caption":"hrbrmstr"},"logo":{"@id":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2023\/10\/ukr-shield.png?fit=460%2C460&ssl=1"},"description":"Don't look at me\u2026I do what he does \u2014 just slower. #rstats avuncular \u2022 ?Resistance Fighter \u2022 Cook \u2022 Christian \u2022 [Master] Chef des Donn\u00e9es de S\u00e9curit\u00e9 @ @rapid7","sameAs":["http:\/\/rud.is"],"url":"https:\/\/rud.is\/b\/author\/hrbrmstr\/"}]}},"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p23idr-3eL","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":12866,"url":"https:\/\/rud.is\/b\/2021\/01\/04\/bringing-r-to-swift-on-macos\/","url_meta":{"origin":12447,"position":0},"title":"Bringing R to Swift on macOS","author":"hrbrmstr","date":"2021-01-04","format":false,"excerpt":"Over Christmas break I teased some screencaps: A more refined #rstats #swift \"SwiftR\" example. Simple Image view + some text views, a color picker and a button that runs R-in-Swift code (like {reticulate} does for Python in R)Note no ssd\/hd storage round-trip for the plot.Code snippet: https:\/\/t.co\/fWaHnztUgd pic.twitter.com\/y5m1I16tCB\u2014 Caliban's War\u2026","rel":"","context":"In &quot;Apple&quot;","block_context":{"text":"Apple","link":"https:\/\/rud.is\/b\/category\/apple\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":13498,"url":"https:\/\/rud.is\/b\/2022\/07\/10\/rust-cli-for-apples-weatherkit-rest-api\/","url_meta":{"origin":12447,"position":1},"title":"Rust CLI For Apple&#8217;s WeatherKit REST API","author":"hrbrmstr","date":"2022-07-10","format":false,"excerpt":"Apple is in the final stages of shuttering the DarkSky service\/API. They've replaced it with WeatherKit, which has both an xOS framework version as well as a REST API. To use either, you need to be a member of the Apple Developer Program (ADP) \u2014 $99.00\/USD per-year \u2014 and calls\u2026","rel":"","context":"In &quot;Apple&quot;","block_context":{"text":"Apple","link":"https:\/\/rud.is\/b\/category\/apple\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3194,"url":"https:\/\/rud.is\/b\/2015\/01\/08\/new-r-package-metricsgraphics\/","url_meta":{"origin":12447,"position":2},"title":"New R Package: metricsgraphics","author":"hrbrmstr","date":"2015-01-08","format":false,"excerpt":"Mozilla released the [MetricsGraphics.js library](http:\/\/metricsgraphicsjs.org\/) back in November of 2014 ([gh repo](https:\/\/github.com\/mozilla\/metrics-graphics)) and was greeted with great fanfare. It's primary focus is on crisp, clean layouts for interactive time-series data, but they have support for other chart types as well (though said support is far from comprehensive). I had been\u2026","rel":"","context":"In &quot;Charts &amp; Graphs&quot;","block_context":{"text":"Charts &amp; Graphs","link":"https:\/\/rud.is\/b\/category\/charts-graphs\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":12282,"url":"https:\/\/rud.is\/b\/2019\/06\/06\/make-multi-point-dumbbell-plots-in-ggplot2\/","url_meta":{"origin":12447,"position":3},"title":"Make Multi-point &#8220;dumbbell&#8221; Plots in ggplot2","author":"hrbrmstr","date":"2019-06-06","format":false,"excerpt":"A user of the {ggalt} package recently posted a question about how to add points to a geom_dumbbell() plot. For now, this is not something you can do with geom_dumbbell() but with a bit of data wrangling you can do this in a pretty straightforward manner with just your data\u2026","rel":"","context":"In &quot;ggplot&quot;","block_context":{"text":"ggplot","link":"https:\/\/rud.is\/b\/category\/ggplot\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2019\/06\/there-are-three-points-2.png?fit=1200%2C560&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2019\/06\/there-are-three-points-2.png?fit=1200%2C560&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2019\/06\/there-are-three-points-2.png?fit=1200%2C560&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2019\/06\/there-are-three-points-2.png?fit=1200%2C560&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2019\/06\/there-are-three-points-2.png?fit=1200%2C560&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":3622,"url":"https:\/\/rud.is\/b\/2015\/08\/21\/doh-i-could-have-had-just-used-v8\/","url_meta":{"origin":12447,"position":4},"title":"Doh! I Could Have Had Just Used V8!","author":"hrbrmstr","date":"2015-08-21","format":false,"excerpt":"An R user recently had the need to split a \"full, human name\" into component parts to retrieve first & last names. The full names could be anything from something simple like _\"David Regan\"_ to more complex & diverse such as _\"John Smith Jr.\"_, _\"Izaque Iuzuru Nagata\"_ or _\"Christian Schmit\u2026","rel":"","context":"In &quot;Javascript&quot;","block_context":{"text":"Javascript","link":"https:\/\/rud.is\/b\/category\/javascript\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":12891,"url":"https:\/\/rud.is\/b\/2021\/01\/23\/swiftr-switcheroo-calling-compiled-swift-from-r\/","url_meta":{"origin":12447,"position":5},"title":"SwiftR Switcheroo: Calling [Compiled] Swift from R!","author":"hrbrmstr","date":"2021-01-23","format":false,"excerpt":"I've been on a Swift + R bender for a while now, but have been envious of the pure macOS\/iOS (et al) folks who get to use Apple's seriously ++good machine learning libraries, which are even more robust on the new M1 hardware (it's cool having hardware components dedicated to\u2026","rel":"","context":"In &quot;macOS&quot;","block_context":{"text":"macOS","link":"https:\/\/rud.is\/b\/category\/macos\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/posts\/12447","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/comments?post=12447"}],"version-history":[{"count":0,"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/posts\/12447\/revisions"}],"wp:attachment":[{"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/media?parent=12447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/categories?post=12447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/tags?post=12447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}