top | item 27640725

(no title)

0xFF69B4 | 4 years ago

Monads and Functors aren't particularly exciting in Python because properties or contexts can be expressed with e.g. boolean flags and properties or contexts can be ignored when applying functions to unrelated parts of an object.

  # milk_cows :: Person -> Tired Person
  def milk_cows(person):
    ...
    person["tired"] = True
    return person

  # feed_pigs :: Person -> Tired Person
  def feed_pigs(person):
    ...
    person["tired"] = True
    return person

  # birthday :: Person -> Person
  def birthday(person):
    person["age"] += 1
    return person

  # willy :: Person
  willy = {"age": 32, "tired": False}

  # fmap and >>=
  print(milk_cows(birthday(feed_pigs(willy))))
In Haskell there would be a 'Tired a' type with a Monad instance to express the tiredness property and a Functor instance to apply functions to it with no impact on or knowledge of tiredness, e.g. aging.

discuss

order

No comments yet.