|
setup diary |
R | julia | ruby | python3 | |
定義 | f<-function(x){2*x} | f(x)=2x | def f(x) 2*x end | def f(x):return 2*x |
省略値 | f<-function(x=1){2*x} f() | f(x=1)=2x f() | def f(x=1) 2*x end f | def f(x=1):return 2*x f() |
キーワード引数 | f<-function(x,y){x^y} f(y=1,x=2) | f(;x,y)=x^y f(y=1,x=2) | def f(x:,y:) x**y end f(y:1,x:2) | def f(x,y):return x**y f(y=1,x=2) |
可変長引数 | f<-function(x,...){x+length(c(...))} f(3,2,1) | f(x,a...)=x+length(a) f(3,2,1) | def f(x,*a) x+a.size end f(3,2,1) | def f(x,*a):return x+len(a) f(3,2,1) |
引数展開 | f<-function(x,y){x+y} a<-list(1,2) do.call(f,a) | f(x,y)=x+y a=[1,2] f(a...) | def f(x,y) x+y end a=[1,2] f(*a) | def f(x,y):return x+y a=[1,2] f(*a) |
無名関数 | (function(x){2*x})(2) | (x->2x)(2) | proc{|x|2*x}.(2) | (lambda x:2*x)(2) |
配列 | f<-function(x){2*x} f(1:10) | f(x)=2x f.(1:10) | def f(x) 2*x end [*1..10].map{|x|f(x)} | def f(x):return 2*x list(map(f,range(1,11))) |
合成 | f<-function(x){2*x} g<-function(x){x+1} g(f(2)) | f(x)=2x g(x)=x+1 (g∘f)(2) 2|>f|>g | f=proc{|x|2*x} g=proc{|x|x+1} (g<<f).(2) (f>>g).(2) | import toolz def f(x):return 2*x def g(x):return x+1 toolz.compose(g,f)(2) toolz.pipe(2,f, g,) |