Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

(x) => x + 1

(x) => x + 1

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

Andrew Craig

August 01, 2020
Tweet

More Decks by Andrew Craig

Other Decks in Programming

Transcript

  1. Me • Andrew Craig • Data Scientist • GitHub: @andycraig

    • Twitter: @andrew_cb2 • Professional: R, Python • Hobby: Clojure, APL
  2. 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
  3. 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
  4. Future R? 1. \(x) x + 1 # Haskell style

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

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

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

    @(x) x + 1 # EASY 3. (x) => x + 1 # DIFFICULT Let’s try it!
  8. 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
  9. 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
  10. 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
  11. 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,
  12. gram.y: (x) => x + 1 "FUNCTION", "'function'", "EQ_ASSIGN", "'='",

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

    "RIGHT_ASSIGN", "'->'", "ANON", "'=>'", "LBB", "'[['", "FOR", "'for'", "IN", "'in'",
  14. 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,&@$);
  15. 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,&@$);
  16. By the way … What is this? (y = 3)

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

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

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

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

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

    bison -d src/main/gram.y -o src/main/gram.c Compile $ make
  22. 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!
  23. Test > sapply(c(1, 2), function(x) x + 1) [1] 2

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

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

    3 > sapply(c(1, 2), (x) => x + 1) [1] 2 3