Let's take a contrasting look at the complexity of implementing data structures in different languages.
Let's implement a simple sum type, Either
, in a few
polymorphic languages.
Language | Source | Lines of code |
---|---|---|
Java | Either.java | 75 |
Scala | Either.scala | 3 |
Haskell | Either.hs | 1 |
Either
is useful for storing a value of one of two
types, but it isn't useful for building up behavior from multiple
disjunctions.
Let's make it composable by adding map
, ap
,
and flatMap
(a.k.a. fmap
,
<*>
, and >>=
), and biasing it to
the right.
Language | Source | Lines of code |
---|---|---|
Java | RightBiasedEither.java | 102 |
Scala | RightBiasedEither.scala | 15 |
Haskell | RightBiasedEither.hs | 9 |