What should I import for Scalaz' traverse functionalities

Antoine

In every examples I read about Scalaz' traverse features, the following imports were done:

import scalaz._
import Scalaz._

It seems I can't use traverseU until I import Scalaz._.

How does Scalaz object inject traverseU to my collections? I'm completely lost in the reference doc.

What should I import if I just want the traverse and traverseU methods?

senia

For collection traverseU func you'll have to import syntax for traverseU (implicit method for TraverseOps), implicit instance of Traverse[C] (for collection type C) and Applicative[R] (for func result type 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 result type of func is not R[X] with Applicative[R], but some R with Monoid[R] you'll have to import Monoid[R] instance for implicit method Applicative.monoidApplicative:

import scalaz.std.anyVal.intInstance

List(1, 2, 3).traverseU{ identity }
// 6

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

So if you want to get only Traverse[List] for some good reason, you'll have to us it this way:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Scalaz .option - what is this shorthand for?

来自分类Dev

what port number should I use in mongodb

来自分类Dev

如何将Scalaz的traverse和traverseU与Either一起使用

来自分类Dev

Python class inheritance not doing what I think it should

来自分类Dev

What version manager should I use to manage multiple Scala versions?

来自分类Dev

what things i should know when using mailx command

来自分类Dev

What query should I write to calculate the cumulative salary of employees from the salary table

来自分类Dev

What should be in my View Models?

来自分类Dev

Should I always complete a TaskCompletionSource?

来自分类Dev

How should I parse this data?

来自分类Dev

I want to be able to import the JavaMail API and use it in Android Studio. What do I do after downloading mail.jar and activation.jar?

来自分类Dev

Scalaz Kleisli使用的好处

来自分类Dev

Scalaz:未找到monoid

来自分类Dev

从scalaz导入`IO`

来自分类Dev

Scalaz Kleisli使用的好处

来自分类Dev

如何测试scalaz IO?

来自分类Dev

ScalaZ OptionT的例外

来自分类Dev

使用scalaz的===代表adt的

来自分类Dev

scalaz Monoid for SortedMap

来自分类Dev

如何测试scalaz IO?

来自分类Dev

Scalaz:未找到monoid

来自分类Dev

How to use from import if I would like to import the module as a String?

来自分类Dev

Should I be throwing different exception types?

来自分类Dev

Should I use copy or strong with arrays?

来自分类Dev

Should I use a pointer when dealing with lists?

来自分类Dev

Should I pass information inside Observer Events

来自分类Dev

Spark - Which language should I use?

来自分类Dev

How should I define this complex function template?

来自分类Dev

Where should I implement my class method?