804 Views
March 12, 21
スライド概要
「魔術」を理解し、効果的に利用しよう!
「楽しく楽にcoolにsmartに」を理想とするprogrammer/philosopher/liberalist/realist。 好きな言語はClojure, Haskell, Python, English, français, русский。 読書、プログラミング、語学、法学、数学が大好き! イルカと海も大好き🐬
lagénorhynque (def fi e :id : eadi g :a ia e ag h @ age "/ a e [" : a g age [C :i e e [ :c i :c ib e e h e ɛ̃/" "] e Ha g a e i g ["gi h b.c "gi h b.c ["gi h b.c E g i h f a a g age- ea / age h / age h / a a -c i g ai ] a e/d c . e/d c . ia /c a he a ic ] d d e. ede a " e.ca bi "] e- i e- a"])
1. 2. 3. /
metaprogramming R b ( 2 )
( ) ( e ec i (me acla ) e c. ) e c. ( em la e) (mac ) ) (
/ e c. DSL (d mai - e c. / eci c la g age)
e c. e c.
eg Macro Club has t o rules, plus one e ception. The rst rule of Macro Club is Don t Write Macros. (...) The second rule of Macro Club is Write Macros f That s the Onl Wa to Encapsulate a Pattern. (...) The e ception to the rule is that ou can rite an macro that makes life easier for our callers hen compared ith an equi alent function.
Class, Method he > c a G ee i g ...> i a e S i g a e; ...> G ee i g(S i g a e) ...> hi . a e = a e; ...> ...> S i g he () ...> e "He , " + a e + "!"; ...> ...> i a e S i g b () ...> e "B , " + a e + " !"; ...> ...> c ea ed c a G ee i g Java
he > a g = e G ee i g(" ag
g ==> G ee i g@377dca04
he > g.he
()
$3 ==> "He
, ag
he > g.b
E
:
b
() ha
g.b
()
^-------^
h
h
e")
e!"
()
i a e acce
i
G ee i g
he > G ee i g.c a .ge Dec a edMe h d () $4 ==> Me h d[2] a a. a g.S i g G ee i g.he a. a g.S i g G ee i g.b () he He > a ==> he > He $6 ==> "He (), i a e He = G ee i g.c a .ge Dec a edMe h d("he a a. a g.S i g G ee i g.he () .i , ag e(g) h e!" ") a
he > ") B a B ==> he > E ce B i a a a a he B > he > B $12 ==> "B = G ee i g.c a i a e a a. a g.S .ge Dec a edMe h d("b i g G ee i g.b () .i e(g) a a. a g.I ega Acce E ce i : c a REPL.$JShe Ref ec i . e I ega Acce E ce i (Ref ec i . a a Acce ib eOb ec .chec Acce (Acce ib eOb ec . a a:6 Me h d.i e (Me h d. a a:556) (#10:1) . e Acce ib e( .i e(g) , ag h e) e !"
method missing
Ruby
i b( ai ):001:1* c a
G ee i g
i b( ai ):002:2*
def i i ia i e( a e)
i b( ai ):003:2*
@ a e = a e
i b( ai ):004:1*
e d
i b( ai ):005:2*
def e h d_ i i g( , *a g )
i b( ai ):006:2*
"# .ca i a i e , # @ a e !"
i b( ai ):007:1*
e d
i b( ai ):008:1* e d
=> : e h d_ i i g
i b( ai ):009:0> g = G ee i g. e (" ag
h
e")
=> #<G ee i g:0 00007fcb472e0198 @ a e=" ag
h
i b( ai ):010:0> g.he
=> "He
, ag
h
e!"
i b( ai ):011:0> g.b
=> "B
, ag
h
e!"
e">
open class Ruby i b( ai i b( ai i b( ai i b( ai i b( ai => :he i b( ai => "He ):001:1* c a S i g ):002:2* def he ):003:2* "He , # ):004:1* e d ):005:1* e d ):006:0> " ag , ag h h e!" e f !" e".he
refinements Ruby i b( ai ):001:1* i b( ai ):002:2* i b( ai ):003:3* i b( ai ):004:3* i b( ai ):005:2* i b( ai ):006:1* i b( ai ):007:0> e => #< efi e e :S d e G ee i g efi e S i g d def he "He , # e f !" e d e d d i g@G ee i g>
i b( ai i b( ai i b( ai i b( ai i b( ai i b( ai i b( ai i b( ai => :f ):008:1* ):009:1* ):010:2* ):011:3* ):012:3* ):013:2* ):014:1* ):015:0> e d e M i g G ee i g c a << e f def f( a e) a e.he e d e d d
i b( ai ):016:0> M.f(" ag
h
e")
=> "He
, ag
h
e!"
i b( ai ):017:0> " ag
h
e".he
T acebac (
ece
ca
a ):
4: f
/U e / age
h
e/. be
3: f
/U e / age
h
e/. be
2: f
/U e / age
h
e/. be
1: f
(i b):17:i `< ai >'
N Me h dE
( defi ed e h d `he
' f
/ e
/ e
/ e
" ag
i
i
i
/3.0.0/bi /i b
/3.0.0/bi /i b
/3.0.0/ ib/ b
h
e":S
i
e (Ja aSc i ) im lici cla (Scala)
Ruby
i b( ai ):001:0> a e = " ag
h
e"
=> " ag
h
e"
i b( ai ):002:1* def a e.he
i b( ai ):003:1*
"He
, # e f !"
i b( ai ):004:1* e d
=> :he
i b( ai ):005:0> a e.he
=> "He
, ag
h
e!"
i b( ai ):006:0> " ag
h
e".he
T acebac (
ece
ca
a ):
4: f
/U e / age
h
e/. be
3: f
/U e / age
h
e/. be
2: f
/U e / age
h
e/. be
1: f
(i b):6:i `< ai >'
N Me h dE
( defi ed e h d `he
' f
/ e
/ e
/ e
" ag
i
i
i
/3.0.0/bi /i b
/3.0.0/bi /i b
/3.0.0/ ib/ b
h
e":S
i
Me h dT e (P h ) (Ja aSc i ) (Cl j e)
Clojure e => (def ac e `( he ( @b d )) #' e / e [ e e ) & b d ]
e => ( e ( e i (= 1 2) " e a ")) i (= 1 1) " e a ")) a i e => ( e ( i
e => ( ac '( (c ")) e e a d-1 (= 1 2) ( i " e a "))) e.c e/ he (c e.c e/ (= 1 2)) ( i " e a
Elixir ie (1)> def d e M d ...(1)> def ac e ( e , d : b d ) d ...(1)> e d ...(1)> if ! e( e ) d ...(1)> e(b d ) ...(1)> e d ...(1)> e d ...(1)> e d ...(1)> e d : d e, M, <<70, 79, 82, 49, 0, 0, 5, 120, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 17, 8, 69, 108, 105, 120, 105, 114, 46, 77, 8, 95, 95 102, 111, 95, 95, 10, 97, 116, 116, 114, ...>>, : e , 2
ie (2)> e i e M M ie (3)> M. e 1 == 2 d ...(3)> IO. " e a " ...(3)> e d e a : ie (4)> M. e 1 == 1 d ...(4)> IO. " e a " ...(4)> e d i
ie (5)> e d ...(5)> M. e 1 == 1 d ...(5)> IO. " e a " ...(5)> e d ...(5)> e d > Mac .e a d_ ce(__ENV__) ...(5)> Mac . _ i g > IO. if(!(1 == 1)) d IO. (" e a ") e d : >
R J lia Tem la e Ha kell Scalame a Mac P H S ee .j