Slide 1

Slide 1 text

͸ͯͳʹ͓͚Δ ϚΠΫϩαʔϏεͱScala id:aereal

Slide 2

Slide 2 text

ࣗݾ঺հ • id:aereal • גࣜձࣾ͸ͯͳ (2012ʙ) • ͸ͯͳϒοΫϚʔΫ • ͸ͯͳϒϩά • Perl, Ruby, Scala, Shell Script

Slide 3

Slide 3 text

ΞδΣϯμ • ৽ (ϚΠΫϩ) αʔϏεʹ͍ͭͯ • ͳͥϚΠΫϩαʔϏε͔ • ͳͥScala͔ • DDDͷ࣮ફ

Slide 4

Slide 4 text

DISCLAIMER • ։ൃதͷ಺༰ؚ͕·Ε·͢ • ࣮ࡍͷϦϦʔε࣌ʹ͸มߋ͞Ε͍ͯΔՄೳੑ͋Γ

Slide 5

Slide 5 text

৽αʔϏεʹ͍ͭͯ

Slide 6

Slide 6 text

http://hatenablog.com/

Slide 7

Slide 7 text

“͸ͯͳϒϩάΛ΋ͬͱຊ֨తʹ࢖͍͍ͨํͷͨ Ίʹɺ༗ྉϓϥϯʮ͸ͯͳϒϩάProʯΛ༻ҙ͠ ͓ͯΓ·͢ɻ޿ࠂͷඇදࣔ΍ಠࣗυϝΠϯػೳ ͳͲɺ͸ͯͳϒϩάΛΑΓศརʹ͓࢖͍͍ͨͩ ͚·͢ɻ” http://hatenablog.com/guide/pro

Slide 8

Slide 8 text

͸ͯͳϙΠϯτ ΫϨδοτΧʔυ ίϯϏχৼࠐ ۜߦৼࠐ ࢖༻ https://www.hatena.ne.jp/shop/point/list

Slide 9

Slide 9 text

http://hatena.g.hatena.ne.jp/hatena/20150529/1432869070

Slide 10

Slide 10 text

վम or ࡞Γ௚͠ • طଘͷγεςϜ͸ϞϊϦγοΫ • ܾࡁͯ͠͸ͯͳϙΠϯτΛνϟʔδ͢Δ • ͸ͯͳϙΠϯτΛ࢖ͬͯߪೖ͢Δ • ࢓༷Λ੔ཧ͢Δ͍͍ػձͰ΋͋Δ

Slide 11

Slide 11 text

ܾࡁ୅ߦαʔϏε
 (֎෦) ͸ͯͳͷαʔϏε (ϒοΫϚʔΫͳͲ) ݱߦܾࡁγεςϜ ϙΠϯτ؅ཧ ڞ༗Ϟδϡʔϧ ༗ྉϓϥϯ؅ཧ ґଘ ͸ͯͳͷαʔϏε ґଘ ͸ͯͳͷαʔϏε ґଘ etc. ΞΧ΢ϯτ

Slide 12

Slide 12 text

ݱߦܾࡁγεςϜͷߏ੒ • ܾࡁγεςϜ: WebΞϓϦέʔγϣϯ • API͸ͳ͘HTMLͷϑΥʔϜ͕͋ΔͷΈ • ڞ༗Ϟδϡʔϧ: ܾࡁ୅ߦαʔϏεͱ௨৴͢Δ࣮૷ • ͨ͘͞ΜͷϦϙδτϦ͔Βґଘ͞Ε͍ͯΔ • ڞ༗DBΛૢ࡞͢Δ

Slide 13

Slide 13 text

ݱߦܾࡁγεςϜͷվम • ΄ͱΜͲݱ࣮తͰ͸ͳ͍ • ޓ׵ੑʹؔ͢ΔεΩʔϜ͕ͳ͍ • ׬શޙํޓ׵ or die • ෳ਺ͷυϝΠϯ͕ೖΓཚΕ͍ͯΔ • ݱࡏͷ࢓༷ʹӨڹΛڧ͘ड͚Δ • վળ͕·ΘΒͳ͍

Slide 14

Slide 14 text

ϑϧεΫϥον

Slide 15

Slide 15 text

৽ܾࡁγεςϜ • ܾࡁ୅ߦαʔϏεͷήʔτ΢ΣΠͱͯ͠ಇ͘ • ͸ͯͳϒϩάProͷ෇༩ͳͲ͸֤αʔϏεʹҠৡ • JSON over HTTPͳAPIͷΈΛఏڙ͢Δ • ର৅͸SSKDs (= small set of known developers)

Slide 16

Slide 16 text

৽ܾࡁγεςϜ
 ߏ੒ਤ ܾࡁ୅ߦαʔϏε
 (֎෦) ͸ͯͳͷαʔϏε ༗ྉϓϥϯͷ ঢ়ଶ؅ཧ ৽ܾࡁγεςϜ

Slide 17

Slide 17 text

چ ৽

Slide 18

Slide 18 text

׬

Slide 19

Slide 19 text

ͳͥϚΠΫϩαʔϏε͔

Slide 20

Slide 20 text

ϚΠΫϩαʔϏεͰ࡞Δ͔ • 1ʙ3ষʹ͔͚ͯϝϦοτ΍ ʮ༏ΕͨαʔϏεʯʹ͍ͭͯ ड़΂ΒΕ͍ͯΔ • ϚΠΫϩαʔϏεʹΑͬͯ
 αʔϏε͸ΑΓΑ͘ͳΔͷ͔ • ΨόφϯεͳͲɺ
 ίετ͸෷͑ΔͩΖ͏͔ https://www.oreilly.co.jp/books/9784873117607/

Slide 21

Slide 21 text

ૄ݁߹ͱߴڽूੑ 1. ϞϊϦγοΫͰखΛೖΕͮΒ͍ (ີ݁߹) 2. ৽ͨʹ༗ྉϓϥϯΛಋೖ͍ͨ͠αʔϏε͸࠶࣮૷ 3. υϝΠϯ஌͕ࣝྲྀग़ (௿ڽूੑ) 4.

Slide 22

Slide 22 text

࣋ଓՄೳʹ࡞Γ (ͳ͓͠) ͍ͨ • είʔϓΛখ͘͞໌ྎʹอͭ • ૄ݁߹ (ʰϚΠΫϩαʔϏεΞʔΩςΫνϟʱ3.2.1) • ߴڽूੑ (ʰϚΠΫϩαʔϏεΞʔΩςΫνϟʱ3.2.2) • ෼ׂ౷࣏͢Δ • ٕज़ҟ࣭ੑ (͍ͭ·Ͱ΋Perl͸ݫ͍͠) • σϓϩΠͷϥΠϑαΠΫϧΛૣΊΔ

Slide 23

Slide 23 text

νʔϜؒ࿈ܞͷ
 Ή͔ͣ͠͞ͱઓ͏

Slide 24

Slide 24 text

ΠϯηϓγϣϯσοΩΛ࡞Δ • ۙ͝ॴ͞ΜΛ໌Β͔ʹ͢Δ • ϝϯς͸Ͳ͜ʹҾ͖ܧ͙ͷ͔ • ԿΛ༏ઌ͢Δͷ͔ (ఘΊΔͷ͔) Λ͸͖ͬΓͤ͞Δ • είʔϓɺ඼࣭ɺ༧ࢉɺεέδϡʔϧ • ʰΞδϟΠϧαϜϥΠʱ

Slide 25

Slide 25 text

ͳͥϚΠΫϩαʔϏε͔: ·ͱΊ • ࠓճ͸ϚΠΫϩαʔϏεͰ΍͍ͬͯ͘ • ಘΒΕΔϝϦοτ > ෷͏ίετ • ΨόφϯεͳͲෆ҆ͳ఺ʹ΋खΛଧͭ • ΠϯηϓγϣϯσοΩ • ૣ͔͘Βר͖ࠐΉ

Slide 26

Slide 26 text

׬

Slide 27

Slide 27 text

ͳͥScala͔

Slide 28

Slide 28 text

࣮૷ݴޠΛܾΊΔ • ͸ͯͳͷબ୒ࢶͱͯ͠͸Perl, Go, ͦͯ͠Scala • ৽ܾࡁγεςϜͷίΞυϝΠϯʹٕज़ͦͷ΋ͷ͸
 ؚ·Εͳ͍ • ϦεΫΛͱͬͯଞͷ৽ͨͳݴޠΛ࠾༻͢Δ
 ϝϦοτ͸ബ͍

Slide 29

Slide 29 text

࣮૷ݴޠΛܾΊΔ • ͳʹ͔ࢦඪ͕΄͍͠ • ؾ࣋ͪ͸͍Ζ͍Ζ͋Δ • ʮ޷͖͔ͩΒʯͰ΋͍͍͚Ͳ…… • ͲΜͳPros/ConsΛݟͨͷ͔

Slide 30

Slide 30 text

ඇػೳཁٻΛఆΊΔ • ISO 9126 • functionality: ػೳੑ • reliability: ৴པੑ • usability: ࢖༻ੑ • efficiency: ޮ཰ੑ • maintainability: อकੑ • portability: Ҡ২ੑ

Slide 31

Slide 31 text

ඇػೳཁٻΛఆΊΔ • ISO 9126 • functionality: ػೳੑ • reliability: ৴པੑ • usability: ࢖༻ੑ • efficiency: ޮ཰ੑ • maintainability: อकੑ • portability: Ҡ২ੑ

Slide 32

Slide 32 text

ݴޠΛબͿ্ͰٻΊΔ΋ͷ • ๛͔ͳදݱྗ • ෳࡶͳυϝΠϯΛաෆ଍ͳ͘දݱ͍ͨ͠ • ֶशۂઢͷ؇΍͔͞ • ։ൃεϐʔυΛૣΊʹߴΊ҆ఆ͍ͤͨ͞ • কདྷɺ։ൃ͢Δਓʹ΋΍͘͞͠

Slide 33

Slide 33 text

ෳࡶͳυϝΠϯ • ঢ়ଶ΍ߟྀ͢΂͖ม਺͕ೖΓ૊ΜͰ͍Δ • ΫϨδοτΧʔυͷ༗ޮظݶ͕੾Ε͍ͯͨΒ? • ܾࡁ୅ߦαʔϏε͕མ͍ͪͯͨΒ?

Slide 34

Slide 34 text

ঢ়ଶΛྻڍܕͰදݱ͢Δ sealed trait State object State { case object Requested extends State case object Paid extends State case object Failed extends State } ※࣮ࡍ͸΋͏ͪΐͬͱ͍Ζ͍Ζ͋Γ·͢

Slide 35

Slide 35 text

ঢ়ଶΛྻڍܕͰදݱ͢Δ case class Payment(state: State) { def charged: Boolean = state match { case State.Paid => true case _ => false } }

Slide 36

Slide 36 text

ঢ়ଶΛྻڍܕͰදݱ͢Δ • sealedम০͢ΔͱmatchࣜͰ໢ཏੑνΣοΫͰ͖Δ • ͱΓ͏Δঢ়ଶʹ໊͍ͭͯલΛ༩͑ΒΕΔ • ʮPaid͔FailedʹͳͬͨΒͦͷPayment͸
 ऴ୺͠·͢Ͷʯ

Slide 37

Slide 37 text

Scalaͷֶशۂઢ……? • ؇΍͔Ͱ͸ͳ͍! (ݸਓͷݟղ) • ͔͠͠͸ͯͳͰ͸ࢧԉ͢Δ؀ڥΛ੔͖͑ͯͨ • ࣄྫ (Mackerel, ͸ͯͳϒοΫϚʔΫϦχϡʔΞϧ) • ͸ͯͳڭՊॻ github.com/hatena/Hatena-Textbook • ScalaͷఆੴΛ୳Δձ

Slide 38

Slide 38 text

ScalaͷఆੴΛ୳Δձ • ϥΠϒϥϦ΍࣮૷ύλʔϯͷ஌ݟΛ࣋ͪدΔ • ScalaΛ࢖͍ͬͯΔνʔϜͷ༗ࢤͰ։࠵ • Mackerel • ϒοΫϚʔΫϦχϡʔΞϧ • ৽ܾࡁγεςϜ

Slide 39

Slide 39 text

ఆੴΛ୳ΔձͰ࿩ͨ͜͠ͱ • ϨΠϠߏ੒ • ΞϓϦέʔγϣϯ૚ • υϝΠϯ૚ • Πϯϑϥ૚ • બΜͩWAF/ORMͱͦͷഎܠ

Slide 40

Slide 40 text

ͳͥScala͔: ·ͱΊ • ৽ܾࡁγεςϜ͕ٻΊΔཁ݅Λຬͨͯ͘͠Εͦ͏ • ػೳੑ • อकੑ • ScalaΛॻ͘ΤϯδχΞΛҭͯΔ౔৕Λ੔͖͑ͯͨ • ScalaͷఆੴΛ୳ΔձɺڭՊॻ

Slide 41

Slide 41 text

׬

Slide 42

Slide 42 text

DDDͷ࣮ફ

Slide 43

Slide 43 text

DDD͍ͨ͠ • υϝΠϯΤΩεύʔτͷ஌͕ࣝ
 ͦͷ··མͱ͠ࠐ·ΕͨαʔϏεʹ͍ͨ͠ • ཁૉٕज़ (Πϯϑϥ૚) ͱ෼཭Մೳʹ͍ͨ͠ • ࠷ѱ·ͨ࡞Γ௚͢͜ͱʹͳͬͯ΋
 ΑΓ௧ΈΛগͳ͍ͨ͘͠

Slide 44

Slide 44 text

ݱߦܾࡁγεςϜͷ൓ল • ํ਑͕పఈ͞Εͳ͔ͬͨ • ʮApp͸΋͏ͩΊͩɺServiceͰ΍Γͳ͓ͦ͏ʯ • ಺੡ͷPerl൛Active RecordతORM • ϞσϧɺαʔϏεɺϦϙδτϦ͕ᕒવҰମ • Perlͷݶք • ΠϯλʔϑΣʔε͕ͳ͍ • ΧϓηϧԽ͕ۃΊͯࠔ೉

Slide 45

Slide 45 text

͸ͯͳϒοΫϚʔΫ in Scala

Slide 46

Slide 46 text

[PR]
 PerlͰ΋ΠέͯΔ։ൃ͍ͯ͠·͢ Perlͷ্ʹ΋ࡾ೥
 ʙͣͬͱΠέͯΔαʔϏεΛ࡞Γଓ͚Δٕज़ʙ http://yapcasia.org/2015/talk/show/de9e7a1e-136d-11e5-a9fc- d9f87d574c3a

Slide 47

Slide 47 text

DDD஥ؒ

Slide 48

Slide 48 text

͸ͯͳࣾ಺Ͱ։࠵ͨ͠
 DDDษڧձͷ༷ࢠΛ͝঺հ͠·͢ http://developer.hatenastaff.com/entry/2015/08/20/170300

Slide 49

Slide 49 text

ΤϦοΫɾΤϰΝϯεͷ
 υϝΠϯۦಈઃܭ http://www.shoeisha.co.jp/book/detail/9784798126708

Slide 50

Slide 50 text

࣮ફυϝΠϯۦಈઃܭ http://www.shoeisha.co.jp/book/detail/9784798131610

Slide 51

Slide 51 text

ࣾ಺DDDษڧձ

Slide 52

Slide 52 text

ࣾ಺DDDษڧձ • ਎ۙͳέʔεʹ͍ͭͯٞ࿦ • ৽͘͠࡞ΔͳΒɺ͍·͔Βվળ͢ΔͳΒ • ֎ͷੈքͷ஌ݟΛڞ༗ • ʮScalaͩͱ͜͏͍͏ػೳ͕͋ͬͯ……ʯ • ʮHaskellͩͱ……ʯ • ʮRubyͷ˓˓ͱ͍͏ϥΠϒϥϦ͸……ʯ

Slide 53

Slide 53 text

DDDͷ࣮ફͱ࡞ઓ

Slide 54

Slide 54 text

೰Μͩͱ͜ΖɾݟͲ͜Ζ • ΤϯςΟςΟͷදݱ • ґଘੑͷ஫ೖ (DI) • cake pattern • αϒϓϩδΣΫτԽ • layering violationΛ๷͙

Slide 55

Slide 55 text

ΤϯςΟςΟͷදݱ

Slide 56

Slide 56 text

ΤϯςΟςΟͷදݱ • ΤϯςΟςΟ͸ૉ๿ͳcase class • ORM͸Slick • tarao/slick-jdbc-extensionΛ࢖ͬͯੜSQLͰҾ͍ͯ
 case class΁Ϛοϐϯά

Slide 57

Slide 57 text

ΤϯςΟςΟͷදݱ • ΤϯςΟςΟ = ID + υϝΠϯϞσϧ • ܕ΋ύϥϝʔλԽ͞Ε͍ͯΔ • υϝΠϯϞσϧʹରͯ͠ڞม • ಉ͡IDܕ͕ͩυϝΠϯϞσϧ͕ҟͳΔ
 ΤϯςΟςΟΛఆٛͰ͖Δ

Slide 58

Slide 58 text

ίʔυྫ case class EntityKey[ID]( val repr: String ) extends AnyVal abstract class Entity[K, +V]( val id: EntityKey[K], val value: V )(implicit ev: V <:< K)

Slide 59

Slide 59 text

ίʔυྫ case class EntityKey[ID]( val repr: String ) extends AnyVal abstract class Entity[K, +V]( val id: EntityKey[K], val value: V )(implicit ev: V <:< K) phantom type

Slide 60

Slide 60 text

ίʔυྫ case class EntityKey[ID]( val repr: String ) extends AnyVal abstract class Entity[K, +V]( val id: EntityKey[K], val value: V )(implicit ev: V <:< K) ஋ܕΛڞมʹ

Slide 61

Slide 61 text

ίʔυྫ case class EntityKey[ID]( val repr: String ) extends AnyVal abstract class Entity[K, +V]( val id: EntityKey[K], val value: V )(implicit ev: V <:< K) ஋ͷܕ͕IDͷܕͷαϒΫϥεͰ͋Δ
 ܕύϥϝʔλ੍໿

Slide 62

Slide 62 text

cake pattern

Slide 63

Slide 63 text

ΠϯλʔϑΣʔεΛఆٛ package repo trait AccountComponent { def accountLoader: AccountLoader trait AccountLoader }

Slide 64

Slide 64 text

࣮૷ package infra.db trait AccountComponent extends repo.AccountComponent { def accountLoader: AccountLoader = AccountDB object AccountDB extends AccountLoader { ... } }

Slide 65

Slide 65 text

ґଘͷએݴͱ࢖༻ package app trait AccountApp { self: repo.AccountComponent => accountLoader.find(...) }

Slide 66

Slide 66 text

ґଘͷ஫ೖ package main object Root extends app.AccountApp with infra.db.AccountComponent

Slide 67

Slide 67 text

cake pattern • ίϯύΠϧλΠϜͰDI͞ΕΔ • Playͷίϯτϩʔϥ΋cake componentͱͯ͠߹੒

Slide 68

Slide 68 text

DDDͷ࣮ફ: ·ͱΊ • ࣾ಺ษڧձͰʰ࣮ફDDDʱΛྠಡɺ஌ݟΛڞ༗ • զʑʹͱͬͯͪΐ͏ͲΑ͍DDDΛݟ͚ͭΔ • ScalaͷྗΛ׆͔࣮ͨ͠૷ύλʔϯͷൃݟ

Slide 69

Slide 69 text

׬

Slide 70

Slide 70 text

·ͱΊ • ۜͷ஄ؙ͸ͳ͍! • ཁ݅ʹ͍͋ͬͯΔ͔ߟ͑Δ • ཁ݅ʹ͋ΘͤͯΞϨϯδ͍ͯ͘͠ • ʮͪΐ͏Ͳ͍͍DDDʯ • ʮͪΐ͏Ͳ͍͍ϚΠΫϩαʔϏεʯ • ࣌ʹ͸ࣃΛ৯͍͠͹Δ