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
99
Other Decks in Programming
See All in Programming
テスト自動化失敗から再挑戦しチームにオーナーシップを委譲した話/STAC2024 macho
ma_cho29
1
1.3k
Full stack testing :: basic to basic
up1
1
930
プロダクトの品質に コミットする / Commit to Product Quality
pekepek
2
760
선언형 UI에서의 상태관리
l2hyunwoo
0
140
KubeCon + CloudNativeCon NA 2024 Overviewat Kubernetes Meetup Tokyo #68 / amsy810_k8sjp68
masayaaoyama
0
250
htmxって知っていますか?次世代のHTML
hiro_ghap1
0
330
Security_for_introducing_eBPF
kentatada
0
110
バグを見つけた?それAppleに直してもらおう!
uetyo
0
170
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
120
Scalaから始めるOpenFeature入門 / Scalaわいわい勉強会 #4
arthur1
1
300
Keeping it Ruby: Why Your Product Needs a Ruby SDK - RubyWorld 2024
envek
0
180
ブラウザ単体でmp4書き出すまで - muddy-web - 2024-12
yue4u
2
460
Featured
See All Featured
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
How STYLIGHT went responsive
nonsquared
95
5.2k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
810
Building Your Own Lightsaber
phodgson
103
6.1k
Visualization
eitanlees
146
15k
Testing 201, or: Great Expectations
jmmastey
40
7.1k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
Gamification - CAS2011
davidbonilla
80
5.1k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.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