Re:Functors, Applicatives, and Monads in Pictures (2013)
by tel on Thursday October 21, 2021 @06:08PM
(#28951426)
You can also look at the Scala implementations if you like. Scala has HKTs so you can write it all directly.
trait Functor[F[_]] {
def map[A, B](f: A => B)(x: F[A]): F[B]
}
trait Applicative[F[_]] extends Functor[F] {
def pure[A](a: A): F[A]
def apply[A, B](f: F[A => B])(x: F[A]): F[B]
def product[A, B](xa: F[A], xb: F[B]): F[(A, B)]
}
trait Monad[F[_]] extends Applicative[F] {
def bind[A, B](f: A => F[B])(x: F[A]): F[B]
def join[A](x: F[F[A]]): F[A]
}
Re:Functors, Applicatives, and Monads in Pictures (2013)
by dunefox on Thursday October 21, 2021 @05:51AM
(#28942317)
That version is extremely noisy, imo.
Re:Functors, Applicatives, and Monads in Pictures (2013)
by marcosdumay on Thursday October 21, 2021 @11:17AM
(#28945668)
I once had to add logging to a large set of methods in C#, so I thought, well, let's make a function decorator like Python or Haskell, there's no reason it wouldn't work on C#.
Well, it indeed worked perfectly well. But the amount of noise added to satisfy the type checker was larger than reimplementing the log procedure at each place.
I think it's primarily because this is the form you use them in in a language like Haskell. Most explanations are exported from Haskell in one way or another, so they're deeply colored by Haskellisms.
It's the same reason why `ap` and `bind` are generally preferred over `times` and `join` -- in Haskell you can even use `f <$> a <*> b`, which is a deeply clever use of infix `fmap` and `ap` that drives a syntactic analogy with regular function application, `f a b`. Try doing that in another language and you'll barf. And do-notation desugars most cleanly into >>=, so that's where Haskell-oriented explanations start from.
If you implement these in Java (which you can, you just can't abstract over T), it looks like this:
That is, the second argument `T a` in the top poster's comment becomes the implicit `this` parameter for instance methods. I find the `static` alternatives often make more syntactic sense in context; just goes to show that one language's norms don't always transfer.