Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
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
96
Other Decks in Programming
See All in Programming
macOS でできる リアルタイム動画像処理
biacco42
9
2.4k
詳細解説! ArrayListの仕組みと実装
yujisoftware
0
580
Flutterを言い訳にしない!アプリの使い心地改善テクニック5選🔥
kno3a87
1
140
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1k
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
5
2.1k
AWS IaCの注目アップデート 2024年10月版
konokenj
3
3.3k
Nurturing OpenJDK distribution: Eclipse Temurin Success History and plan
ivargrimstad
0
870
Jakarta EE meets AI
ivargrimstad
0
510
OnlineTestConf: Test Automation Friend or Foe
maaretp
0
100
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
100
Contemporary Test Cases
maaretp
0
130
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
250
Featured
See All Featured
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.3k
Code Reviewing Like a Champion
maltzj
520
39k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Code Review Best Practice
trishagee
64
17k
Thoughts on Productivity
jonyablonski
67
4.3k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
It's Worth the Effort
3n
183
27k
Building Adaptive Systems
keathley
38
2.3k
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