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
100
Other Decks in Programming
See All in Programming
テストをしないQAエンジニアは何をしているか?
nealle
0
130
技術を根付かせる / How to make technology take root
kubode
1
240
自分ひとりから始められる生産性向上の取り組み #でぃーぷらすオオサカ
irof
8
2.6k
Amazon S3 TablesとAmazon S3 Metadataを触ってみた / 20250201-jawsug-tochigi-s3tables-s3metadata
kasacchiful
0
100
Grafana Cloudとソラカメ
devoc
0
140
第3回関東Kaggler会_AtCoderはKaggleの役に立つ
chettub
3
890
Multi Step Form, Decentralized Autonomous Organization
pumpkiinbell
1
660
AWS Lambda functions with C# 用の Dev Container Template を作ってみた件
mappie_kochi
0
240
DROBEの生成AI活用事例 with AWS
ippey
0
130
Conform を推す - Advocating for Conform
mizoguchicoji
3
680
ARA Ansible for the teams
kksat
0
150
Honoをフロントエンドで使う 3つのやり方
yusukebe
5
2.2k
Featured
See All Featured
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
31
2.1k
Mobile First: as difficult as doing things right
swwweet
223
9.3k
YesSQL, Process and Tooling at Scale
rocio
171
14k
How GitHub (no longer) Works
holman
313
140k
Making the Leap to Tech Lead
cromwellryan
133
9.1k
Code Review Best Practice
trishagee
66
17k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
The Language of Interfaces
destraynor
156
24k
How to Think Like a Performance Engineer
csswizardry
22
1.3k
GraphQLの誤解/rethinking-graphql
sonatard
68
10k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.6k
It's Worth the Effort
3n
184
28k
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