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. ϓϩάϥϚʔͬͯ
    ͲΜͳਓ
    ڇೕͱཛͰཧղ͢ΔϓϩάϥϚʔͱ͍͏ਓछ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  4. from shop import Item, Shop
    shop = Shop('ΠΦϯ υʔϜγςΟళ')
    cart = shop.cart()
    cart.add(Item.MILK)
    receipt = shop.paycheck(cart)
    print(receipt)
    ࣮ࡍʹ࡞ͬͯΈͨͷͰݕূ͠·͠ΐ͏
    ͓ళͷΧʔτʹ঎඼ΛೖΕͯࢧ෷͍Λ͢ΔͱϨγʔτ͕ग़·͢
    IUUQTHJTUHJUIVCDPNUBOBLBIJTBUFSV
    EGFDBBFGDCBGEE

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  10. item = Item.MILK
    count = 1
    for i in range(count):
    cart.add(item)
    ϓϩάϥϚʔ͸͔͍͜͠ͷͰɺม਺Λ࢖͑·͢

    View full-size slide

  11. ʮങ͖ͬͯͯʯ
    ʮങ͏ʯखॱ͸εςοϓ͕ଟ͍͔Β·ͱΊΑ͏
    ͦͯ͠ʮങ͏ʯΛҙຯ͢Δ໊લ΋͚ͭͳ͖Ό
    ϓϩάϥϚʔ೴ʹ͔͔Δͱ

    View full-size slide

  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)
    ϓϩάϥϚʔ͸͔͍͜͠ͷͰɺؔ਺Λ࡞Ε·͢

    View full-size slide

  13. ʮཛ͕͋ͬͨΒʯ
    ϓϩάϥϚʔ೴ʹ͔͔Δͱ

    View full-size slide

  14. count = 1
    # ௥Ճ࢓༷ ళʹཛ͕͋Δ৔߹͸ߪೖྔΛ 6 ͱ͢Δ
    if shop.stock_of(Item.EGG) > 0:
    count = 6
    ϓϩάϥϚʔ͸ਓʹࢥΘΕ͍ͯΔ΄Ͳ࣮ࡍʹ͸
    ͔͘͜͠ͳ͍ͷͰɺ͙͢ʹʮ৚݅จʯΛཚ༻͠·͢
    ͪͳΈʹʮ͋ͱͦΕ͔Β͜Ε΋ʯͱ࠷ޙʹݴΘΕͨ͜ͱ͸ಛʹ΍Β͔͠·͢

    View full-size slide

  15. $ python main.py ⏎
    ΠΦϯ υʔϜγςΟళ Ͱͷ͓ങ্͍͛:
    ڇೕύοΫ ... 6఺
    ʮͩͬͯཛ͕͔͋ͬͨΒʯ
    ͔ͯ͘͠ʜ

    View full-size slide

  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)
    ಄ͷମૢ
    Ͳ͕͓͔͍͜͠Ͱ͠ΐ͏͔
    ώϯτ͔͜͜ΒԼ

    View full-size slide

  17. ϓϩάϥϜࢥͬͨͱ͓Γʹ͸ಈ͔ͳ͍ɻॻ͍ͨͱ͓Γʹಈ͘ɻ
    ϓϩάϥϚʔݴΘΕͨ͜ͱ͚ͩΛ΍ΕͱݴΘΕΔͱɺ
    ༩͑ΒΕͨϩδοΫؒҧ͍Λͦͷ··࠶ݱͯ͠͠·͏ɻ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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)
    ΄Μͱ͏ʹ͔͍͜͠ϓϩάϥϚʔ͸
    ࢥߟΛิ͏ͨΊʹʮผͷ֓೦ʯΛͻͶΓग़͠·͢
    ࣮ࡏ͠ͳ͍ʮങ͍෺͢Δͱ͍͏͜ͱʯΛදݱͯ͠ΈΑ͏
    ങ͍෺ͱ͸
    ঎඼ΛखʹऔΔ͜ͱ
    ࢧ෷͏͜ͱ

    View full-size slide

  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)
    ͦͷ֓೦Λ࢖ͬͯʮจষͷΑ͏ʹॻ͘ʯͱɺ
    ύζϧͷ··Ͱ͸ݟ͑ͳ͔ͬͨʮؾ͖ͮʯ͕͋Γ·͢
    ͋Ε ͜͜঎඼ͷࢦఆ͕ཁΔͧ

    View full-size slide

  22. shopping = Shopping(shop)
    shopping.take(Item.MILK, 1)
    if shop.stock_of(Item.EGG) > 0:
    shopping.take(Item.EGG, 6)
    ʮಡΊΔʯίʔυ͸͙͢ʹͳ͓ͤ·͢
    ཛ͕͋ͬͨͱ͖ങͬͯཉ͍͠ͷ͸ɺڇೕ͡Όͳͯ͘ී௨ཛͰ͠ΐ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  26. $ python main.py ⏎
    ΠΦϯ υʔϜγςΟళ Ͱͷ͓ങ্͍͛:
    ڇೕύοΫ ... 1఺
    shop = Shop('ΠΦϯ υʔϜγςΟళ')
    shop.stocks(Item.EGG, 0)
    ͔͍͜͠ϓϩάϥϚʔ͸ɺཛ͕ͳ͍৔߹΋ςετ͠·͢
    ళͷࡏݿΛʹͯ͠ΈΔ

    View full-size slide

  27. shop = Shop('ΠΦϯ υʔϜγςΟళ')
    shop.stocks(Item.EGG, 5)
    if shop.stock_of(Item.EGG) > 0:
    shopping.take(Item.EGG, 6)
    ͋Ε ཛͷࡏݿ͕ΑΓগͳ͍ͱʜ
    ΋ͬͱ͔͍͜͠ϓϩάϥϚʔ͸ɺςετ͔ΒϩδοΫΛൃݟ͠·͢
    ͭ͋Δ΋ͷΛ
    ͭͰ΋͋Ε͹
    ͭങ͏

    View full-size slide

  28. shop = Shop('ΠΦϯ υʔϜγςΟళ')
    shop.stocks(Item.MILK, 0)
    shopping.take(Item.MILK, 1)
    ͦ΋ͦ΋ɺࡏݿ͋Δ͔Θ͔Βͳ͍ͷʹɺ
    ແ৚݅ʹڇೕΛങ͓͏ͱͯ͠Δ͚Ͳʜ

    View full-size slide

  29. $ python main.py
    Traceback (most recent call last):
    File "main.py", line 43, in
    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: ཛͷࡏݿ͕͋Γ·ͤΜ
    ίʔυ͸ؒҧͬͯͳͯ͘΋ɺ͓͔͠ͳϩδοΫͩͱɺ
    ࣮ߦͨ͠ͱ͖Τϥʔ͕ग़·͢
    ʮ͏ΘɺͳΜ͔Ϡόͦ͏ͳͷग़Δ΍Μʯ

    View full-size slide

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

    View full-size slide

  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)
    ͬͯͳײ͡Ͱ߄ͯΔͱΖ͘ͳ͜ͱΛ͠·ͤΜ
    ͔͘͜͠ͳ͍ͷͰ৚݅จΛʜ

    View full-size slide

  32. $ python main.py ⏎
    ΠΦϯ υʔϜγςΟళ Ͱͷ͓ങ্͍͛:
    ͸

    shop.stocks(Item.EGG, 5)
    shop.stocks(Item.MILK, 0)

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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) ཛͭཉ͍͠Ͱ͢

    View full-size slide

  36. buyer = Buyer(shop)
    buyer.wants(Item.MILK, 1)
    buyer.purchase()
    class Buyer:
    def wants(self, item, wanted):
    ...
    def purchase(self):
    ...
    ʮ঎඼Λཉ͍͠ʯͱࢥ͏ͷ͸ʮߪೖऀʯͩ͠
    ʮߪೖऀʯͷ͢Δ͜ͱ͸ʮߪങʯͩΑͶ
    ʜͱɺ࡞ͬͨ֓೦͕ͩΜͩΜɺݱ࣮Λ໛ͨ͠ײ͡ʹۙ͘ͳΓ·͢
    ߪೖऀ͸
    ڇೕΛͻͱͭཉ͍͠ͱݴͬͯ
    ۚΛ෷ͬͨ
    ໊લΛม͑ͯΈͨ
    ໊લΛม͑ͯΈͨ

    View full-size slide

  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)
    ຊ෺ͷϓϩάϥϚʔͷॻ͘ίʔυ͸ɺ
    ෳࡶ͗ͯ͢ී௨ͷਓ͕ಡΊͳ͍ຐ๏ͷΑ͏ͳ΋ͷͰ͸ͳͯ͘ɺ
    Ұݟ୭Ͱ΋ॻ͚ΔΜ͡Όͳ͍͔ͱࢥ͑ͯ͠·͏΄Ͳ؆୯ʹͳΓ·͢
    ୭Ͱ΋ॻ͚ͦ͏ͳͷʹຌਓʹ͸Ͱ͖ͳ͍ɺ
    ͱ͍͏఺Ͱચ࿅͞ΕͨσβΠϯͱࣅ͍ͯ·͢

    View full-size slide

  38. ങ͖ͬͯͯཉ͍͠΋ͷ
    ڇೕύοΫʜ
    ཛʜ
    ଍Γͳͯ͘΋ళʹ͋Δ෼Ͱ͍͍Α—

    ຊ෺ͷϓϩάϥϚʔ͕ٻΊΔ࢓༷͸ɺ
    ʮʙͨΒʯͷखॱͰ͸ͳͯ͘ɺ཰௚Ͱ؆ܿͳʮཁٻʯͰ͢ɻ
    ίʔυΛॻ͍ͯؾ͚ͮͳ͍ਓ͕ߟ͑ͨϩδοΫͳΜͯɺͲ͏ͤ࿙Ε͕͋ΔͷͰ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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)

    View full-size slide

  42. $ python main.py ⏎
    ΠΦϯ υʔϜγςΟళ Ͱͷ͓ങ্͍͛:
    ཛ ... 5఺
    ฼͕ٻΊ͍ͯͨങ͍෺
    ʮ͝ΊΜڇೕ͸ͳ͔͚ͬͨͲɺཛ͸ͳΜͱ͔ͭ࢒ͬͯͨΑʯ
    ʮؾ͕ར͘ΘͶʯ
    ΊͰͨ͠ΊͰͨ͠

    View full-size slide