了解Scala FP库 [英] Making sense of Scala FP Libraries

查看:192
本文介绍了了解Scala FP库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅出于快速起见,对于那些想要开始使用Scala FP库的人来说,这是一个在纯FP方面变得更好的旅程.

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.

有人可以澄清一下Cats和Cats-Effects,Cats-Effects IO之间的区别/关系吗?最重要的是,Zio和Monix对此持何立场?最后,与ScalaZ 7/8有什么关系?

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?

到目前为止,根据我所读的内容,可以根据可用的文档很好地组合要使用的库,它们的作用是Cats,然后是Cats-Effect,以及可以与Cats-effects一起使用的ZIO?但是我不太清楚为什么,并且想为自己设定一条学习如何成为更好的FP程序员的良好道路,同时保持生产力,并且不必在开始选择之前完成"Scala FP编程".

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.

推荐答案

Scalaz 开始是为了将Haskell的一些完善的抽象移植到Scala(例如Monad,Functor的类型类等等) ).问题在于它没有很好的文档,因此,基本上,您需要使用Haskell库的文档才能了解如何使用某些 Scalaz 资源. 如今,您有Sam Halliday的凡人功能编程,您可以将其用作 Scalaz 的学习资源.

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.

是后来创建的,实际上是重新实现了 Scalaz 所提供的功能. Cat 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 Cats 可能具有非常相似的用途,因此它们正在争夺Scala的通用FP库.在这两个库之间,还有一些库可用作兼容性层.

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 是一个库,为Scala提供标准" IO monad (再次从Haskell(?)借来的想法) .它取决于 Cats 核心库中的代码.

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.

您可以在此处了解更多信息当存在标准库 Future 时,为什么Scala需要 IO monad.

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

Monix 是另一个库,为Scala提供了 IO monad ,但这次将其称为Task.它本来应该是一个高级抽象,并使用标准库 Future 提供更轻松的代码互操作.实际上,它与 Cats-Effect Monix 的创建者此处,您可能会发现有关差异的更多信息在cats.effect.IOmonix.eval.Task之间,以及两者的一些历史记录.

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.

最后,有一个 ZIO ,其开始是为 Scalaz 重新实现 IO monad 的尝试,但最终成为了一个完全独立的库(因此它不依赖 Scalaz 代码库.

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).

所有库的妙处在于,它们都实现了 Cats-Effect 的类型类(例如 Sync Concurrent ),因此使用称为 tagless final 的模式,您可以在实现之间进行切换.

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.

类型类的层次结构还用作许多(所有(?)) IO 实现之间的内插库(截至在该处创建时)是fs2.Taskmonix.Taskscalaz.IO).同样,显然,在 IO 部分的未来中可以移到另一个模块中,仅保留互操作性 typeclasss .

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.

如果您不使用 tagless final ,您仍然可以使用在某些 IO 单子之间提供互操作的模块,例如 zio-interop-cats (介于 ZIO Cats-Effect 之间,或 catnap 用于 Monix - Cats-Effect .

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.

这篇关于了解Scala FP库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆