【Go言語】クロージャ
by
Tomoki Ota
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
Tomoki Ota ʲGoݴޠʳΫϩʔδϟ࣮ફฤ
Slide 2
Slide 2 text
Ϋϩʔδϟ࣮ફฤ GoͷΠςϨʔλ ΞδΣϯμ 2
Slide 3
Slide 3 text
Ϋϩʔδϟͱʁ
Slide 4
Slide 4 text
Ϋϩʔδϟͱʁ 4 •ڥΛΩϟϓνϟͰ͖Δແ໊ؔ
Slide 5
Slide 5 text
ϝϦοτ 5 • άϩʔόϧมΛ༻͍Δඞཁ͕ͳ͘ͳΔ • ؔͷείʔϓΛ੍ݶͰ͖Δ
Slide 6
Slide 6 text
Ϋϩʔδϟͷ߹ 6
Slide 7
Slide 7 text
ؔͷ߹ 7
Slide 8
Slide 8 text
Ϋϩʔδϟ࣮ફฤ
Slide 9
Slide 9 text
9 ࠨͷίʔυͷ ʁ
Slide 10
Slide 10 text
10 1 ॏ͍ॲཧcalcTrainingRepsͷݺͼग़͠෦͕ ෳՕॴ͋ΔͷͰɺมߋ͕໘
Slide 11
Slide 11 text
11 2 ॏ͍ॲཧΛෳճ࣮ߦ͠ͳ͚ΕͳΒͳ͍ Մೳੑ͕͋Δɻ
Slide 12
Slide 12 text
12 ؔͰϦϑΝΫλ
Slide 13
Slide 13 text
13 ؔͰϦϑΝΫλ ͜Εͩͱ2͕ղܾ͍ͯ͠ͳ͍ɻ ͞ΒʹɺrepsΛ༻͠ͳ͍ͱ͖ ॏ͍ॲཧΛݺͼͩ͞ΕΔͱ͍͏৽ͨͳ͕ Ճ͞ΕΔɻ
Slide 14
Slide 14 text
14 ΫϩʔδϟͰϦϑΝΫλ ΫϩʔδϟΛ͏͜ͱͰɺreps͕ඞཁͳ͚࣌ͩ ίʔυΛ࣮ߦ͢Δ
Slide 15
Slide 15 text
ߴ֊ؔ
Slide 16
Slide 16 text
ߴ֊ؔͱʁ 16 ؔΛҾʹͱͬͨΓɺؔΛΓͱͨ͠Γ͢Δؔ
Slide 17
Slide 17 text
ϝϦοτ 17 ϓϩάϥϜͷॊೈੑ ίʔυͷ؆ܿੑ ࠶ར༻ੑ ϝϯςφϯεੑ
Slide 18
Slide 18 text
Goʹ͓͚Δߴ֊ؔ 18
Slide 19
Slide 19 text
ίʔϧόοΫ 19
Slide 20
Slide 20 text
ΧϦʔԽ
Slide 21
Slide 21 text
ΧϦʔԽͱʁ 21 f(a, b, c)Λf(a)(b)(c)ͷΑ͏ʹҾΛ1ͭʹ͢Δ
Slide 22
Slide 22 text
22 ΧϦʔԽ͍ͯ͠ͳ͍ྫ ΧϦʔԽΛͬͯϦϑΝΫλͨ͠ྫ
Slide 23
Slide 23 text
෦ద༻
Slide 24
Slide 24 text
෦ద༻ 24 ΧϦʔԽ͞ΕͨؔɺҙͷҾΛݻఆͨ͠ผͷؔΛ࡞͢Δ͜ͱ͕Ͱ͖Δ
Slide 25
Slide 25 text
25 ෦ద༻
Slide 26
Slide 26 text
CPS
Slide 27
Slide 27 text
CPSͱʁ 27 continuation-passing styleɺܧଓ͠ελΠϧͱ͍͏ɻ ؔͷୈ1Ҿʹɺޙʹଓ͘ॲཧ(ܭଌ)Λ͢ϓϩάϥϛϯάελΠϧ CJavaͰɺޙʹଓ͘ॲཧΛηϛίϩϯͱվߦͷޙʹଓࣜ͘
Slide 28
Slide 28 text
CPSͷ݅ 28 •First-class Function(ୈҰڃؔ) •Tail-Call optimization(ඌݺͼग़͠࠷దԽ) GoͷίϯύΠϥରԠ͍ͯ͠ͳ͍
Slide 29
Slide 29 text
CPSͷ݅ 29
Slide 30
Slide 30 text
CPSͷྫ 30
Slide 31
Slide 31 text
CPSͷ݅ 31
Slide 32
Slide 32 text
࠶ؼͱඌ࠶ؼ 32
Slide 33
Slide 33 text
࠶ؼͱඌ࠶ؼ 33
Slide 34
Slide 34 text
࠶ؼͱඌ࠶ؼ 34
Slide 35
Slide 35 text
δΣωϨʔλ
Slide 36
Slide 36 text
δΣωϨʔλͱ 36 ΠςϨʔλΛ؆୯ʹ࡞Δ͜ͱ͕Ͱ͖ΔɻΠςϨʔλΛฦؔ͢ δΣωϨʔλؔͱ͍͏ɻ ·ͨɺδΣωϨʔλؔͰ࡞͞ΕͨΠςϨʔλͷ͜ͱΛ δΣωϨʔλͱݺͿ͜ͱ͋Δɻ
Slide 37
Slide 37 text
GoͷΠςϨʔλ
Slide 38
Slide 38 text
range over int
Slide 39
Slide 39 text
range over int 39 Go1.22Ҏ߱ Go1.22Ҏલ
Slide 40
Slide 40 text
range over func
Slide 41
Slide 41 text
range over func 41 GoͷΠςϨʔλͷσϑΝΫτελϯμʔυ
Slide 42
Slide 42 text
v1.23ҎલͷΠςϨʔλ 42 database/sql.Rows archive/tar.Reader.Next
Slide 43
Slide 43 text
range over func 43 ҎԼͷγάωνϟͷؔΛrangeͰ෮ॲཧͰ͖Δɻ
Slide 44
Slide 44 text
range over func 44
Slide 45
Slide 45 text
range over func 45 rangeͰyieldΛ༻͢Δ͜ͱ͕Ͱ͖ΔΑ͏ʹͳͬͨɻ
Slide 46
Slide 46 text
GoͷΠςϨʔλ 46 • PushελΠϧ : 1ͭͣͭσʔλΛ͢ • PullελΠϧ : 1ͭͣͭσʔλΛड͚औΔ
Slide 47
Slide 47 text
func(func()bool) 47
Slide 48
Slide 48 text
48 func(func(V)bool)
Slide 49
Slide 49 text
func(func(K, V)bool) 49
Slide 50
Slide 50 text
iterύοέʔδ
Slide 51
Slide 51 text
iterύοέʔδ 51 https://github.com/golang/go/tree/go1.23.0/src/iter 1.23ͰՃ͞Εͨ