News for hackers. Stuff that matters.
Re:Functors and Monads for People Who Have Read Too Many “Tu... by 0xFF69B4 on Saturday June 26, 2021 @06:25AM (#27640725)
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.

There are no comments on this comment yet.