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
pp and reflect
Search
Takashi Kokubun
June 21, 2015
Programming
6
11k
pp and reflect
Go Conference 2015 summer
Takashi Kokubun
June 21, 2015
Tweet
Share
More Decks by Takashi Kokubun
See All by Takashi Kokubun
YJIT Makes Rails 1.7x faster / RubyKaigi 2024
k0kubun
7
13k
Ruby JIT Hacking Guide / RubyKaigi 2023
k0kubun
2
9.5k
YJIT: Dive into Ruby's JIT compiler written in Rust / Rust.Tokyo 2022
k0kubun
1
2k
Towards Ruby 4 JIT / RubyKaigi 2022
k0kubun
3
11k
Optimizing Production Performance with MRI JIT / RubyConf 2021
k0kubun
1
420
Why Ruby's JIT was slow / RubyKaigi Takeout 2021
k0kubun
3
1.9k
数時間かかる週一リリースを毎日何度も爆速でできるようにするまで / CI/CD Conference 2021
k0kubun
21
14k
Ruby 3 JIT's roadmap / RubyConf China 2020
k0kubun
0
770
Ruby 3.0 JIT on Rails
k0kubun
9
9.1k
Other Decks in Programming
See All in Programming
短期間での新規プロダクト開発における「コスパの良い」Goのテスト戦略」 / kamakura.go
n3xem
2
180
Kaigi on Railsに初参加したら、その日にLT登壇が決定した件について
tama50505
0
110
Online-Dokumentation, die hilft: Strukturen, Prozesse, Tools
ahus1
0
100
競技プログラミングへのお誘い@阪大BOOSTセミナー
kotamanegi
0
360
採用事例の少ないSvelteを選んだ理由と それを正解にするためにやっていること
oekazuma
2
1.1k
これが俺の”自分戦略” プロセスを楽しんでいこう! - Developers CAREER Boost 2024
niftycorp
PRO
0
200
KubeCon + CloudNativeCon NA 2024 Overviewat Kubernetes Meetup Tokyo #68 / amsy810_k8sjp68
masayaaoyama
0
260
StarlingMonkeyを触ってみた話 - 2024冬
syumai
3
280
Fibonacci Function Gallery - Part 1
philipschwarz
PRO
0
220
iOS開発におけるCopilot For XcodeとCode Completion / copilot for xcode
fuyan777
1
110
フロントエンドのディレクトリ構成どうしてる? Feature-Sliced Design 導入体験談
osakatechlab
8
4.1k
わたしの星のままで一番星になる ~ 出産を機にSIerからEC事業会社に転職した話 ~
kimura_m_29
0
200
Featured
See All Featured
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
520
How STYLIGHT went responsive
nonsquared
96
5.2k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
GitHub's CSS Performance
jonrohan
1031
460k
Music & Morning Musume
bryan
46
6.2k
Facilitating Awesome Meetings
lara
50
6.1k
Become a Pro
speakerdeck
PRO
26
5k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
For a Future-Friendly Web
brad_frost
175
9.4k
We Have a Design System, Now What?
morganepeng
51
7.3k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Transcript
pp and reflect Go Conference 2015 summer Takashi Kokubun
Who is k0kubun? • Twitter, GitHub: @k0kubun • Cookpad •
Rubyist
k0kubun/pp Colored Pretty Printer https://github.com/k0kubun/pp
k0kubun/pp • Prettily print any complex data structures • Simple
APIs like “fmt” import "github.com/k0kubun/pp" for _, tweet := range tweets { pp.Println(tweet) }
k0kubun/pp • Prettily print any complex data structures • Simple
APIs like “fmt”
k0kubun/pp ͷෆศͳॴ • ᶃ import͢ΔURL͕ࢥ͍ग़ͤͳ͍ • ᶄ import͢͠ͷ͕ΊΜͲ͍͘͞
import͢ΔURL͕ࢥ͍ग़ͤͳ͍ Ϣʔβʔͷෆຬᶃ
import͢ΔURL͕ࢥ͍ग़ͤͳ͍ import "github.com/◦◦◦/pp" ↑ ͜ͷਓ୭͚ͩͬ…
import͢ΔURL͕ࢥ͍ग़ͤͳ͍ ppͷcontributorͷํ
import͢ΔURL͕ࢥ͍ग़ͤͳ͍ ձࣾͷઌഐ ͍͍ͪͪιʔείʔυʹ k0kubun͞Μͷ໊લॻ͖ͨ͘ͳ͍Ͱ͢Ͷ…
go-pp/ppͱ͍͏forkΛimport import “github.com/go-pp/pp" or import “gopkg.in/pp.v2” ղܾํ๏ɿ
import͢͠ͷ͕ΊΜͲ͍͘͞ Ϣʔβʔͷෆຬᶄ
import͢͠ͷ͕ΊΜͲ͍͘͞ import "github.com/k0kubun/pp" ↑ pp.Println() Λॻ͍ͨΓফͨ͠Γ͢ ΔͨͼʹՃ͢͠ඞཁ͕͋Δ
ؔΛϥοϓͯ͠gitignore import p "github.com/k0kubun/pp" func pp(a ...interface{}) { p.Println(a...) }
• ʮ,*ʯΛgitignoreͯ͠ΔͳΒʮ,.goʯͱ͔ • ͬͯͳͯ͘importফ͞ͳͯ͘ࡁΉ ղܾํ๏ɿ
package “reflect” Standard library for reflection k0kubun/pp is powered by
“reflect”
What’s the usage of reflect? • ͷϝλใΛಡΉ • ppͩͱstructͷશfieldྻڍ •
encoding/jsonͩͱstructͷfieldͷλά • ϝλϓϩάϥϛϯά
ϝλϓϩάϥϛϯάͷಛ • εϚʔτͳॻ͖ํͰίʔυͷߦΛݮΒͤΔ • ॻָ͍͍ͯͯ͠ • ੩తղੳ͕͘͠ɺಡΈʹ͘͘ͳΔ • झຯҎ֎ͰΘͳ͍ͷ͕ແ
reflectͰͰ͖Δͷ࣍ͷ͏ͪͲΕʁ Go ϝλϓϩάϥϛϯάΫΠζ
reflectͰͰ͖Δͷ࣍ͷ͏ͪͲΕʁ ૢ࡞ ճ ؔఆٛ ؔݺͼग़͠ ϝιουఆٛ ϝιουݺͼग़͠ ҙͷܕͷOFX ˕ɿstringͰ໊લΛࢦఆՄɹ˓ɿreflect.Value͔ΒՄೳɹ×ɿແཧ ˕ɺ˓ɺ×
ώϯτ: Rubyશ෦˕
reflectͰͰ͖Δͷ࣍ͷ͏ͪͲΕʁ ૢ࡞ ճ ؔఆٛ ˓ ؔݺͼग़͠ ˓ ϝιουఆٛ º ϝιουݺͼग़͠
˕ ҙͷܕͷOFX ˓ ˕ɿstringͰ໊લΛࢦఆՄɹ˓ɿreflect.Value͔ΒՄೳɹ×ɿແཧ ϝλϓϩՄ
reflectʹΑΔؔఆٛ • ҙͷγάωνϟͷؔΛ࡞ΕΔ var f func(int, int) int v :=
reflect.MakeFunc( reflect.TypeOf(f), func(in []reflect.Value) []reflect.Value { return []reflect.Value{in[0]} }, ) reflect.ValueOf(&f).Elem().Set(v)
reflectʹΑΔؔݺͼग़͠ • ҙͷγάωνϟͷؔΛݺͼग़ͤΔ f := func(a int, b int) int
{ return a + b } v := reflect.ValueOf(f) in := []reflect.Value{} for i := 0; i < v.Type().NumIn(); i++ { in = append(in, reflect.ValueOf(1)) } v.Call(in)
reflectʹΑΔϝιουఆٛ • ී௨ʹແཧ
reflectʹΑΔϝιουݺͼग़͠ • stringͰ໊લΛࢦఆͯ͠ϝιουΛݺͼग़ͤΔ type Hoge struct{} func (h Hoge) Foo()
{ print("foo") } func (h Hoge) Bar() { print("bar") } h := new(Hoge) for _, name := range []string{"Foo", "Bar"} { v := reflect.ValueOf(h) v.MethodByName(name).Call([]reflect.Value{}) }
reflectʹΑΔnew • ҙͷܕͷΛॳظԽͨ͠ͷΛ࡞ΕΔ t := reflect.TypeOf(a) reflect.New(t)
·ͱΊ • k0kubun/ppͬͯԼ͍͞ • pp.Println() Λϥοϓͯ͠gitignore͓ͯ͘͠ • reflect͏ͱϝλϓϩͰ͖Δ • stringͰࢦఆͰ͖Δͷϝιουݺͼग़͠ͷΈ