Finding my way through Scalaz [duplicate]

喜欢而已 提交于 2019-11-29 20:11:49

I would recommend the excellent series Learning scalaz by Eugene Yokota on Scalaz 7. The author follows the structure of Learn You a Haskell for Great Good. The approach is systematic and very pedagogic.

Travis Brown

My advice would be not to wait until you feel like you have a high-level understanding of the library—just pick a couple of tools to start with and then follow conceptual links as you go.

Validation (which by the way isn't actually a monad) is probably the single best place to start. If you've ever used Either for validation in the standard library, Validation will feel both familiar and much more convenient. You'll find lots of useful discussions of Validation both here on StackOverflow and elsewhere.

Once you're comfortable working with Validation you should have a good basic understanding of the applicative functor type class, which is useful in many other contexts.

Monoid is another good starting point. It's a very simple type class (essentially just an associative addition-like operation and an identity element), and once you understand it you'll see monoids everywhere. See for example this answer (full disclosure: it's by me) showing how to use monoids to solve a problem that might not initially look very monoidy.

There are lots of other handy little tools in Scalaz that you can use without needing to grasp the entire big picture. Bifunctor is one of my favorites—it makes working with tuples much more convenient by giving you a way to map a function over either side:

scala> import scalaz._, Scalaz._
import scalaz._
import Scalaz._

scala> def inc(i: Int) = i + 1
inc: (i: Int)Int

scala> def repeat(n: Int)(s: String) = s * n
repeat: (n: Int)(s: String)String

scala> (inc(_)) <-: (1, "a") :-> repeat(3)
res0: (Int, String) = (2,aaa)

Once you have a good working understanding of a few of these concepts, I'd suggest Brent Yorgey's Typeclassopedia, which is Haskell-oriented but does a fantastic job of giving you just enough category theory and abstract algebra to understand most of the stuff you'll find in Scalaz.

Some of the videos I found useful:

Most of these have great slides, if you are hardcore then read them without the video.

Also learn to read Haskell type signatures and browse the Haskell typeclassopedia.

While I would never turn anyone away from the Haskell tutorials, they can be a bit mind-boggling if you're an OOP-style developer and aren't familiar with why you'd ever want to live in a functional world.

I gave a talk called "scalaz For the Rest of Us" which approaches scalaz though examples that everyone is familiar with: memoization (Memo in scalaz), domain validation (Validation in scalaz), etc. That way the "use case" is clear and can start learning how to solve the familiar problems using the power of scalaz.

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