What should I import for Scalaz cross functions?

In each example that I read about Scalaz's move functions, the following import operations were done:

import scalaz._ import Scalaz._ 

It seems I can not use traverseU while I import Scalaz._ .

How does a Scalaz object insert traverseU into my collections? I am completely lost in the reference document.

What do i need to import if i just need the traverse and traverseU methods?

+6
source share
3 answers

For collection traverseU func you will need to import the syntax for traverseU (implicit method for TraverseOps ), the implicit instance of Traverse[C] (for collection type C ) and Applicative[R] (for func the result type is R[X] ).

For instance:

 import scalaz.syntax.traverse.ToTraverseOps // F[A] => TraverseOps[F, A] import scalaz.std.list.listInstance // Traverse[List] import scalaz.std.option.optionInstance // Applicative[Option] List(1, 2, 3).traverseU{ Option(_) } // Some(List(1, 2, 3)) 

In case the result type func not R[X] with Applicative[R] , but some R with Monoid[R] you will have to import a Monoid[R] instance for the implicit Applicative.monoidApplicative method:

 import scalaz.std.anyVal.intInstance List(1, 2, 3).traverseU{ identity } // 6 

Note that listInstance also MonadPlus[List] , Zip[List] , Unzip[List] , etc.

So, if you want to get only Traverse[List] for some good reason, you have to do it like this:

 implicit val traverseList: scalaz.Traverse[List] = scalaz.std.list.listInstance implicit val applicativeOption: scalaz.Applicative[Option] = scalaz.std.option.optionInstance 
+7
source

Is there a specific reason why you do not want to import the entire library? AFAIK, you cannot import only traverse and traverseU - they do not exist in isolation. Read the Typeclass Pattern - this should start answering your questions about how Scalaz does what it does. To learn more about Scalaz in general, I would recommend learning the Scalaz series.

0
source

traverseU is on TraverseOps . Converting to this happens using TraverseSyntax . Therefore, import scalaz.syntax.TraverseSyntax._ should be enough.

-1
source

Source: https://habr.com/ru/post/979861/


All Articles