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

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

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

F62b70b58d2f54c3d421a8f19c1c8817?s=128

集約のエンティティ

August 30, 2020
Tweet

Transcript

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

  2. 2020.08.30 2

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

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

  5. 2020.08.30 5 ʮαϒεΫϦϓγϣϯϏδωεʹ͓͚Δ
 શۀ຿ΛҰؾ௨؏ͰΧόʔʯ
 ɾϓϥΠγϯά ɾ঎඼؅ཧ
 ɾސ٬؅ཧ ɾܖ໿؅ཧ ɾ੥ٻॻͷൃߦˍૹ෇
 ɾΫϨδοτΧʔυܾࡁ


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

  7. 2020.02.16 7 %%%

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

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

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

    LocalDate, // ར༻։࢝೔ endAt: LocalDate, // ར༻ऴྃ೔ billingDate: LocalDate, // ੥ٻ೔ status: AvailabilityStatus // εςʔλε )
  11. 2020.08.30 11 ྫɿܞଳճઢͷܖ໿ ֤݄ͷར༻͝ͱʹ੥ٻ͕ൃੜ͢Δ ೥ܖ໿ͰճઢΛܖ໿͢Δͱɺϲ݄෼ͷར༻͕ੜ͡Δ ੥ٻ͕׬ྃ͢Δͱର৅ͷར༻ͷঢ়ଶ͕ ʮະ੥ٻʯ͔Βʮ੥ٻࡁΈʯʹͳΔ

  12. 2020.08.30 12 ܖ໿υϝΠϯ ܖ໿Λந৅Խͨ͠ܖ໿ΦϒδΣΫτ &OUJUZ ͸ར༻Λෳ਺࣋ͭ case class Contract (

    id: ContractId, // ܖ໿ID availabilities: Seq[Availability], // ར༻ status: ContractStatus, // εςʔλε pricing: …, // ྉۚઃఆ … )
  13. 2020.08.30 13 ະ੥ٻͷར༻ΛٻΊΔ ར༻ͷεςʔλε͕ະ੥ٻͳ΋ͷ case class Contract (…) { def

    unBilledAvailabilities: Seq[Availability] = availabilities.filter(_.status = UnBilled) // ະ੥ٻͷར༻ }
  14. 2020.08.30 14 ࣍ճͷ੥ٻ೔ΛٻΊΔ ະ੥ٻͷར༻ͷ͏ͪɺ੥ٻ೔͕࠷΋͍ۙ΋ͷ case class Contract (…) { def

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

    shouldCreateBilling(today: LocalDate): Seq[Availability] = availabilities.filter(_.status = UnBilled) // ະ੥ٻͷར༻Ͱ .filter(_.billingDate = today) // ੥ٻ೔͕ࠓ೔ }
  16. 2020.08.30 16

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

  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 }
  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: ར༻Λ࢖ͬͯ੥ٻΛ࡞Δ } }
  20. 2020.08.30 20 ͱ͍͏Θ͚Ͱ

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

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

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

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

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

  26. 2020.08.30 26 ☺

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

  28. 2020.08.30 28

  29. 2020.08.30 29 JNQMJDJU 4FRʹϝιουΛੜ΍ͤΔ͕ʜ ఆٛ͢Δ৔ॴ͕೉͍͠ υϝΠϯ૚ʁ  Ͳ͜Ͱఆٛ͞Ε͍ͯΔ͔ ݺͼग़͠ଆ͔Β Θ͔Γʹ͍͘

    υϝΠϯ஌ࣝ͸JNQMJDJUͨ͘͠ͳ͍
  30. 2020.08.30 30 ͡Ό͋Ͳ͏͢Δ͔

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

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

    case class Contract ( id: ContractId, availabilities: Availabilities, // ίϨΫγϣϯΦϒδΣΫτΛ࣋ͭ status: ContractStatus, pricing: …, )
  33. 2020.08.30 33 ະ੥ٻͷར༻ΛٻΊΔ ར༻ͷυϝΠϯ஌ࣝͱ࣮ͯ͠૷Ͱ͖Δ case class Availabilities (…) { def

    unBilledAvailabilities: Availabilities = Availabilities(availabilities.filter(_.status = UnBilled)) }
  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: ར༻Λ࢖ͬͯ੥ٻΛ࡞Δ } }
  35. 2020.08.30 35 ίϨΫγϣϯΦϒδΣΫτͷར఺ ϝιου͕ੜ΍ͤΔ ܕͷදݱྗ͕࢖͑Δ ϦϑΝΫλ͠΍͍͢ 'JOE6TBHFͰӨڹൣғ͕Θ͔Γ΍͍͢

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

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

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

  40. 2020.08.30 40