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

Scala初心者は◯◯◯にメソッドを生やしたいと考えた / Pre-ScalaMatsuri 2020

Scala初心者は◯◯◯にメソッドを生やしたいと考えた / Pre-ScalaMatsuri 2020

2020年8月30日に開催されたプレScalaMatsuriのLightning Talk発表資料です
https://scalaconfjp.doorkeeper.jp/events/110045

集約のエンティティ

August 30, 2020
Tweet

More Decks by 集約のエンティティ

Other Decks in Programming

Transcript

  1. !QJDUJOZ
    1SF4DBMB.BUTVSJ
    -JHIUOJOH5BML
    4DBMBॳ৺ऀ͸
    ̋̋̋ʹϝιουΛ
    ੜ΍͍ͨ͠ͱߟ͑ͨ

    View Slide

  2. 2020.08.30 2

    View Slide

  3. 2020.08.30 3
    4DBMBະܦݧͰΞϧϓೖࣾ
    4DBMBྺ೥൒

    View Slide

  4. 2020.02.16 4
    ςΫϊϩδʔͷྗͰ
    اۀͷܦࡁ׆ಈΛ
    ࢧ͑ɺ֦ு͢Δɻ

    View Slide

  5. 2020.08.30 5
    ʮαϒεΫϦϓγϣϯϏδωεʹ͓͚Δ

    શۀ຿ΛҰؾ௨؏ͰΧόʔʯ

    ɾϓϥΠγϯά
    ɾ঎඼؅ཧ

    ɾސ٬؅ཧ
    ɾܖ໿؅ཧ
    ɾ੥ٻॻͷൃߦˍૹ෇

    ɾΫϨδοτΧʔυܾࡁ

    ɾ֤छσʔλ෼ੳ
    ɾ࢓༁ొ࿥ɺձܭॲཧ
    ɹɹɹɹɹɹɹɹɹɹɹetc…

    View Slide

  6. 2020.02.16 6

    View Slide

  7. 2020.02.16 7
    %%%

    View Slide

  8. 2020.08.30 8
    Ξϧϓɺ%%%΍ͬͯΔΑ
    ɹɹɹɹɹɹɹ͸αϒεΫϦϓγϣϯྖҬͷ4BB4
    ෳࡶ౓͕ඇৗʹߴ͍
    ͦͷͨΊ։ൃॳظ͔Β%%%Λಋೖ
    શࣾతʹ࣮ફ

    View Slide

  9. 2020.08.30 9
    ͔͜͜Βࠓ೔ͷ࿩
    ˞ίʔυ͸αϯϓϧͰ͢

    View Slide

  10. 2020.08.30 10
    ར༻υϝΠϯ
    ੥ٻର৅ͱͳΔར༻ظؒΛந৅Խͨ͠ར༻ΦϒδΣΫτ 70

    case class Availability (
    startAt: LocalDate, // ར༻։࢝೔
    endAt: LocalDate, // ར༻ऴྃ೔
    billingDate: LocalDate, // ੥ٻ೔
    status: AvailabilityStatus // εςʔλε
    )

    View Slide

  11. 2020.08.30 11
    ྫɿܞଳճઢͷܖ໿
    ֤݄ͷར༻͝ͱʹ੥ٻ͕ൃੜ͢Δ
    ೥ܖ໿ͰճઢΛܖ໿͢Δͱɺϲ݄෼ͷར༻͕ੜ͡Δ
    ੥ٻ͕׬ྃ͢Δͱର৅ͷར༻ͷঢ়ଶ͕
    ʮະ੥ٻʯ͔Βʮ੥ٻࡁΈʯʹͳΔ

    View Slide

  12. 2020.08.30 12
    ܖ໿υϝΠϯ
    ܖ໿Λந৅Խͨ͠ܖ໿ΦϒδΣΫτ &OUJUZ
    ͸ར༻Λෳ਺࣋ͭ
    case class Contract (
    id: ContractId, // ܖ໿ID
    availabilities: Seq[Availability], // ར༻
    status: ContractStatus, // εςʔλε
    pricing: …, // ྉۚઃఆ

    )

    View Slide

  13. 2020.08.30 13
    ະ੥ٻͷར༻ΛٻΊΔ
    ར༻ͷεςʔλε͕ະ੥ٻͳ΋ͷ
    case class Contract (…) {
    def unBilledAvailabilities: Seq[Availability] =
    availabilities.filter(_.status = UnBilled) // ະ੥ٻͷར༻
    }

    View Slide

  14. 2020.08.30 14
    ࣍ճͷ੥ٻ೔ΛٻΊΔ
    ະ੥ٻͷར༻ͷ͏ͪɺ੥ٻ೔͕࠷΋͍ۙ΋ͷ
    case class Contract (…) {
    def nextBillingDate(today: LocalDate): LocalDate =
    availabilities.filter(_.status = UnBilled) // ະ੥ٻͷར༻Ͱ
    .filter(_.billingDate > today) // ੥ٻ೔͕ࠓ೔ΑΓະདྷͰ
    .minBy(_.billingDate.toEpochDay) // ੥ٻ೔͕࠷΋աڈͷ΋ͷ
    .billingDate
    }

    View Slide

  15. 2020.08.30 15
    ࠓ೔੥ٻ͢Δར༻ΛٻΊΔ
    ະ੥ٻͷར༻ͷ͏ͪɺ੥ٻ೔͕ࠓ೔ͷ΋ͷ
    case class Contract (…) {
    def shouldCreateBilling(today: LocalDate): Seq[Availability] =
    availabilities.filter(_.status = UnBilled) // ະ੥ٻͷར༻Ͱ
    .filter(_.billingDate = today) // ੥ٻ೔͕ࠓ೔
    }

    View Slide

  16. 2020.08.30 16

    View Slide

  17. 2020.08.30 17
    ͍ͭ΋ಉ͡pMUFSͯ͠ͳ͍ʁ

    View Slide

  18. 2020.08.30 18
    ϦϑΝΫλ
    ར༻͕ܖ໿಺ʹ͔͠ొ৔͠ͳ͍ͳΒ͜ΕͰ΋͍͍͕ʜ
    case class Contract (…) {
    def unBilledAvailabilities: Seq[Availability] =
    availabilities.filter(_.status = UnBilled) // ະ੥ٻͷར༻
    def nextBillingDate(today: Localdate): LocalDate =
    unBilledAvailabilities
    .filter(_.billingDate > today) // ੥ٻ೔͕ࠓ೔ΑΓະདྷͰ
    .minBy(_.billingDate.toEpochDay) // ੥ٻ೔͕࠷΋աڈͷ΋ͷ
    .billingDate
    }

    View Slide

  19. 2020.08.30 19
    ੥ٻΛൃੜͤ͞Δ
    ੥ٻίϯςΩετͰར༻Λ৮ͬͨΓ΋͢Δ
    case class Billing(…)
    object Billing {
    def createBilling(today: LocalDate, contract: Contract): Billing = {
    val availabilities = contract.shouldCreateBilling(today)
    val billing = ??? // TODO: ར༻Λ࢖ͬͯ੥ٻΛ࡞Δ
    }
    }

    View Slide

  20. 2020.08.30 20
    ͱ͍͏Θ͚Ͱ

    View Slide

  21. 2020.08.30 21
    4FRʹϝιουΛੜ΍͍ͨ͠

    View Slide

  22. !QJDUJOZ
    1SF4DBMB.BUTVSJ
    -JHIUOJOH5BML
    4DBMBॳ৺ऀ͸
    ̋̋̋ʹϝιουΛ
    ੜ΍͍ͨ͠ͱߟ͑ͨ

    View Slide

  23. !QJDUJOZ
    1SF4DBMB.BUTVSJ
    -JHIUOJOH5BML
    4DBMBॳ৺ऀ͸
    ̋̋̋ʹϝιουΛ
    ੜ΍͍ͨ͠ͱߟ͑ͨ
    Seq

    View Slide

  24. 2020.08.30 24
    4FRʹϝιουΛੜ΍͍ͨ͠
    ҙຯΛ࣋ͬͨίϨΫγϣϯૢ࡞ʹ໋໊͍ͨ͠
    ϩδοΫͷݟ௨͠ΛΑ͍ͨ͘͠
    *%&ͷίʔυิ׬Λޮ͔͍ͤͨ

    View Slide

  25. 2020.08.30 25
    ʮͦΕJNQMJDJUͰͰ͖ΔΑʯ

    View Slide

  26. 2020.08.30 26

    View Slide

  27. 2020.08.30 27
    ʮ͓͢͢Ί͠ͳ͍͚Ͳʯ

    View Slide

  28. 2020.08.30 28

    View Slide

  29. 2020.08.30 29
    JNQMJDJU
    4FRʹϝιουΛੜ΍ͤΔ͕ʜ
    ఆٛ͢Δ৔ॴ͕೉͍͠ υϝΠϯ૚ʁ

    Ͳ͜Ͱఆٛ͞Ε͍ͯΔ͔ ݺͼग़͠ଆ͔Β
    Θ͔Γʹ͍͘
    υϝΠϯ஌ࣝ͸JNQMJDJUͨ͘͠ͳ͍

    View Slide

  30. 2020.08.30 30
    ͡Ό͋Ͳ͏͢Δ͔

    View Slide

  31. 2020.08.30 31
    υϝΠϯ஌ࣝ͸ܕʹ͠Α͏

    View Slide

  32. 2020.08.30 32
    ར༻ͷίϨΫγϣϯΦϒδΣΫτԽ
    ܖ໿͸ར༻ίϨΫγϣϯΦϒδΣΫτΛ࣋ͭΑ͏ʹͳΔ
    case class Availabilities(
    values: Seq[Availability]
    )
    case class Contract (
    id: ContractId,
    availabilities: Availabilities, // ίϨΫγϣϯΦϒδΣΫτΛ࣋ͭ
    status: ContractStatus,
    pricing: …,
    )

    View Slide

  33. 2020.08.30 33
    ະ੥ٻͷར༻ΛٻΊΔ
    ར༻ͷυϝΠϯ஌ࣝͱ࣮ͯ͠૷Ͱ͖Δ
    case class Availabilities (…) {
    def unBilledAvailabilities: Availabilities =
    Availabilities(availabilities.filter(_.status = UnBilled))
    }

    View Slide

  34. 2020.08.30 34
    ੥ٻΛൃੜͤ͞Δ
    ར༻ίϨΫγϣϯܕΛૢ࡞͢Ε͹Α͘ͳΔ
    case class Billing(…)
    object Billing {
    def createBilling(today: LocalDate, contract: Contract): Billing = {
    val availabilities: Availabilities =
    contract.shouldCreateBilling(today) // AvailabilitesܕΛฦ͢
    val billing = ??? // TODO: ར༻Λ࢖ͬͯ੥ٻΛ࡞Δ
    }
    }

    View Slide

  35. 2020.08.30 35
    ίϨΫγϣϯΦϒδΣΫτͷར఺
    ϝιου͕ੜ΍ͤΔ
    ܕͷදݱྗ͕࢖͑Δ
    ϦϑΝΫλ͠΍͍͢
    'JOE6TBHFͰӨڹൣғ͕Θ͔Γ΍͍͢

    View Slide

  36. 2020.08.30 36
    5JQT
    ඪ४తͳૢ࡞͸USBJUͰܧঝͰ͖ΔΑ͏ʹ͓ͯ͘͠ͱศར
    trait class CustomCollection[A] {
    val values: Seq[A]
    def isEmpty: Boolean = values.isEmpty
    def nonEmpty: Boolean = values.nonEmpty
    def length: Boolean = values.length

    }

    View Slide

  37. 2020.08.30 37
    ؾ෇͖
    4FRʹϝιουΛੜ΍͍ͨ͠ͱߟ͑ͨ
    Ͱ΋ɺ4FRʹϝιουΛੜ΍͢ͷ͸͋͘·Ͱखஈ
    ͳͥͦ͏ߟ͑ͨͷ͔ɺཧ༝ΛݴޠԽ͢Δ͜ͱ͕େࣄ
    ผͷखஈͰ໨త͕ୡ੒Ͱ͖Δ͜ͱ΋͋Δ

    View Slide

  38. 2020.08.30 38
    ·ͱΊ
    4FRʹϝιουΛੜ΍ͨ͘͠ͳͬͨ
    ίϨΫγϣϯ͕υϝΠϯϩδοΫΛ࣋ͭ͜ͱ͕͋Δ
    υϝΠϯϩδοΫΛ࣋ͭ΋ͷ͸ܕͱͯ͠ఆٛ͢Δͷ͕Α͍
    *%&ͷԸܙ͸େ͖͍

    View Slide

  39. 2020.08.30 39
    4DBMBΤϯδχΞ࠾༻த

    View Slide

  40. 2020.08.30 40

    View Slide