Reduce / fold over list of monoid but reducer returns Either

淺唱寂寞╮ 提交于 2019-12-05 12:25:49

You might want to take a look at foldM. Your code would then look approximately like this:

Foldable[Vector].foldM(items, "")(combiner)

The foldM method has the signature

def foldM[G[_], A, B](fa: F[A], z: B)(f: (B, A) ⇒ G[B])(implicit G: Monad[G]): G[B]

so in your case, the type(-constructor) parameters would unify as follows:

  • G[X] = Either[String, ?]
  • A = String
  • B = String
  • F[X] = Vector[X]

so that f: (A, B) => G[B] would become f: (String, String) => Either[String, String], which is exactly the type of combiner when it's converted into a function.

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