Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
(x) => x + 1
Search
Andrew Craig
August 01, 2020
Programming
2
1.2k
(x) => x + 1
Short talk about adding (x) => x + 1 anonymous functions to R
Andrew Craig
August 01, 2020
Tweet
Share
More Decks by Andrew Craig
See All by Andrew Craig
R in Visual Studio Code
andycraig
2
1.5k
Writing a Functional Language Compiler
andycraig
0
99
Other Decks in Programming
See All in Programming
みんなでプロポーザルを書いてみた
yuriko1211
0
300
Missing parts when designing and implementing Android UI
ericksli
0
360
.NET Conf 2024の振り返り
tomokusaba
0
170
Modular Monolith Monorepo ~シンプルさを保ちながらmonorepoのメリットを最大化する~
yuisakamoto
10
3.5k
CSC509 Lecture 13
javiergs
PRO
0
120
AWS AppSyncを用いた GraphQL APIの開発について - NIFTY Tech Talk #22
niftycorp
PRO
0
100
競技プログラミングで 基礎体力を身につけよう / You can get basic skills through competitive programming
mdstoy
0
130
Jakarta EE meets AI
ivargrimstad
0
790
ペアーズにおけるAmazon Bedrockを⽤いた障害対応⽀援 ⽣成AIツールの導⼊事例 @ 20241115配信AWSウェビナー登壇
fukubaka0825
6
2.2k
初めてDefinitelyTypedにPRを出した話
syumai
0
470
N.E.X.T LEVEL
pluu
2
200
Cognitoが大型アップデート!Managed Loginとパスワードレスログインを実際に使ってみた@しむそくRadio Special Day1
tmhirai
2
110
Featured
See All Featured
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
44
2.2k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Designing for Performance
lara
604
68k
Typedesign – Prime Four
hannesfritz
40
2.4k
Art, The Web, and Tiny UX
lynnandtonic
297
20k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
247
1.3M
Large-scale JavaScript Application Architecture
addyosmani
510
110k
The World Runs on Bad Software
bkeepers
PRO
65
11k
How to train your dragon (web standard)
notwaldorf
88
5.7k
Fireside Chat
paigeccino
34
3k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
Transcript
(x) => x + 1 Andrew Craig Tokyo.R #87 2020/08/01
(x) => x + 1 Changing R to add new
syntax
Me • Andrew Craig • Data Scientist • GitHub: @andycraig
• Twitter: @andrew_cb2 • Professional: R, Python • Hobby: Clojure, APL
> sapply(c(1, 2), function(x) x + 1) [1] 2 3
> sapply(c(1, 2), function(x) x + 1) [1] 2 3
> sapply(c(1, 2), function(x) x + 1) [1] 2 3
> sapply(c(1, 2), function(x) x + 1) [1] 2 3
long!
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
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
Future R? 1. \(x) x + 1 # Haskell style
2. @(x) x + 1 # Matlab style 3. (x) => x + 1 # JavaScript style
Future R? 1. \(x) x + 1 # EASY 2.
@(x) x + 1 # EASY 3. (x) => x + 1 # DIFFICULT
Future R? 1. \(x) x + 1 # EASY 2.
@(x) x + 1 # EASY 3. (x) => x + 1 # DIFFICULT
Future R? 1. \(x) x + 1 # EASY 2.
@(x) x + 1 # EASY 3. (x) => x + 1 # DIFFICULT Let’s try it!
How?!?!
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
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
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
R source code gram.y???
R source code gram.y??? grammar file
gram.y
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,
gram.y: (x) => x + 1
gram.y: (x) => x + 1 "FUNCTION", "'function'", "EQ_ASSIGN", "'='",
"RIGHT_ASSIGN", "'->'", "LBB", "'[['", "FOR", "'for'", "IN", "'in'",
gram.y: (x) => x + 1 "FUNCTION", "'function'", "EQ_ASSIGN", "'='",
"RIGHT_ASSIGN", "'->'", "ANON", "'=>'", "LBB", "'[['", "FOR", "'for'", "IN", "'in'",
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,&@$);
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,&@$);
By the way … What is this? (y = 3)
By the way … What is this? (y = 3)
=> y + 1 It was a new function!
By the way … What is this? (y = 3)
It was NOT a new function!
By the way … What is this? (y = 3)
It was NOT a new function! DIFFICULT PART
By the way … What is this? (y = 3)
It was NOT a new function! Needs grammar change too
gram.y: (x) => x + 1 gram.y → gram.c
gram.y: (x) => x + 1 gram.y → gram.c $
bison -d src/main/gram.y -o src/main/gram.c
gram.y: (x) => x + 1 gram.y → gram.c $
bison -d src/main/gram.y -o src/main/gram.c Compile $ make
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!
Test > sapply(c(1, 2), function(x) x + 1) [1] 2
3
Test > sapply(c(1, 2), function(x) x + 1) [1] 2
3 > sapply(c(1, 2), (x) => x + 1)
Test > sapply(c(1, 2), function(x) x + 1) [1] 2
3 > sapply(c(1, 2), (x) => x + 1) [1] 2 3
Test > sapply(c(1, 2), function(x) x + 1) [1] 2
3 > sapply(c(1, 2), (x) => x + 1) [1] 2 3
Code https://github.com/andycraig/r-anonymous-functions gram.y: +38 lines -6 lines
You can program in R You can program R