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
これでLambdaが不要に?!Step FunctionsのJSONata対応について
iwatatomoya
2
3.7k
命名をリントする
chiroruxx
1
410
Fibonacci Function Gallery - Part 1
philipschwarz
PRO
0
220
情報漏洩させないための設計
kubotak
2
170
htmxって知っていますか?次世代のHTML
hiro_ghap1
0
340
ブラウザ単体でmp4書き出すまで - muddy-web - 2024-12
yue4u
3
470
testcontainers のススメ
sgash708
1
120
Beyond ORM
77web
6
780
17年周年のWebアプリケーションにTanStack Queryを導入する / Implementing TanStack Query in a 17th Anniversary Web Application
saitolume
0
250
nekko cloudにおけるProxmox VE利用事例
irumaru
3
430
採用事例の少ないSvelteを選んだ理由と それを正解にするためにやっていること
oekazuma
2
1k
テストケースの名前はどうつけるべきか?
orgachem
PRO
0
130
Featured
See All Featured
For a Future-Friendly Web
brad_frost
175
9.4k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.9k
Optimising Largest Contentful Paint
csswizardry
33
3k
Side Projects
sachag
452
42k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
2
290
A Tale of Four Properties
chriscoyier
157
23k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Unsuck your backbone
ammeep
669
57k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
Designing for Performance
lara
604
68k
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Λͬͯ؆ܿʹॻ͚ΔΑ͏ʹͳͬ ͨɻ