プログラマーって どんな人? -- 牛乳と卵で理解するプログラマーという人種

プログラマーって どんな人? -- 牛乳と卵で理解するプログラマーという人種

Around Taisho 『大正GeekNight〜プログラマーたちが大正にやってくる〜』No.11
... のスピーチです

8c1ecc738eee0254a41c2d4b33cda7b6?s=128

Hisateru Tanaka

February 28, 2019
Tweet

Transcript

  1. ϓϩάϥϚʔͬͯ ͲΜͳਓ ڇೕͱཛͰཧղ͢ΔϓϩάϥϚʔͱ͍͏ਓछ

  2. ͨͳ͔ͻͯ͞Δ @tanakahisateru ॴ௕

  3. ฼͕ݴ͍·ͨ͠ ʮϋχʔɺࢢ৔΁ߦͬͯڇೕΛළͰ ͭങ͖ͬͯͯɻ΋͠ཛ͕͋ͬͨΒ ͓ͭئ͍ʯ ࢲ͸ͭͷڇೕළΛ࣋ͬͯؼ͖ͬͯ ·ͨ͠ɻ ൴ঁ͸ݴ͍·ͨ͠ʮͳΜͯ͜ͱɺͳ ΜͰڇೕΛຊ΋ങ͖ͬͯͨͷ ʯ ࢲ͸ݴ͍·ͨ͠ʮͩͬͯཛ͕͋ͬͨ

    ͔Βʯ ϓϩάϥϚʔͱ͸
  4. from shop import Item, Shop shop = Shop('ΠΦϯ υʔϜγςΟళ') cart

    = shop.cart() cart.add(Item.MILK) receipt = shop.paycheck(cart) print(receipt) ࣮ࡍʹ࡞ͬͯΈͨͷͰݕূ͠·͠ΐ͏ ͓ళͷΧʔτʹ঎඼ΛೖΕͯࢧ෷͍Λ͢ΔͱϨγʔτ͕ग़·͢ IUUQTHJTUHJUIVCDPNUBOBLBIJTBUFSV EGFDBBFGDCBGEE
  5. $ python main.py ⏎ ΠΦϯ υʔϜγςΟళ Ͱͷ͓ങ্͍͛: ڇೕύοΫ ... 1఺

  6. ʮͭങ͖ͬͯͯʯ ΉΉɺ͜͜͸ʹͳΔ͔΋͠Εͳ͍ͧʜ ͍΍ɺສશΛظͯ͠Ͱ΋͍͍Α͏൚༻తʹ͓͔ͯ͠Ͷ͹ ϓϩάϥϚʔ೴ʹ͔͔Δͱ

  7. cart.add(Item.MILK) cart.add(Item.MILK) cart.add(Item.MILK) cart.add(Item.MILK) : :

  8. for i in range(100): cart.add(Item.MILK) ϓϩάϥϚʔ͸͔͍͜͠ͷͰɺ܁Γฦ͠Λ࢖͑·͢

  9. ʮڇೕΛʯʮ̍ͭʯ ԿΛɺԿݸɺ͜Ε͸มΘΔ͔΋͠Εͳ͍ͳ ϓϩάϥϚʔ೴ʹ͔͔Δͱ

  10. item = Item.MILK count = 1 for i in range(count):

    cart.add(item) ϓϩάϥϚʔ͸͔͍͜͠ͷͰɺม਺Λ࢖͑·͢
  11. ʮങ͖ͬͯͯʯ ʮങ͏ʯखॱ͸εςοϓ͕ଟ͍͔Β·ͱΊΑ͏ ͦͯ͠ʮങ͏ʯΛҙຯ͢Δ໊લ΋͚ͭͳ͖Ό ϓϩάϥϚʔ೴ʹ͔͔Δͱ

  12. def buy(shop, item, count): cart = shop.cart() for i in

    range(count): cart.add(item) return shop.paycheck(cart) item = Item.MILK count = 1 receipt = buy(shop, item, count) print(receipt) ϓϩάϥϚʔ͸͔͍͜͠ͷͰɺؔ਺Λ࡞Ε·͢
  13. ʮཛ͕͋ͬͨΒʯ ϓϩάϥϚʔ೴ʹ͔͔Δͱ

  14. count = 1 # ௥Ճ࢓༷ ళʹཛ͕͋Δ৔߹͸ߪೖྔΛ 6 ͱ͢Δ if shop.stock_of(Item.EGG)

    > 0: count = 6 ϓϩάϥϚʔ͸ਓʹࢥΘΕ͍ͯΔ΄Ͳ࣮ࡍʹ͸ ͔͘͜͠ͳ͍ͷͰɺ͙͢ʹʮ৚݅จʯΛཚ༻͠·͢ ͪͳΈʹʮ͋ͱͦΕ͔Β͜Ε΋ʯͱ࠷ޙʹݴΘΕͨ͜ͱ͸ಛʹ΍Β͔͠·͢
  15. $ python main.py ⏎ ΠΦϯ υʔϜγςΟళ Ͱͷ͓ങ্͍͛: ڇೕύοΫ ... 6఺

    ʮͩͬͯཛ͕͔͋ͬͨΒʯ ͔ͯ͘͠ʜ
  16. from shop import Item, Shop def buy(shop, item, count): cart

    = shop.cart() for i in range(count): cart.add(item) return shop.paycheck(cart) shop = Shop('ΠΦϯ υʔϜγςΟళ') item = Item.MILK count = 1 # ௥Ճ࢓༷ ళʹཛ͕͋Δ৔߹͸ߪೖྔΛ 6 ͱ͢Δ if shop.stock_of(Item.EGG) > 0: count = 6 receipt = buy(shop, item, count) print(receipt) ಄ͷମૢ Ͳ͕͓͔͍͜͠Ͱ͠ΐ͏͔ ώϯτ͔͜͜ΒԼ
  17. ϓϩάϥϜࢥͬͨͱ͓Γʹ͸ಈ͔ͳ͍ɻॻ͍ͨͱ͓Γʹಈ͘ɻ ϓϩάϥϚʔݴΘΕͨ͜ͱ͚ͩΛ΍ΕͱݴΘΕΔͱɺ ༩͑ΒΕͨϩδοΫؒҧ͍Λͦͷ··࠶ݱͯ͠͠·͏ɻ

  18. ·ɺݴ͏ͯ΋ۃ୺ͳδϣʔΫͰ͔͢Β

  19. Ͱ͸ɺ͔͜͜Βઌ͸ ຊ෺ͷϓϩάϥϚʔͷ࿩Ͱ͢ ϓϩάϥϚʔͷΈͳ͞Μɺ͋ͳͨ͸ຊ౰ʹϓϩάϥϚʔͰ͠ΐ͏͔

  20. class Shopping: def __init__(self, shop): self.shop = shop self.cart =

    shop.cart() def take(self, item, count): for i in range(count): self.cart.add(item) def paycheck(self): return self.shop.paycheck(self.cart) ΄Μͱ͏ʹ͔͍͜͠ϓϩάϥϚʔ͸ ࢥߟΛิ͏ͨΊʹʮผͷ֓೦ʯΛͻͶΓग़͠·͢ ࣮ࡏ͠ͳ͍ʮങ͍෺͢Δͱ͍͏͜ͱʯΛදݱͯ͠ΈΑ͏ ങ͍෺ͱ͸ ঎඼ΛखʹऔΔ͜ͱ ࢧ෷͏͜ͱ
  21. shop = Shop('ΠΦϯ υʔϜγςΟళ') shopping = Shopping(shop) shopping.take(Item.MILK, 1) if

    shop.stock_of(Item.EGG) > 0: shopping.take(Item.MILK, 6) # FIXME ਖ਼͍͠঎඼͕Կ͔֬ೝͯ͠मਖ਼͢Δ͜ͱ receipt = shopping.paycheck() print(receipt) ͦͷ֓೦Λ࢖ͬͯʮจষͷΑ͏ʹॻ͘ʯͱɺ ύζϧͷ··Ͱ͸ݟ͑ͳ͔ͬͨʮؾ͖ͮʯ͕͋Γ·͢ ͋Ε ͜͜঎඼ͷࢦఆ͕ཁΔͧ
  22. shopping = Shopping(shop) shopping.take(Item.MILK, 1) if shop.stock_of(Item.EGG) > 0: shopping.take(Item.EGG,

    6) ʮಡΊΔʯίʔυ͸͙͢ʹͳ͓ͤ·͢ ཛ͕͋ͬͨͱ͖ങͬͯཉ͍͠ͷ͸ɺڇೕ͡Όͳͯ͘ී௨ཛͰ͠ΐ
  23. $ python main.py ⏎ ΠΦϯ υʔϜγςΟళ Ͱͷ͓ങ্͍͛: ڇೕύοΫ ... 1఺

    ཛ ... 6఺
  24. ͓ͬͱɺ ͜ΕͰऴΘΓ͔ͳͱࢥͬͨ  ͦΜͳΘ͚ͳ͍͡Όͳ͍Ͱ͔͢ɺ ·ͩϠό͍࢓༷࿙Ε͕͋Γ·͢Α

  25. $ python main.py ⏎ ΠΦϯ υʔϜγςΟళ Ͱͷ͓ങ্͍͛: ڇೕύοΫ ... 1఺

    ཛ ... 6఺ shop = Shop('ΠΦϯ υʔϜγςΟళ') shop.stocks(Item.EGG, 100) ;ͭ͏ϓϩάϥϚʔ͸ɺཛ͕े෼ʹ͋Δ৔߹Λςετ͠·͢ ళʹԿ఺ࡏݿ͕͋Δ͔એݴ
  26. $ python main.py ⏎ ΠΦϯ υʔϜγςΟళ Ͱͷ͓ങ্͍͛: ڇೕύοΫ ... 1఺

    shop = Shop('ΠΦϯ υʔϜγςΟళ') shop.stocks(Item.EGG, 0) ͔͍͜͠ϓϩάϥϚʔ͸ɺཛ͕ͳ͍৔߹΋ςετ͠·͢ ళͷࡏݿΛʹͯ͠ΈΔ
  27. shop = Shop('ΠΦϯ υʔϜγςΟళ') shop.stocks(Item.EGG, 5) if shop.stock_of(Item.EGG) > 0:

    shopping.take(Item.EGG, 6) ͋Ε ཛͷࡏݿ͕ΑΓগͳ͍ͱʜ ΋ͬͱ͔͍͜͠ϓϩάϥϚʔ͸ɺςετ͔ΒϩδοΫΛൃݟ͠·͢ ͭ͋Δ΋ͷΛ ͭͰ΋͋Ε͹ ͭങ͏
  28. shop = Shop('ΠΦϯ υʔϜγςΟళ') shop.stocks(Item.MILK, 0) shopping.take(Item.MILK, 1) ͦ΋ͦ΋ɺࡏݿ͋Δ͔Θ͔Βͳ͍ͷʹɺ ແ৚݅ʹڇೕΛങ͓͏ͱͯ͠Δ͚Ͳʜ

  29. $ python main.py Traceback (most recent call last): File "main.py",

    line 43, in <module> shopping.take(Item.EGG, 6) File "main.py", line 30, in take self.cart.add(item) File “/.../shop.py”, line 19, in add raise Exception("%sͷࡏݿ͕͋Γ·ͤΜ" % item.value) Exception: ཛͷࡏݿ͕͋Γ·ͤΜ ίʔυ͸ؒҧͬͯͳͯ͘΋ɺ͓͔͠ͳϩδοΫͩͱɺ ࣮ߦͨ͠ͱ͖Τϥʔ͕ग़·͢ ʮ͏ΘɺͳΜ͔Ϡόͦ͏ͳͷग़Δ΍Μʯ
  30. # FIXME ࢓༷όά: 1 ͭͰ΋ߪೖ͢Δʹ͸ࡏݿ͕ඞཁ shopping.take(Item.MILK, 1) # FIXME ࡏݿ͕

    1 ʙ 5 ͷͱ͖ΤϥʔͰམͪΔ if shop.stock_of(Item.EGG) > 0: shopping.take(Item.EGG, 6) ࡏݿ͕଍Γͳ͍ͱ͖ߪೖͨ͠Βࢮ͵΍͹͍ ແ৚݅ʹͭങ͖ͬͯͯͱ͔ɺͳΜͯةݥͳ࢓༷ͳΜͩ
  31. # ߪೖ͢Δʹ͸ࡏݿ͕ඞཁ [hotfix] if shop.stock_of(Item.MILK) >= 1: shopping.take(Item.MILK, 1) #

    6 ͭߪೖ͢Δʹ͸ 6 Ҏ্ͷࡏݿ͕ඞཁ [hotfix] if shop.stock_of(Item.EGG) >= 6: shopping.take(Item.EGG, 6) ͬͯͳײ͡Ͱ߄ͯΔͱΖ͘ͳ͜ͱΛ͠·ͤΜ ͔͘͜͠ͳ͍ͷͰ৚݅จΛʜ
  32. $ python main.py ⏎ ΠΦϯ υʔϜγςΟళ Ͱͷ͓ങ্͍͛: ͸ ແ shop.stocks(Item.EGG,

    5) shop.stocks(Item.MILK, 0)
  33. ʮڇೕͳ͔ͬͨ ͠ΐ͏͕ͳ͍ɺΘ͔Δɻ Ͱ΋͞ɺͳΜͰཛ͋ͬͨ͜ΜͳΒɺ ͋Δ͚ͩങͬͯདྷͳ͔ͬͨͷ ʯ ʮͭങ͑ͳ͍͔Βɺ͔ͳʯ ΘΓͱϦΞϧʹ͜͏͍͏ϛε͸ى͖·͢

  34. stock = shop.stock_of(Item.EGG) if stock > 0: if stock <

    6: shopping.take(Item.EGG, stock) else: shopping.take(Item.EGG, 6) ͋Δͱ͖͚ͩങͬͯɺͳ͍ͱ͖͸ങͬͪΌμϝͰʜ ࡏݿ͕ΑΓগͳ͍ͳΒࡏݿͿΜ͚ͩങͬͯ ͦ͏͡Όͳ͔ͬͨΒങͬͯʜ ͜Ε͕ڇೕͱཛʹରͯ͠ඞཁͰʜ ͜͏ͳͬͯ͠·͏ͱటপͰ͢
  35. class Shopping: def wants(self, item, wanted): stock = shop.stock_of(item) if

    stock < 1: # ࡏݿ͕ͻͱͭ΋ͳ͚Ε͹खʹऔΕͳ͍ pass elif stock < wanted: # ࡏݿ͕ཉ͍͠ྔΑΓগͳ͍ͱ͖͸ࡏݿ෼Ͱզຫ͢Δ self.take(item, stock) else: # ࡏݿʹ໰୊͕ͳ͚Ε͹ཉ͍͠෼͚ͩߪೖͰ͖Δ self.take(item, wanted) ͦΜͳෳࡶͳهड़Λɺཁ͢ΔʹʮԿ͕Կݸཉ͍͠ͷ͔ʯͱ ݴ͍׵͑ΒΕΔΑ͏ʹ͢ΔͳͲ͍ͯ͘͠ͱ shopping.wants(Item.EGG, 6) ཛͭཉ͍͠Ͱ͢
  36. buyer = Buyer(shop) buyer.wants(Item.MILK, 1) buyer.purchase() class Buyer: def wants(self,

    item, wanted): ... def purchase(self): ... ʮ঎඼Λཉ͍͠ʯͱࢥ͏ͷ͸ʮߪೖऀʯͩ͠ ʮߪೖऀʯͷ͢Δ͜ͱ͸ʮߪങʯͩΑͶ ʜͱɺ࡞ͬͨ֓೦͕ͩΜͩΜɺݱ࣮Λ໛ͨ͠ײ͡ʹۙ͘ͳΓ·͢ ߪೖऀ͸ ڇೕΛͻͱͭཉ͍͠ͱݴͬͯ ۚΛ෷ͬͨ ໊લΛม͑ͯΈͨ ໊લΛม͑ͯΈͨ
  37. shop = Shop('ΠΦϯ υʔϜγςΟళ') shop.stocks(Item.MILK, 0) shop.stocks(Item.EGG, 5) buyer =

    Buyer(shop) buyer.wants(Item.MILK, 1) buyer.wants(Item.EGG, 6) receipt = buyer.purchase() print(receipt) ຊ෺ͷϓϩάϥϚʔͷॻ͘ίʔυ͸ɺ ෳࡶ͗ͯ͢ී௨ͷਓ͕ಡΊͳ͍ຐ๏ͷΑ͏ͳ΋ͷͰ͸ͳͯ͘ɺ Ұݟ୭Ͱ΋ॻ͚ΔΜ͡Όͳ͍͔ͱࢥ͑ͯ͠·͏΄Ͳ؆୯ʹͳΓ·͢ ୭Ͱ΋ॻ͚ͦ͏ͳͷʹຌਓʹ͸Ͱ͖ͳ͍ɺ ͱ͍͏఺Ͱચ࿅͞ΕͨσβΠϯͱࣅ͍ͯ·͢
  38. ങ͖ͬͯͯཉ͍͠΋ͷ ڇೕύοΫʜ ཛʜ ଍Γͳͯ͘΋ళʹ͋Δ෼Ͱ͍͍Α— ຊ෺ͷϓϩάϥϚʔ͕ٻΊΔ࢓༷͸ɺ ʮʙͨΒʯͷखॱͰ͸ͳͯ͘ɺ཰௚Ͱ؆ܿͳʮཁٻʯͰ͢ɻ ίʔυΛॻ͍ͯؾ͚ͮͳ͍ਓ͕ߟ͑ͨϩδοΫͳΜͯɺͲ͏ͤ࿙Ε͕͋ΔͷͰ

  39. ͦͷཁٻͱ࣮૷ίʔυ͸ɺ ݴޠ͕ҧ͏͚ͩͷɺ ҙຯ͕ಉ͡จষʹͳΔͷ͕ཧ૝Ͱ͢ ڇೕύοΫʜ ཛʜ buyer.wants(Item.MILK, 1) buyer.wants(Item.EGG, 6) པΉਓͷ੠ͱϓϩάϥϜͱ͸ɺͦͷઌೖ؍ʹ൓ͯ͠ɺ

    ૬ޓʹ࡞༻͢Δ Α͏ʹ຿ΊΔ΂͖ ΋ͷͳͷͰ͢
  40. ຊ෺ͷϓϩάϥϚʔʹʮݴΘΕͨͱ͓Γʹ࡞ͬͯʯͱ Ұํతʹݴͬͯ͸͍͚·ͤΜɻ ຊ෺ͷϓϩάϥϚʔͱ͸ɺର࿩Λ௨ͯ͡ ʮཔΈࣄͷݴ͍ํΛमਖ਼ʯ͍͖ͯ͠·͢ɻ ཧղ͠߹͏͜ͱ͕࠷ྑͷ݁ՌΛ΋ͨΒ͠·͢ɻ

  41. shop = Shop('ΠΦϯ υʔϜγςΟళ') shop.stocks(Item.MILK, 0) shop.stocks(Item.EGG, 5) buyer =

    Buyer(shop) buyer.wants(Item.MILK, 1) buyer.wants(Item.EGG, 6) receipt = buyer.purchase() print(receipt)
  42. $ python main.py ⏎ ΠΦϯ υʔϜγςΟళ Ͱͷ͓ങ্͍͛: ཛ ... 5఺

    ฼͕ٻΊ͍ͯͨങ͍෺ ʮ͝ΊΜڇೕ͸ͳ͔͚ͬͨͲɺཛ͸ͳΜͱ͔ͭ࢒ͬͯͨΑʯ ʮؾ͕ར͘ΘͶʯ ΊͰͨ͠ΊͰͨ͠