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

あるインスタンスの取る値が
何パターンあるか数えてみるンゴ!

 あるインスタンスの取る値が
何パターンあるか数えてみるンゴ!

0e742980c48d689f31700b208f22fd5a?s=128

Kohei Kawaguchi

May 28, 2021
Tweet

Transcript

  1. !L@LPIFZJʢ5XJUUFSʣ ͋ΔΠϯελϯεͷऔΔ஋͕ Կύλʔϯ͋Δ͔਺͑ͯΈΔϯΰʂ

  2. ͸͡Ίʹ ͜ΜͳπΠʔτ͕ಥ೗λΠϜϥΠϯʹݱΕΔ IUUQTUXJUUFSDPNJOBNJZTUBUVT T

  3. ͸͡Ίʹ ఴ෇͞Ε͍ͯΔιʔείʔυͷཁ໿ IUUQTUXJUUFSDPNJOBNJZTUBUVT T w 4IBSFE̍ w TIBSFEΠϯελϯε͕ৗʹଘࡏ w TIBSFEΠϯελϯεͷґଘؔ܎͸Π

    ϯελϯεͷੜ੒ޙʹղܾ w 4IBSFE̎ w TIBSFEΠϯελϯε͸ϥϯλΠϜ࣌ ʹੜ੒͞ΕΔ w ґଘؔ܎͸ΠχγϟϥΠβͰղܾ
  4. ͸͡Ίʹ ࣗ෼ͷݟղ w γϯάϧτϯʹରͯ͠TIBSFEͱ͍͏໊લΛ෇͚Δ͜ͱ͸Ұൠత w (PPHMFͷTUZMFHVJEʹ΋هड़͞Ε͍ͯΔ<> w γϯάϧτϯ͸ʮ͋ΔΫϥεͷΠϯελϯε͕͔̍ͭ͠ͳ͍ʯ͜ͱΛ୲อ͢ Δઃܭ<> w

    4IBSFE̎͸ΠϯελϯεΛෳ਺ݸ࡞ΕΔͨΊɺఆٛʹ൓͢Δ <>IUUQTHPPHMFHJUIVCJPTXJGU <>IUUQTKBXJLJQFEJBPSHXJLJ4JOHMFUPO@&&#'&#$&#
  5. ͸͡Ίʹ ࣗ෼ͷݟղ w ݁࿦ w 4IBSFE͕̍είͰ͢ʂ w ཧ༝ w 4IBSFE̎ͷར༻ऀ͸ͦΕ͕γϯάϧτϯͰ͋Δ͜ͱΛظ଴͢Δ

    w ͔͠͠ɺ࣮૷্ͦ͏ͳ͍ͬͯͳ͍ͨΊɺೝࣝͷᴥᴪ͕ੜ͡Δ w ݁Ռతʹϓϩάϥϛϯάϛε͕ى͜Δ͜ͱΛݒ೦
  6. ͸͡Ίʹ ग़୊ऀͷݟղ IUUQT[FOOEFWJOBNJZBSUJDMFTCFE ཁ໿͢Δͱ 4IBSFE̍ͷํ͕ྑ͍ ˞ൈਮՕॴҎ֎ʹ΋ ཧ༝͕͋Γ·͢

  7. ͸͡Ίʹ ௚࿨ɺ௚ੵܕͬͯͳΜͩʁʁ IUUQT[FOOEFWJOBNJZBSUJDMFTCFE ࿦ڌʹ͋Δ௚࿨ɾ௚ੵܕ ΛվΊͯௐ΂ͯΈΔ͜ͱʹ

  8. ୅਺తσʔλܕ ఆٛ w ܕಉ࢜Λ૊Έ߹Θͤͯ࡞ΒΕΔܕͰ௚ੵܕɺ௚࿨ܕɺ͓Αͼྻڍܕ͕͋Δ<̏> w 4XJGUͰ͸ͦΕͧΕ࣍ͷΑ͏ʹදݱ͞ΕΔ w ௚ੵܕɿTUSVDU DMBTT౳ͷෳ਺ͷϓϩύςΟΛ·ͱΊ͋͛ͨ΋ͷ w

    ௚࿨ܕɿ"TTPDJBUFEWBMVFΛ࣋ͭFOVN w ྻڍܕɿ"TTPDJBUFEWBMVFΛ࣋ͨͳ͍FOVN w ௚࿨ܕͱ௚ੵܕ͕͋Ε͹ͲΜͳσʔλߏ଄Ͱ΋දݱͰ͖Δ<> w ྻڍܕ͸௚࿨ܕʹΑͬͯදݱ͞Ε͍ͯΔͱߟ͑ΒΕΔʢओ؍ʣ <̏>IUUQTRJJUBDPNL[@NPSJUBJUFNTEEBFDD <>IUUQTCMPHQFOHJONVSBUFDIFOUSZ VUN@TPVSDFGFFE
  9. ୅਺తσʔλܕ ͦΕͧΕͷ୅਺తσʔλܕ͕औΓ͏Δ஋ͷ਺ w ௚ੵܕʢTUSVDU DMBTTʣ w ֤ϓϩύςΟͷऔΓ͏Δ஋ͷ਺ͷੵ w ௚࿨ܕʢFOVNʣ w

    ֤έʔεͷऔΓ͏Δ஋ͷ਺ͷ࿨
  10. ୅਺తσʔλܕ ͦΕͧΕͷ୅਺తσʔλܕ͕औΓ͏Δ஋ͷ਺ͷྫ w )VNBO͸ݸͷ஋ΛऔΓ͏Δ w BHF͸छྨͷ਺஋ΛऔΓ͏Δ w JT8PNBO͸USVFͱGBMTFͷछྨͷ஋Λ औΓ͏Δ w

    (FOEFS͸̎ݸͷ஋ΛऔΓ͏Δ w XPNBOͱNBO struct Human { let age: Int8 let isWoman: Bool } enum Gender { case woman case man }
  11. ୅਺తσʔλܕ ͦΕͧΕͷ୅਺తσʔλܕ͕औΓ͏Δ஋ͷ਺ͷྫ w )VNBO͸ݸͷ஋ΛऔΓ͏Δ w NBO͸छྨͷ਺஋ΛऔΓ͏Δ w XPNBO͸छྨͷ਺஋ΛऔΓ͏Δ enum Human

    { case man(age: Int8) case woman(age: Int8) }
  12. ୅਺తσʔλܕΛ࢖ͬͯίʔυΛ෼ੳ Ϟνϕʔγϣϯ w ٙ໰ w Ұମɺ͋ΔܕͷΠϯελϯε͕औΓ͏Δ஋ͷ਺Λௐ΂ͯԿͷಘ͕͋ΔΜ ͩʁʁʂʂ w ճ౴ w

    ίʔυͷෳࡶ౓Λఆྔతʹ෼ੳɾٞ࿦Ͱ͖Δʂʂ
  13. ௚ੵܕΛ࢖༻ͯ͠ίʔυ͕ෳࡶʹͳΔྫ લఏ w "OZ0QFSBUJPO͸̍̒௨Γͷ஋ΛऔΔ w #PPM͸̎௨Γͷ஋ΛऔΔ w #PPM͸̏ݸ͋Δ w 0QUJPOBM)PHF&SSPS͸̎௨Γͷ

    ஋ΛऔΔ w /JMͱ<ͳΜ͔΍͹͍> enum HogeError: Error { case ͳΜ͔΍͹͍ } struct AnyOperation { var isExecuting: Bool var isCancelled: Bool var isFinished: Bool var error: HogeError? }
  14. ௚ੵܕΛ࢖༻ͯ͠ίʔυ͕ෳࡶʹͳΔྫ ໰୊ఏٞ w JT$BODFMMFEGBMTFͷ࣌ʹFSSPS͸OJM w ࣮૷্ɺ্هͷ࢓༷Λҧ൓Ͱ͖Δ w JT&YFDVUJOHUSVF͔ͭ JT'JOJTIFEUSVFͷঢ়ଶ͸ଘࡏ͠ͳ͍ w

    ࣮૷্ɺ্هͷ࢓༷Λҧ൓Ͱ͖Δ enum HogeError: Error { case ͳΜ͔΍͹͍ } struct AnyOperation { var isExecuting: Bool var isCancelled: Bool var isFinished: Bool var error: HogeError? }
  15. ௚ੵܕΛ࢖༻ͯ͠ίʔυ͕ෳࡶʹͳΔྫ ໰୊ఏٞ w JT$BODFMMFEGBMTFͷ࣌ʹFSSPS͸ OJM w JT&YFDVUJOHUSVF͔ͭ JT'JOJTIFEUSVFͷঢ়ଶ͸ଘࡏ͠ͳ ͍ enum

    HogeError: Error { case ͳΜ͔΍͹͍ } struct AnyOperation { var isExecuting: Bool var isCancelled: Bool var isFinished: Bool var error: HogeError? } w ࢓্༷ଘࡏ͠ͳ͍ঢ়ଶΛڐ༰͢Δઃܭ ͷͨΊɺσʔλͷෆ੔߹Λى͜͞ͳ͍ ͨΊʹඞཁҎ্ͷߟྀΛڧ͍ΒΕΔ
  16. ௚ੵܕΛ࢖༻ͯ͠ίʔυ͕ෳࡶʹͳΔྫ ໰୊ఏٞ w JT$BODFMMFEUSVFͷ࣌ʹ͔͠FSSPS ͸ଘࡏ͠ͳ͍ w JT&YFDVUJOHUSVF͔ͭ JT'JOJTIFEUSVFͷঢ়ଶ͸ଘࡏ͠ͳ ͍ enum

    HogeError: Error { case ͳΜ͔΍͹͍ } struct AnyOperation { var isExecuting: Bool var isCancelled: Bool var isFinished: Bool var error: HogeError? } w ࢓্༷ଘࡏ͠ͳ͍ঢ়ଶΛڐ༰͢Δઃܭ ͷͨΊɺσʔλͷෆ੔߹Λى͜͞ͳ͍ ͨΊʹඞཁҎ্ͷߟྀΛڧ͍ΒΕΔ ຊ౰͸࣍ͷ̏ঢ়ଶ͚ͩ؅ཧ͍ͨ͠ʂʂʂ  ࣮ߦத  ਖ਼ৗऴྃ  ҟৗऴྃ  Τϥʔ͕)PHF&SSPSͷԿʹ֘౰͢Δ͔ ͳͷʹࠓ͸̍̒ঢ়ଶ΋͋Δ😭😭😭
  17. ௚ੵܕΛ࢖༻ͯ͠ίʔυ͕ෳࡶʹͳΔྫ ௚࿨ܕʹมߋ w ௚࿨ܕʹमਖ਼͢Δ w ͜ͷ௚࿨ܕ͸̏ͭͷ஋͔͠औΒͳ͍ͷͰɺ࠷ ΋ϛχϚϜͳઃܭʹͳ͍ͬͯΔ͜ͱΛ֬ೝͰ ͖Δ w ௚ੵܕΛ࢖͍ͬͯͨͱ͖͸ύλʔϯͩͬͨͷ

    Ͱɺ̍̏ύλʔϯ΋ෆཁͳߟྀΛڧ͍ΒΕ͍ͯͨ enum OperationState { case executing case succeed case error(with: Error) }
  18. ௚ੵܕΛ࢖༻ͯ͠ίʔυ͕ෳࡶʹͳΔྫ ͦΜͳࣄ͍͍ͪͪߟ͑ͳͯ͘΋ϊϦͰॻ͚Δ͡ΌΜʁͱ͍͏໰ʹରͯ͠ w ࣮ࡍɺҎ্·Ͱઆ໌ͨ͠ࣄΛ͍͍ͪͪܭࢉ͠ͳ͕ΒίʔυΛॻ͘ࣄ͸ଟ͘ͳ͍ w งғؾͰΘ͔Δ΍Μʂ w ͔͠͠ίʔυ͕ෳࡶͩͱײͨ࣌͡ʹɺงғؾͰ͸ͳͦ͘Ε͕ͳͥෳࡶͳͷ͔Λ આ໌Ͱ͖Δͷ͸େࣄ w

    ྫ͑͹ɺίʔυϨϏϡʔͷ࣌ʹɺʮͳΜ͔೉͍͔͠Β௚ͯ͠ʯͱݴΘΕΔΑ Γɺʮߟྀͷඞཁͷແ͍Oݸͷঢ়ଶΛڐ༰͢Δઃܭʹͳ͍ͬͯΔͷͰ௚͠ ͯʯͱఆྔతʹݴΘΕΔํ͕ೲಘײ͋Δ͠ɺݐઃͳٞ࿦ʹܨ͕Δɻ
  19. ͓ΘΓʹ w ୅਺తͳσʔλߏ଄ʹ͍ͭͯઆ໌͠·ͨ͠ w ಉ͡Α͏ͳσʔλߏ଄Λఆٛͯ͠΋ɺ௚࿨ܕͱ௚ੵܕͰऔΓ͏Δ஋ͷ਺͕େ ͖͘ҟΔྫΛ঺հ͠·ͨ͠ w ιʔείʔυͷෳࡶੑʹؔ͢Δٞ࿦Λఆྔత͔ͭݐઃతʹߦ͏ํ๏Λࣔࠦ͠ ·ͨ͠ !L@LPIFZJʢ5XJUUFSʣ

  20. Ҿ༻จݙ <>IUUQTHPPHMFHJUIVCJPTXJGU <>IUUQTKBXJLJQFEJBPSHXJLJ4JOHMFUPO@&&#'&#$&# <̏>IUUQTRJJUBDPNL[@NPSJUBJUFNTEEBFDD <>IUUQTCMPHQFOHJONVSBUFDIFOUSZ VUN@TPVSDFGFFE IUUQTUXJUUFSDPNJOBNJZTUBUVT T IUUQT[FOOEFWJOBNJZBSUJDMFTCFE