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
150
昔の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
1.6k
Perl Quiz
skaji
0
130
cpm at PerlCon 2019
skaji
0
110
Perl5 Package Manager
skaji
0
650
Internal of cpm
skaji
0
570
How to execute external programs in Perl
skaji
3
560
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
CSC305 Lecture 13
javiergs
PRO
0
120
WEBエンジニア向けAI活用入門
sutetotanuki
0
290
From Subtype Polymorphism To Typeclass-based Ad hoc Polymorphism- An Example
philipschwarz
PRO
0
110
とにかくAWS GameDay!AWSは世界の共通言語!.pdf / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
0
160
Android 15 でアクションバー表示時にステータスバーが白くなってしまう問題
tonionagauzzi
0
110
OpenTelemetryでRailsのパフォーマンス分析を始めてみよう(KoR2024)
ymtdzzz
3
1k
hotwire_or_react
harunatsujita
6
2.8k
約9000個の自動テストの 時間を50分->10分に短縮 Flakyテストを1%以下に抑えた話
hatsu38
21
9k
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
0
120
Go言語でターミナルフレンドリーなAIコマンド、afaを作った/fukuokago20_afa
monochromegane
2
140
Boost Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
440
生成 AI を活用した toitta 切片分類機能の裏側 / Inside toitta's AI-Based Factoid Clustering
pokutuna
0
530
Featured
See All Featured
Six Lessons from altMBA
skipperchong
26
3.4k
It's Worth the Effort
3n
183
27k
Building a Scalable Design System with Sketch
lauravandoore
459
33k
Code Reviewing Like a Champion
maltzj
519
39k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9k
Practical Orchestrator
shlominoach
186
10k
Writing Fast Ruby
sferik
626
60k
Happy Clients
brianwarren
97
6.7k
Code Review Best Practice
trishagee
64
17k
Measuring & Analyzing Core Web Vitals
bluesmoon
0
29
Testing 201, or: Great Expectations
jmmastey
38
7k
A designer walks into a library…
pauljervisheath
202
24k
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Λͬͯ؆ܿʹॻ͚ΔΑ͏ʹͳͬ ͨɻ