Making sense of Scala FP Libraries

只谈情不闲聊 提交于 2020-05-09 18:47:04

问题


Just for the sake of quick clarity for someone who wants to start working with Scala FP library, on a journey to become better at pure FP.

Would someone clarify the difference/relation between Cats and Cats-Effect, Cats-Effects IO? On top of that, where does Zio, and Monix stand with respect to it? Finally, what would be the relation to ScalaZ 7/8?

So far based on what I have read, a good combination of the library to work with based on the documentation available and what they do would be Cats, then Cats-Effect, and ZIO which could be used with Cats-effects? But I can't quite understand well why and would like to set myself a good path to learn to be better FP programmer while remaining productive, and not having to finish "FP Programming in Scala" before I start making choices.


回答1:


Scalaz started as an attempt to port to Scala some well-established abstractions from Haskell (like typeclasses for Monad, Functor and much more). Problem with it was, that it doesn't have great documentation, so basically, you needed to use documentation of Haskell libraries in order to understand how to use certain Scalaz resources. Nowadays, you there's Sam Halliday’s Functional Programming for Mortals which you can use as a learning source for Scalaz.

Cats was created later, as essentially reimplementation what Scalaz provided. Cats has a lot better documentation than Scalaz, there is also great book Scala with Cats.

Scalaz and Cats might have very similar purposes, so they're competing as general purpose FP library for Scala. There are also libraries that serve as compatibility-layer between both libraries.

Cats-Effect is a library, which provides "standard" IO monad for Scala (again idea borrowed from Haskell (?)). It depends on code from Cats core library.

You can read more here why there is a need for IO monad for Scala, when there's standard's library Future.

Monix is another library, which provides an IO monad for Scala, but this time it's called Task. It was meant to be a more high-level abstraction and provide easier interop with code using standard library Future. In reality, it shares a lot of code with Cats-Effect and creator of Monix Alexandru Nedelcu is also one of the main contributors of Cats-Effect.
Here you may find more information about the differences between cats.effect.IO and monix.eval.Task, as well as some of the history of both.

Lastly, there is ZIO which started as an attempt to reimplement IO monad for Scalaz, but ended up as a completely separate library (so it does not depend on Scalaz codebase).

The great thing about all libraries is, they're all implementing typeclasses (like Sync or Concurrent) from Cats-Effect, so using pattern called tagless final you're able to switch between implementation.

That hierarchy of typeclasses also serve as an interpolation library between, many (all (?)) of the IO implementations (as by the time it was created there already were fs2.Task, monix.Task & scalaz.IO). Also, apparently, in a future the IO part may be moved into another module, leaving only the interoperability typeclasses.

If you don't use tagless final you can still use modules that provide interop between certain IO monads, for example zio-interop-cats (between ZIO and Cats-Effect or catnap for Monix-Cats-Effect.



来源:https://stackoverflow.com/questions/56868566/making-sense-of-scala-fp-libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!