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
110
Other Decks in Programming
See All in Programming
pylint custom ruleで始めるレビュー自動化
shogoujiie
0
150
2025.2.14_Developers Summit 2025_登壇資料
0101unite
0
200
PRレビューのお供にDanger
stoticdev
1
230
Flutter × Firebase Genkit で加速する生成 AI アプリ開発
coborinai
0
170
苦しいTiDBへの移行を乗り越えて快適な運用を目指す
leveragestech
0
1.1k
Generating OpenAPI schema from serializers throughout the Rails stack - Kyobashi.rb #5
envek
1
390
推しメソッドsource_locationのしくみを探る - はじめてRubyのコードを読んでみた
nobu09
2
320
データベースのオペレーターであるCloudNativePGがStatefulSetを使わない理由に迫る
nnaka2992
0
240
Serverless Rust: Your Low-Risk Entry Point to Rust in Production (and the benefits are huge)
lmammino
1
150
責務と認知負荷を整える! 抽象レベルを意識した関心の分離
yahiru
8
1.3k
技術を改善し続ける
gumioji
0
120
密集、ドキュメントのコロケーション with AWS Lambda
satoshi256kbyte
1
210
Featured
See All Featured
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Into the Great Unknown - MozCon
thekraken
35
1.6k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
129
19k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.2k
A better future with KSS
kneath
238
17k
YesSQL, Process and Tooling at Scale
rocio
172
14k
Building Adaptive Systems
keathley
40
2.4k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
175
52k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
33
2.8k
The Cost Of JavaScript in 2023
addyosmani
47
7.4k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
7k
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