Upgrade to Pro — share decks privately, control downloads, hide ads and more …

slices/maps pkgを活用してオレオレ実装を撲滅したい

Ayaka Yoshida
July 08, 2024
1.1k

slices/maps pkgを活用してオレオレ実装を撲滅したい

OPTiM × every Golang Developer Night 2024/07/02
https://optim.connpass.com/event/321331/

Ayaka Yoshida

July 08, 2024
Tweet

Transcript

  1. Copyright © 2015 every, Inc. All rights reserved. 2 ΞδΣϯμ

    - ࣗݾ঺հ - ௥Ճ͞Εͨؔ਺ͰؾʹͳΔ΋ͷΛϐοΫΞοϓ - ӡ༻͞Ε͍ͯΔίʔυͰஔ͖׵͑ - ·ͱΊ
  2. Copyright © 2015 every, Inc. All rights reserved. 3 ࣗݾ঺հ

    ▪໊લɿayaka.yoshida ▪ॴଐɿτϞχς ▪ग़਎ɿେࡕ෎ਧాࢢ ▪ܦྺɿ ɾ2020.4-2022.6ɹอݥ୅ཧళ ๏ਓӦۀ ɾ2023.2- ΤϒϦʔೖࣾ
  3. Copyright © 2015 every, Inc. All rights reserved. 4 slicesɺmapsύοέʔδ௥Ճͷissue͸2021೥5݄ɺ2021೥8݄ʹΦʔϓϯ😳

    ※ͦΕͧΕgolang.org/x/exp/slicesɺgolang.org/x/exp/mapsͱ͍͏࣮ݧతͳύοέʔδͰ͸1.18 ʢ2022-03-15ʣʹ௥Ճ͞Ε͍ͯͨ Go1.21 Release Notesʢ2023-08-08ʣ Go 1.21Ͱslices, mapsύοέʔδ͕௥Ճ͞Ε·ͨ͠ʂ https://tip.golang.org/doc/go1.21 https://github.com/golang/go/issues/45955 https://github.com/golang/go/issues/57433 https://github.com/golang/go/issues/47649 https://github.com/golang/go/issues/57436 https://tip.golang.org/doc/go1.18
  4. Copyright © 2015 every, Inc. All rights reserved. 5 slices

    func All[Slice ~[]E, E any](s Slice) iter.Seq2[int, E] func AppendSeq[Slice ~[]E, E any](s Slice, seq iter.Seq[E]) Slice func Backward[Slice ~[]E, E any](s Slice) iter.Seq2[int, E] func BinarySearch[S ~[]E, E cmp.Ordered](x S, target E) (int, bool) func BinarySearchFunc[S ~[]E, E, T any](x S, target T, cmp func(E, T) int) (int, bool) func Chunk[Slice ~[]E, E any](s Slice, n int) iter.Seq[Slice] func Clip[S ~[]E, E any](s S) S func Clone[S ~[]E, E any](s S) S func Collect[E any](seq iter.Seq[E]) []E func Compact[S ~[]E, E comparable](s S) S func CompactFunc[S ~[]E, E any](s S, eq func(E, E) bool) S func Compare[S ~[]E, E cmp.Ordered](s1, s2 S) int func CompareFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, cmp func(E1, E2) int) int func Concat[S ~[]E, E any](slices ...S) S func Contains[S ~[]E, E comparable](s S, v E) bool func ContainsFunc[S ~[]E, E any](s S, f func(E) bool) bool func Delete[S ~[]E, E any](s S, i, j int) S func DeleteFunc[S ~[]E, E any](s S, del func(E) bool) S func Equal[S ~[]E, E comparable](s1, s2 S) bool func EqualFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, eq func(E1, E2) bool) bool func Grow[S ~[]E, E any](s S, n int) S func Index[S ~[]E, E comparable](s S, v E) int func IndexFunc[S ~[]E, E any](s S, f func(E) bool) int func Insert[S ~[]E, E any](s S, i int, v ...E) S func IsSorted[S ~[]E, E cmp.Ordered](x S) bool func IsSortedFunc[S ~[]E, E any](x S, cmp func(a, b E) int) bool func Max[S ~[]E, E cmp.Ordered](x S) E func MaxFunc[S ~[]E, E any](x S, cmp func(a, b E) int) E func Min[S ~[]E, E cmp.Ordered](x S) E func MinFunc[S ~[]E, E any](x S, cmp func(a, b E) int) E func Repeat[S ~[]E, E any](x S, count int) S func Replace[S ~[]E, E any](s S, i, j int, v ...E) S func Reverse[S ~[]E, E any](s S) func Sort[S ~[]E, E cmp.Ordered](x S) func SortFunc[S ~[]E, E any](x S, cmp func(a, b E) int) func SortStableFunc[S ~[]E, E any](x S, cmp func(a, b E) int) func Sorted[E cmp.Ordered](seq iter.Seq[E]) []E func SortedFunc[E any](seq iter.Seq[E], cmp func(E, E) int) []E func SortedStableFunc[E any](seq iter.Seq[E], cmp func(E, E) int) []E func Values[Slice ~[]E, E any](s Slice) iter.Seq[E] ௥Ճ͞Εͨؔ਺ͨͪ maps func All[Map ~map[K]V, K comparable, V any](m Map) iter.Seq2[K, V] func Clone[M ~map[K]V, K comparable, V any](m M) M func Collect[K comparable, V any](seq iter.Seq2[K, V]) map[K]V func Copy[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](dst M1, src M2) func DeleteFunc[M ~map[K]V, K comparable, V any](m M, del func(K, V) bool) func Equal[M1, M2 ~map[K]V, K, V comparable](m1 M1, m2 M2) bool func EqualFunc[M1 ~map[K]V1, M2 ~map[K]V2, K comparable, V1, V2 any](m1 M1, m2 M2, eq func(V1, V2) bool) bool func Insert[Map ~map[K]V, K comparable, V any](m Map, seq iter.Seq2[K, V]) func Keys[Map ~map[K]V, K comparable, V any](m Map) iter.Seq[K] func Values[Map ~map[K]V, K comparable, V any](m Map) iter.Seq[V] https://pkg.go.dev/slices@master https://pkg.go.dev/maps@master ௥Ճ͞Εͨؔ਺ slices: 40 maps: 10
  5. Copyright © 2015 every, Inc. All rights reserved. 6 ؾʹͳΔؔ਺ΛϐοΫΞοϓ

    Slices func Index func Insert func SortFunc func Contains / ContainsFunc Map func Keys func Values
  6. Copyright © 2015 every, Inc. All rights reserved. 7 return:

    mapͷΩʔΛฦ͢ KeysΛ࢖Θͣಉ༷ʹΩʔΛฦ͢ํ๏ https://go.dev/play/p/dzRfw-UIj0b func Keys[M ~map[K]V, K comparable, V any](m M) []K ؾʹͳΔؔ਺ΛϐοΫΞοϓ return: mapͷόϦϡʔΛฦ͢ KeysΛ࢖Θͳ͍࣌ͷόϦϡʔόʔδϣϯͰ࣮ݱՄೳ func Values[M ~map[K]V, K comparable, V any](m M) []V mapsύοέʔδΛ࢖͏ͱ...(Πϯϙʔτํ๏஫ҙ) https://go.dev/play/p/TZImsujd2nw
  7. Copyright © 2015 every, Inc. All rights reserved. 10 return:

    sͷதͰv͕࠷ॳʹݱΕΔΠϯσοΫεɻଘࡏ͠ͳ͚Ε͹−1Λฦ͢ func Index[S ~[]E, E comparable](s S, v E) int https://cs.opensource.google/go/go/+/master:src/ slices/slices.go;l=95
  8. Copyright © 2015 every, Inc. All rights reserved. 11 return:

    s಺Ͱf(s[i])Λຬͨ͢Ұ൪ॳΊͷΠϯσοΫεɻଘࡏ͠ͳ͚Ε͹−1Λฦ͢ func IndexFunc[S ~[]E, E any](s S, f func(E) bool) int goplayground https://cs.opensource.google/go/go/+/ master:src/slices/slices.go;l=106
  9. Copyright © 2015 every, Inc. All rights reserved. 12 return:

    εϥΠεs಺ͷ೚ҙͷindex(i)ͷҐஔʹ஋Λ௥Ճͨ͠εϥΠε εϥΠεʹ஋௥Ճͱ͍͏ͱappendؔ਺͕ࢥ͍ු͔Ϳ💡 insertؔ਺ͷ஀ੜʹΑͬͯԿ͕Α͘ͳΔͷ͔࣮ࡍʹ͔֬ΊΔʂ ex.) appendؔ਺ͰεϥΠε಺ͷ೚ҙͷҐஔʹ஋Λ௥Ճ͢Δ৔߹ ɹɹhttps://go.dev/play/p/osHa5VL4XaI ex.) Insertؔ਺Λ࢖ͬͯεϥΠε಺ͷ೚ҙͷҐஔʹ஋Λ௥Ճ͢Δ৔߹ ɹɹhttps://go.dev/play/p/18VN9IXgYtW func Insert[S ~[]E, E any](s S, i int, v ...E) S https://pkg.go.dev/slices@master#Insert
  10. Copyright © 2015 every, Inc. All rights reserved. 15 ˕Insertؔ਺Λ࢖͏࢖Θͳ͍ͷࠩ͸ྺવ

    appendؔ਺ͷΈͰͷૠೖͷ৔߹͸ҎԼͷέʔεΛ૝ఆ͠ϋϯυϦϯάඞཁ͕͋Δ ⚪︎ ΠϯσοΫεi͕sͷൣғΛ௒͍͑ͯΔ৔߹ ⚪︎ ௥ՃલͷεϥΠεͷΩϟύγςΟ͕௥ՃޙͷΩϟύγςΟΑΓখ͍͞৔߹ ⚪︎ εϥΠεsͱv͕ॏෳ͢Δ৔߹ʢex. s := [a, b, c], i=1, v = s[2:]ʣ ɹ ɹ↓ ͜ͷลΓΛߟྀͯ͠Insertؔ਺͸࡞੒͞Ε͍ͯΔͷͰInsertؔ਺Λར༻ͨ͠ํ͕͓खܰʂ ʢ຤ඌʹ௥Ճͷ࣌Ҏ֎ʣ func Insert[S ~[]E, E any](s S, i int, v ...E) S
  11. Copyright © 2015 every, Inc. All rights reserved. 16 return:

    εϥΠε x Λ cmp ؔ਺ʹΑܾͬͯఆ͞Εͨঢॱʹιʔτ͢Δɻ xͷཁૉ͸cmp.OrderedΛຬͨ͢ඞཁ͋Γɻʢԋࢉࢠ< <= >= >ͰൺֱͰ͖Δʣ cmp΋1.21Ͱ௥Ճ͞Εͨύοέʔδ↓ ࢖͍ํɿhttps://go.dev/play/p/c6xIbaBfcrn γϯϓϧʹεϥΠεͷཁૉΛฒͼସ͑Ͱ͋Ε͹func Sort[S ~[]E, E cmp.Ordered](x S)Λ࢖ͬ ͯ΋ʂ func SortFunc[S ~[]E, E any](x S, cmp func(a, b E) int)ɹ https://pkg.go.dev/slices#SortFunc https://pkg.go.dev/cmp@master https://pkg.go.dev/slices#Sort
  12. Copyright © 2015 every, Inc. All rights reserved. 18 sort.Sortͱͷҧ͍͸...?

    func Sort(data Interface) return: LessϝιουͰܾఆ͞ΕͨঢॱͰσʔλΛιʔτ͢Δɻ Ҿ਺͸data Interfaceɺࣗ෼ͰLen, Less, SwapΛ࣮૷͢Δඞཁ͕͋Δʂ ͦͷ఺ɺslices.SortFunc͸ൺֱ༻ؔ਺ͷ࣮૷ෆཁʂ “Note: the newer slices.SortFunc function is more ergonomic and runs faster. 👉 ࠓޙฒ΂ସ͑ͷඞཁ͕͋Δ࣌ʹ͸slices.SortFuncΛ࢖͏ํ͕ྑͦ͞͏ʂ func SortFunc[S ~[]E, E any](x S, cmp func(a, b E) int)ɹ https://pkg.go.dev/sort@master#Sort
  13. Copyright © 2015 every, Inc. All rights reserved. 19 return:

    Contains: εϥΠεsʹvؚ͕·Ε͍ͯΔ͔ ContainsFunc: εϥΠεs಺Ͱf(s[i])Λຬͨ͢΋ͷ͕গͳ͘ͱ΋1ͭ͋Δ͔ func Contains[S ~[]E, E comparable](s S, v E) bool func ContainsFunc[S ~[]E, E any](s S, f func(E) bool) bool https://cs.opensource.google/go/go/+/ master:src/slices/slices.go;l=116
  14. Copyright © 2015 every, Inc. All rights reserved. 21 ࣮ࡍʹӡ༻͍ͯ͠ΔίʔυͰஔ͖׵͑ͯΈͨ

    ஔ͖׵͑ΔϝϦοτ - ࣮૷ऀ͝ͱʹҟͳΔॲཧ͕ൃੜ͠ͳ͍ util/slice.goͰContainsϝιου͕ఆٛ͞Ε͍ͯͨɻ ͔͠͠slice.ContainsΛ࢖Θͣʹॲཧ಺ʹinArray͕͞Ε͍ͯΔ΋ͷ΋͋ͬͨ(×4)
  15. Copyright © 2015 every, Inc. All rights reserved. 22 ·ͱΊ

    ͜Ε·Ͱ͸append΍copyΛۦ࢖ͯ͠ૢ࡞͍ͨ͠εϥΠε͝ͱʹؔ਺Λఆٛ͢Δඞཁ͕͋Γ·͠ ͕ͨslices, mapsύοέʔδͰରԠͰ͖ΔΑ͏ʹͳΓ·ͨ͠ʂ SliceTricksͱ͍͏wiki΋ଘࡏ͓ͯ͠ΓɺΑ͏΍͔͘ͱ͍ͬͨײ͔͡΋͠Ε·ͤΜʂ ͜Ε͔Β͸slices, mapsύοέʔδΛ༗ޮ׆༻͍͖͍ͯͨ͠Ͱ͢ʂ