(x) => x + 1

(x) => x + 1

Short talk about adding (x) => x + 1 anonymous functions to R

822cde5b176ef140171a27f8294c2bd8?s=128

Andrew Craig

August 01, 2020
Tweet

Transcript

  1. (x) => x + 1 Andrew Craig Tokyo.R #87 2020/08/01

  2. (x) => x + 1 Changing R to add new

    syntax
  3. Me • Andrew Craig • Data Scientist • GitHub: @andycraig

    • Twitter: @andrew_cb2 • Professional: R, Python • Hobby: Clojure, APL
  4. > sapply(c(1, 2), function(x) x + 1) [1] 2 3

  5. > sapply(c(1, 2), function(x) x + 1) [1] 2 3

  6. > sapply(c(1, 2), function(x) x + 1) [1] 2 3

  7. > sapply(c(1, 2), function(x) x + 1) [1] 2 3

    long!
  8. Future R? useR! 2020 talk by Luke Tierney (Syntax Extensions

    starts at 1:07:00) https://www.youtube.com/watch?v=X_eDHNVceCU&feature=youtu.be
  9. Future R? useR! 2020 talk by Luke Tierney (Syntax Extensions

    starts at 1:07:00) https://www.youtube.com/watch?v=X_eDHNVceCU&feature=youtu.be 1. \(x) x + 1 2. @(x) x + 1 3. (x) => x + 1
  10. Future R? 1. \(x) x + 1 # Haskell style

    2. @(x) x + 1 # Matlab style 3. (x) => x + 1 # JavaScript style
  11. Future R? 1. \(x) x + 1 # EASY 2.

    @(x) x + 1 # EASY 3. (x) => x + 1 # DIFFICULT
  12. Future R? 1. \(x) x + 1 # EASY 2.

    @(x) x + 1 # EASY 3. (x) => x + 1 # DIFFICULT
  13. Future R? 1. \(x) x + 1 # EASY 2.

    @(x) x + 1 # EASY 3. (x) => x + 1 # DIFFICULT Let’s try it!
  14. How?!?!

  15. R source code doc etc m4 po share src tests

    tools ChangeLog config.site configure https://cran.rstudio.com/src/base/R-3/R-3.6.3.tar.gz
  16. R source code appl extra gnuwin32 include library main modules

    nmath scripts unix Makefile.in https://cran.rstudio.com/src/base/R-3/R-3.6.3.tar.gz
  17. R source code g_her_glyph.c g_her_metr.h g_jis.h gevents.c gram.c gram.y gram-ex.c

    graphics.c grep.c gzio.h identical.c https://cran.rstudio.com/src/base/R-3/R-3.6.3.tar.gz
  18. R source code gram.y???

  19. R source code gram.y??? grammar file

  20. gram.y

  21. gram.y | FUNCTION '(' formlist ')' cr expr_or_assign %prec {

    $$ = xxdefun($1,$3,$6,&@$); se | IF ifcond expr_or_assign ELSE expr_or_assign { | FOR forcond expr_or_assign %prec FOR { $$ = xxf | WHILE cond expr_or_assign { $$ = xxwhile($1,$2,$ | expr LBB sublist ']' ']' { $$ = xxsubscript($1,
  22. gram.y: (x) => x + 1

  23. gram.y: (x) => x + 1 "FUNCTION", "'function'", "EQ_ASSIGN", "'='",

    "RIGHT_ASSIGN", "'->'", "LBB", "'[['", "FOR", "'for'", "IN", "'in'",
  24. gram.y: (x) => x + 1 "FUNCTION", "'function'", "EQ_ASSIGN", "'='",

    "RIGHT_ASSIGN", "'->'", "ANON", "'=>'", "LBB", "'[['", "FOR", "'for'", "IN", "'in'",
  25. gram.y: (x) => x + 1 | '(' SYMBOL ')'

    ANON expr_or_assign { $$ = xxfirstformal0($2); mo $$ = xxdefunanon($$,$5,&@$); | '(' SYMBOL EQ_ASSIGN expr ')' ANON expr_or_assign { $$ = xxfirstformal1($2,$4); $$ = xxdefunanon($$,$7,&@$); | '(' formlist ')' ANON expr_or_assign { $$ = xxdefunanon($2,$5,&@$);
  26. gram.y: (x) => x + 1 | '(' SYMBOL ')'

    ANON expr_or_assign { $$ = xxfirstformal0($2); mo $$ = xxdefunanon($$,$5,&@$); | '(' SYMBOL EQ_ASSIGN expr ')' ANON expr_or_assign { $$ = xxfirstformal1($2,$4); $$ = xxdefunanon($$,$7,&@$); | '(' formlist ')' ANON expr_or_assign { $$ = xxdefunanon($2,$5,&@$);
  27. By the way … What is this? (y = 3)

  28. By the way … What is this? (y = 3)

    => y + 1 It was a new function!
  29. By the way … What is this? (y = 3)

    It was NOT a new function!
  30. By the way … What is this? (y = 3)

    It was NOT a new function! DIFFICULT PART
  31. By the way … What is this? (y = 3)

    It was NOT a new function! Needs grammar change too
  32. gram.y: (x) => x + 1 gram.y → gram.c

  33. gram.y: (x) => x + 1 gram.y → gram.c $

    bison -d src/main/gram.y -o src/main/gram.c
  34. gram.y: (x) => x + 1 gram.y → gram.c $

    bison -d src/main/gram.y -o src/main/gram.c Compile $ make
  35. gram.y: (x) => x + 1 gram.y → gram.c $

    bison -d src/main/gram.y -o src/main/gram.c Compile $ make Creates R with (x) => x + 1!
  36. Test > sapply(c(1, 2), function(x) x + 1) [1] 2

    3
  37. Test > sapply(c(1, 2), function(x) x + 1) [1] 2

    3 > sapply(c(1, 2), (x) => x + 1)
  38. Test > sapply(c(1, 2), function(x) x + 1) [1] 2

    3 > sapply(c(1, 2), (x) => x + 1) [1] 2 3
  39. Test > sapply(c(1, 2), function(x) x + 1) [1] 2

    3 > sapply(c(1, 2), (x) => x + 1) [1] 2 3
  40. Code https://github.com/andycraig/r-anonymous-functions gram.y: +38 lines -6 lines

  41. You can program in R You can program R