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
170
昔の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
2.5k
Perl Quiz
skaji
0
130
cpm at PerlCon 2019
skaji
0
110
Perl5 Package Manager
skaji
0
660
Internal of cpm
skaji
0
570
How to execute external programs in Perl
skaji
3
570
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
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
1.2k
Click-free releases & the making of a CLI app
oheyadam
2
120
LLM生成文章の精度評価自動化とプロンプトチューニングの効率化について
layerx
PRO
2
190
as(型アサーション)を書く前にできること
marokanatani
10
2.7k
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
340
みんなでプロポーザルを書いてみた
yuriko1211
0
260
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
8
540
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
290
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.4k
CSC509 Lecture 11
javiergs
PRO
0
180
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
300
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
880
Featured
See All Featured
Docker and Python
trallard
40
3.1k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
Into the Great Unknown - MozCon
thekraken
32
1.5k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
93
16k
Art, The Web, and Tiny UX
lynnandtonic
297
20k
Automating Front-end Workflow
addyosmani
1366
200k
Code Review Best Practice
trishagee
64
17k
VelocityConf: Rendering Performance Case Studies
addyosmani
325
24k
Faster Mobile Websites
deanohume
305
30k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
How STYLIGHT went responsive
nonsquared
95
5.2k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
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Λͬͯ؆ܿʹॻ͚ΔΑ͏ʹͳͬ ͨɻ