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를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
170
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
190
ペアーズにおけるAmazon Bedrockを⽤いた障害対応⽀援 ⽣成AIツールの導⼊事例 @ 20241115配信AWSウェビナー登壇
fukubaka0825
6
2k
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
1k
ヤプリ新卒SREの オンボーディング
masaki12
0
130
みんなでプロポーザルを書いてみた
yuriko1211
0
280
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1.1k
GitHub Actionsのキャッシュと手を挙げることの大切さとそれに必要なこと
satoshi256kbyte
5
430
距離関数を極める! / SESSIONS 2024
gam0022
0
290
C++でシェーダを書く
fadis
6
4.1k
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
10
1.3k
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
250
Featured
See All Featured
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.8k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
47
2.1k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
0
100
What's new in Ruby 2.0
geeforr
343
31k
Navigating Team Friction
lara
183
14k
The Art of Programming - Codeland 2020
erikaheidi
52
13k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
How to Ace a Technical Interview
jacobian
276
23k
Site-Speed That Sticks
csswizardry
0
28
Making Projects Easy
brettharned
115
5.9k
Side Projects
sachag
452
42k
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Λͬͯ؆ܿʹॻ͚ΔΑ͏ʹͳͬ ͨɻ