Fight the Future

Java言語とJVM、そしてJavaエコシステム全般にまつわること

モナドについて調べていく(12)

One Div Zero: Monads are Elephants Part 1の翻訳続き。
パート1はこれで終わり。

Conclusion for Part I(パート1の結論)


Scala monads must have map and flatMap methods. Map can be implemented via flatMap and a constructor or flatMap can be implemented via map and flatten.

ScalaモナドはmapとflatMapメソッドを持たなければなりません。mapはflatMapメソッドとコンストラクタを使って実装できますが、逆にflatMapがmapとflattenを使って実装していてもよいです。


flatMap is the heart of our elephantine beast. When you're new to monads, it may help to build at least the first version of a flatMap in terms of map and flatten. Map is usually pretty straight forward. Figuring out what makes sense for flatten is the hard part.

flatMapは象という動物の中心です。モナドに初めて触れるなら、少なくともmapとflattenを使って最初のバージョンのflatMapを構築するといいかもしれません。
mapは一般的にとても単純です。flattenのために意味を成しているところが難しい部分であると理解してください。


As you move into monads that aren't collections you may find that flatMap should be implemented first and map should be implemented based on it and unit.

モナドはコレクションではないとわかったとき、flatMapを最初に実装しmapがflatMapとunitをベースに実装すべきだということが理解できるでしょう。

In part 2 I'll cover Scala's syntactic sugar for monads. In part 3 I'll present the elephant's DNA: the monad laws. Finally, in part 4 I'll show a monad that's only barely a container. In the meantime, here's a cheat sheet for translating between computer science papers on monads, Haskell, and Scala.

パート2ではモナドのためScalaのシンタックスシュガーを紹介します。パート3では象のDNAすなわちモナド則を紹介します。
最後となるパート4ではどうにかコンテナであるようなモナドを示します。
話は変わって、モナドについてのコンピュータ科学論文とHaslkell、Scalaを変換するチートシートをお見せします。

Generic Haskell Scala
M data M a or newtype M a or instance Monad (M a) class M[A] or case class M[A] or trait M[A]
M a M a M[A]
unit v return v new M(v) or M(v)
map f m fmap f m m map f
bind f m m >>= f or f =<< m m flatMap f
join join flatten
do for