Slide 1

Slide 1 text

1 slices/maps pkgΛ׆༻ͯ͠ΦϨΦϨ࣮૷Λ๾໓͍ͨ͠ OPTiM × every Golang Developer Night 2024/07/02 גࣜձࣾΤϒϦʔ ayaka.yoshida

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Copyright © 2015 every, Inc. All rights reserved. 3 ࣗݾ঺հ ■໊લɿayaka.yoshida ■ॴଐɿτϞχς ■ग़਎ɿେࡕ෎ਧాࢢ ■ܦྺɿ ɾ2020.4-2022.6ɹอݥ୅ཧళ ๏ਓӦۀ ɾ2023.2- ΤϒϦʔೖࣾ

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Copyright © 2015 every, Inc. All rights reserved. 6 ؾʹͳΔؔ਺ΛϐοΫΞοϓ Slices func Index func Insert func SortFunc func Contains / ContainsFunc Map func Keys func Values

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

Copyright © 2015 every, Inc. All rights reserved. 8 Keysؔ਺Λ࢖ΘͣmapͷΩʔΛฦ͢ํ๏

Slide 9

Slide 9 text

Copyright © 2015 every, Inc. All rights reserved. 9 Keys, Valuesؔ਺Λ࢖͏৔߹

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Copyright © 2015 every, Inc. All rights reserved. 13 appendؔ਺ͰεϥΠε಺ͷ೚ҙͷҐஔʹ஋Λ௥Ճ

Slide 14

Slide 14 text

Copyright © 2015 every, Inc. All rights reserved. 14 Insertؔ਺Λ࢖ͬͯεϥΠε಺ͷ೚ҙͷҐஔʹ஋Λ௥Ճ

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Copyright © 2015 every, Inc. All rights reserved. 17 slices.SortFuncͷ࢖͍ํ

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

Copyright © 2015 every, Inc. All rights reserved. 20 ࣮ࡍʹӡ༻͍ͯ͠ΔίʔυͰஔ͖׵͑ͯΈͨ

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

No content