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
昔のGo, 今のGo ~比較, ソート編~
Search
Shoichi Kaji
May 17, 2024
Programming
0
180
昔のGo, 今のGo ~比較, ソート編~
kamakura.go #6, 2024-05-17
Shoichi Kaji
May 17, 2024
Tweet
Share
More Decks by Shoichi Kaji
See All by Shoichi Kaji
宿泊予約サイトにおける検索と料金計算の両立
skaji
2
3.6k
Perl Quiz
skaji
0
130
cpm at PerlCon 2019
skaji
0
110
Perl5 Package Manager
skaji
0
660
Internal of cpm
skaji
0
580
How to execute external programs in Perl
skaji
3
580
Writing Perl with w0rp/ale
skaji
0
2.2k
nginx unit
skaji
0
44
Perl6 ecosystem
skaji
1
1.4k
Other Decks in Programming
See All in Programming
Go の GC の不得意な部分を克服したい
taiyow
2
780
創造的活動から切り拓く新たなキャリア 好きから始めてみる夜勤オペレーターからSREへの転身
yjszk
1
130
StarlingMonkeyを触ってみた話 - 2024冬
syumai
3
270
From Translations to Multi Dimension Entities
alexanderschranz
2
130
Scalaから始めるOpenFeature入門 / Scalaわいわい勉強会 #4
arthur1
1
320
テストコード文化を0から作り、変化し続けた組織
kazatohiei
2
1.5k
fs2-io を試してたらバグを見つけて直した話
chencmd
0
230
テストケースの名前はどうつけるべきか?
orgachem
PRO
0
130
PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform
ttskch
0
210
The Efficiency Paradox and How to Save Yourself and the World
hollycummins
1
440
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
140
103 Early Hints
sugi_0000
1
230
Featured
See All Featured
Mobile First: as difficult as doing things right
swwweet
222
9k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
How To Stay Up To Date on Web Technology
chriscoyier
789
250k
Code Reviewing Like a Champion
maltzj
520
39k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.9k
4 Signs Your Business is Dying
shpigford
181
21k
Into the Great Unknown - MozCon
thekraken
33
1.5k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
127
18k
How STYLIGHT went responsive
nonsquared
95
5.2k
Raft: Consensus for Rubyists
vanstee
137
6.7k
Speed Design
sergeychernyshev
25
670
Transcript
ੲͷGo, ࠓͷGo ~ ൺֱ, ιʔτฤ ~ Shoichi Kaji
ൺֱ, ιʔτؔ࿈ͷมߋ • go1.18 generics • go1.20 time.Compare • go1.21
slices package, cmp package • go1.22 cmp.Or
time.Compare • time.Timeʹ࣌ࠁ͕"લ"͔൱͔Λฦ͢Beforeϝιου͕͋Δ: func (t Time) Before(t2 Time) bool •
go1.20ͰCompareϝιου͕Ճ͞Εͨ: func (t Time) Compare(t2 Time) int • t͕t2ΑΓ"લ"ͳΒ: -1 t͕t2ͱ"͍͠"ͳΒ: 0 t͕t2ΑΓ"ޙΖ"ͳΒ: +1
time.Compare • ࠓ·Ͱt͕t2 "Ҏલ"Λ͍ࣔͨ͠ͱ͖ t.Before(t2) || t.Equal(t2) ͋ ͱॻ͔ͳ͚Ε͍͚ͳ͔ͬͨͱ͜Ζ t.Compare(t2)
<= 0 ͋ ͱॻ͚ΔΑ͏ʹͳͬͨ 👍
slices.Sort/SortFunc • sort packageʹSlice,Sort͕ؔఆٛ͞Ε͍ͯΔ: func Slice(x any, less func(i, j
int) bool) func Sort(x Interface) • εϥΠεͷιʔτ্هͷͲͪΒ͔Λ͏ͷ͕Ұൠతͩͬͨ • Slice: εϥΠεΛanyͰड͚औ͍ͬͯΔ 🥲 • Sort: sort.InterfaceΛຬͨ͢ɺLen, Less, Swapϝιου Λ࣮ͨ͠ܕΛΘ͟Θ͟࡞Βͳ͍ͱ͍͚ͳ͍ 🥲
slices.Sort/SortFunc • go1.21Ͱೖͬͨslices packageʹgenericsΛͬͨε ϥΠεͷιʔτ͕ؔఆٛ͞Ε͍ͯΔ func Sort[S ~[]E, E cmp.Ordered](x
S) func SortFunc[S ~[]E, E any](x S, compare func(a, b E) int) • ܕ͕͍ͭͨঢ়ଶͰεϥΠεΛड͚औΔ 😊 • ιʔτ݅Λࣔ͢Ҿͷ͕ؔɺless͔ΒcompareʹมΘ͍ͬͯΔ ͜ͱʹ͍ͨ͠ • ࠓޙsort.Sort/SliceͰͳͪ͘͜ΒΛ͏ͱΑͦ͞͏
ෳͷιʔτ݅ • ෳͷιʔτ݅compare1,2,3ΛΈ߹Θͤෳࡶ ͳιʔτ݅Λॻ͘͜ͱ͕͋Δ: • compare := func(e1, e2 E)
int { if c := compare1(e1, e2); c != 0 { return c } if c := compare2(e1, e2); c != 0 { return c } return compare3(e1, e2) }
ෳͷιʔτ݅ • go1.21Ͱcmp.Or͕ಋೖ͞Εͨɻ͜ΕෳͷҾΛड͚औΓɺड͚औͬͨҾ ͷதͰ࠷ॳʹzeroͰͳ͔ͬͨҾΛฦؔ͢Ͱ͋Δɻ • ͜ΕΛ͏ͱઌ΄Ͳͷcompare؆ܿʹॻ͚Δ 👍 • ͨͩ͠cmp.OrͪΖΜབྷతͰͳ͍ɻ ͕ͦ͜ॏཁͳͱ͖Ҏલͷॻ͖ํΛ͖͢ɻ
compare := func(e1, e2 E) int { return cmp.Or( compare1(e1, e2), compare2(e1, e2), compare3(e1, e2), ) }
·ͱΊ • time.Compare͕ಋೖ͞ΕͨΓɺslices.SortFunc͕CompareΛ ཁٻ͍ͯ͠Δ͜ͱΛݟΔʹɺ "ൺֱ"ϝιου/ؔCompare͕ελϯμʔυʹͳ͍͖ͬͯͦ ͏ɻࣗͰ࡞ͬͨܕʹൺֱϝιουΛੜ͍ͨ͠ͱ͖ɺ Compare(e2 E) intΛ༻ҙ͢ΔͱΑͦ͞͏ɻ •
sort.Sort/SlicesͰͳ͘ɺslices.Sort/SortFuncΛ͏ͱΑ͍ɻ • ෳͷιʔτ݅cmp.OrΛͬͯ؆ܿʹॻ͚ΔΑ͏ʹͳͬ ͨɻ