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

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

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

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

Hisateru Tanaka

February 28, 2019
Tweet

More Decks by Hisateru Tanaka

Other Decks in Programming

Transcript

  1. from shop import Item, Shop shop = Shop('ΠΦϯ υʔϜγςΟళ') cart

    = shop.cart() cart.add(Item.MILK) receipt = shop.paycheck(cart) print(receipt) ࣮ࡍʹ࡞ͬͯΈͨͷͰݕূ͠·͠ΐ͏ ͓ళͷΧʔτʹ঎඼ΛೖΕͯࢧ෷͍Λ͢ΔͱϨγʔτ͕ग़·͢ IUUQTHJTUHJUIVCDPNUBOBLBIJTBUFSV EGFDBBFGDCBGEE
  2. item = Item.MILK count = 1 for i in range(count):

    cart.add(item) ϓϩάϥϚʔ͸͔͍͜͠ͷͰɺม਺Λ࢖͑·͢
  3. 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) ϓϩάϥϚʔ͸͔͍͜͠ͷͰɺؔ਺Λ࡞Ε·͢
  4. count = 1 # ௥Ճ࢓༷ ళʹཛ͕͋Δ৔߹͸ߪೖྔΛ 6 ͱ͢Δ if shop.stock_of(Item.EGG)

    > 0: count = 6 ϓϩάϥϚʔ͸ਓʹࢥΘΕ͍ͯΔ΄Ͳ࣮ࡍʹ͸ ͔͘͜͠ͳ͍ͷͰɺ͙͢ʹʮ৚݅จʯΛཚ༻͠·͢ ͪͳΈʹʮ͋ͱͦΕ͔Β͜Ε΋ʯͱ࠷ޙʹݴΘΕͨ͜ͱ͸ಛʹ΍Β͔͠·͢
  5. 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) ಄ͷମૢ Ͳ͕͓͔͍͜͠Ͱ͠ΐ͏͔ ώϯτ͔͜͜ΒԼ
  6. 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) ΄Μͱ͏ʹ͔͍͜͠ϓϩάϥϚʔ͸ ࢥߟΛิ͏ͨΊʹʮผͷ֓೦ʯΛͻͶΓग़͠·͢ ࣮ࡏ͠ͳ͍ʮങ͍෺͢Δͱ͍͏͜ͱʯΛදݱͯ͠ΈΑ͏ ങ͍෺ͱ͸ ঎඼ΛखʹऔΔ͜ͱ ࢧ෷͏͜ͱ
  7. 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) ͦͷ֓೦Λ࢖ͬͯʮจষͷΑ͏ʹॻ͘ʯͱɺ ύζϧͷ··Ͱ͸ݟ͑ͳ͔ͬͨʮؾ͖ͮʯ͕͋Γ·͢ ͋Ε ͜͜঎඼ͷࢦఆ͕ཁΔͧ
  8. shopping = Shopping(shop) shopping.take(Item.MILK, 1) if shop.stock_of(Item.EGG) > 0: shopping.take(Item.EGG,

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

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

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

    shopping.take(Item.EGG, 6) ͋Ε ཛͷࡏݿ͕ΑΓগͳ͍ͱʜ ΋ͬͱ͔͍͜͠ϓϩάϥϚʔ͸ɺςετ͔ΒϩδοΫΛൃݟ͠·͢ ͭ͋Δ΋ͷΛ ͭͰ΋͋Ε͹ ͭങ͏
  12. $ 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: ཛͷࡏݿ͕͋Γ·ͤΜ ίʔυ͸ؒҧͬͯͳͯ͘΋ɺ͓͔͠ͳϩδοΫͩͱɺ ࣮ߦͨ͠ͱ͖Τϥʔ͕ग़·͢ ʮ͏ΘɺͳΜ͔Ϡόͦ͏ͳͷग़Δ΍Μʯ
  13. # FIXME ࢓༷όά: 1 ͭͰ΋ߪೖ͢Δʹ͸ࡏݿ͕ඞཁ shopping.take(Item.MILK, 1) # FIXME ࡏݿ͕

    1 ʙ 5 ͷͱ͖ΤϥʔͰམͪΔ if shop.stock_of(Item.EGG) > 0: shopping.take(Item.EGG, 6) ࡏݿ͕଍Γͳ͍ͱ͖ߪೖͨ͠Βࢮ͵΍͹͍ ແ৚݅ʹͭങ͖ͬͯͯͱ͔ɺͳΜͯةݥͳ࢓༷ͳΜͩ
  14. # ߪೖ͢Δʹ͸ࡏݿ͕ඞཁ [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) ͬͯͳײ͡Ͱ߄ͯΔͱΖ͘ͳ͜ͱΛ͠·ͤΜ ͔͘͜͠ͳ͍ͷͰ৚݅จΛʜ
  15. stock = shop.stock_of(Item.EGG) if stock > 0: if stock <

    6: shopping.take(Item.EGG, stock) else: shopping.take(Item.EGG, 6) ͋Δͱ͖͚ͩങͬͯɺͳ͍ͱ͖͸ങͬͪΌμϝͰʜ ࡏݿ͕ΑΓগͳ͍ͳΒࡏݿͿΜ͚ͩങͬͯ ͦ͏͡Όͳ͔ͬͨΒങͬͯʜ ͜Ε͕ڇೕͱཛʹରͯ͠ඞཁͰʜ ͜͏ͳͬͯ͠·͏ͱటপͰ͢
  16. 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) ཛͭཉ͍͠Ͱ͢
  17. buyer = Buyer(shop) buyer.wants(Item.MILK, 1) buyer.purchase() class Buyer: def wants(self,

    item, wanted): ... def purchase(self): ... ʮ঎඼Λཉ͍͠ʯͱࢥ͏ͷ͸ʮߪೖऀʯͩ͠ ʮߪೖऀʯͷ͢Δ͜ͱ͸ʮߪങʯͩΑͶ ʜͱɺ࡞ͬͨ֓೦͕ͩΜͩΜɺݱ࣮Λ໛ͨ͠ײ͡ʹۙ͘ͳΓ·͢ ߪೖऀ͸ ڇೕΛͻͱͭཉ͍͠ͱݴͬͯ ۚΛ෷ͬͨ ໊લΛม͑ͯΈͨ ໊લΛม͑ͯΈͨ
  18. 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) ຊ෺ͷϓϩάϥϚʔͷॻ͘ίʔυ͸ɺ ෳࡶ͗ͯ͢ී௨ͷਓ͕ಡΊͳ͍ຐ๏ͷΑ͏ͳ΋ͷͰ͸ͳͯ͘ɺ Ұݟ୭Ͱ΋ॻ͚ΔΜ͡Όͳ͍͔ͱࢥ͑ͯ͠·͏΄Ͳ؆୯ʹͳΓ·͢ ୭Ͱ΋ॻ͚ͦ͏ͳͷʹຌਓʹ͸Ͱ͖ͳ͍ɺ ͱ͍͏఺Ͱચ࿅͞ΕͨσβΠϯͱࣅ͍ͯ·͢
  19. 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)
  20. $ python main.py ⏎ ΠΦϯ υʔϜγςΟళ Ͱͷ͓ങ্͍͛: ཛ ... 5఺

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