Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Metaprogramming

Introduction to Metaprogramming

メタプログラミング入門

「魔術」を理解し、効果的に利用しよう!

Kent OHASHI

March 12, 2021
Tweet

More Decks by Kent OHASHI

Other Decks in Programming

Transcript

  1. lagénorhynque (def fi e ag h e :id @ age

    h e : eadi g "/ a e ɛ̃/" :a ia e [" "] : a g age [C e Ha e E g i h f a ai ] :i e e [ g a i g a g age- ea i g a a he a ic ] :c i ["gi h b.c / age h e/d c . d e. ede a " "gi h b.c / age h e/d c . d e.ca bi "] :c ib e ["gi h b.c / a a -c ia /c e- i e- a"])
  2. ( ) ( ) ( ) e c. ( )

    ( ) ( ) e c. e ec i me acla em la e mac
  3. / / e c. DSL ( ) e c. d

    mai - eci c la g age
  4. e g 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.
  5. , Java 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
  6. he > a g = e G ee i g("

    ag h e") g ==> G ee i g@377dca04 he > g.he () $3 ==> "He , ag h e!" he > g.b () E : b () ha i a e acce i G ee i g g.b () ^-------^
  7. 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 (), i a e a a. a g.S i g G ee i g.b () he > a He = G ee i g.c a .ge Dec a edMe h d("he ") He ==> a a. a g.S i g G ee i g.he () he > He .i e(g) $6 ==> "He , ag h e!"
  8. he > a B = G ee i g.c a

    .ge Dec a edMe h d("b ") B ==> i a e a a. a g.S i g G ee i g.b () he > B .i e(g) E ce i a a. a g.I ega Acce E ce i : c a REPL.$JShe a Ref ec i . e I ega Acce E ce i (Ref ec i . a a a Acce ib eOb ec .chec Acce (Acce ib eOb ec . a a:6 a Me h d.i e (Me h d. a a:556) a (#10:1) he > B . e Acce ib e( e) he > B .i e(g) $12 ==> "B , ag h e !"
  9. Ruby method missing 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 e"> i b( ai ):010:0> g.he => "He , ag h e!" i b( ai ):011:0> g.b => "B , ag h e!"
  10. open class Ruby i b( ai ):001:1* c a S

    i g i b( ai ):002:2* def he i b( ai ):003:2* "He , # e f !" i b( ai ):004:1* e d i b( ai ):005:1* e d => :he i b( ai ):006:0> " ag h e".he => "He , ag h e!"
  11. Ruby refinements i b( ai ):001:1* d e G ee

    i g i b( ai ):002:2* efi e S i g d i b( ai ):003:3* def he i b( ai ):004:3* "He , # e f !" i b( ai ):005:2* e d i b( ai ):006:1* e d i b( ai ):007:0> e d => #< efi e e :S i g@G ee i g>
  12. i b( ai ):008:1* d e M i b( ai

    ):009:1* i g G ee i g i b( ai ):010:2* c a << e f i b( ai ):011:3* def f( a e) i b( ai ):012:3* a e.he i b( ai ):013:2* e d i b( ai ):014:1* e d i b( ai ):015:0> e d => :f
  13. 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 / e i /3.0.0/bi /i b 3: f /U e / age h e/. be / e i /3.0.0/bi /i b 2: f /U e / age h e/. be / e i /3.0.0/ ib/ b 1: f (i b):17:i `< ai >' N Me h dE ( defi ed e h d `he ' f " ag h e":S i
  14. 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 / e i /3.0.0/bi /i b 3: f /U e / age h e/. be / e i /3.0.0/bi /i b 2: f /U e / age h e/. be / e i /3.0.0/ ib/ b 1: f (i b):6:i `< ai >' N Me h dE ( defi ed e h d `he ' f " ag h e":S i
  15. Clojure e => (def ac e [ e & b

    d ] `( he ( e ) @b d )) #' e / e
  16. e => ( e (= 1 2) ( i "

    e a ")) e a i e => ( e (= 1 1) ( i " e a ")) i
  17. e => ( ac e a d-1 '( e (=

    1 2) ( i " e a "))) (c e.c e/ he (c e.c e/ (= 1 2)) ( i " e a "))
  18. 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
  19. 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
  20. 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 :