简体   繁体   English

Scalaz Kleisli提问

[英]Scalaz Kleisli question

There is a trait called Kleisli in the scalaz library. scalaz库中有一个名为Kleislitrait Looking at the code: 看代码:

import scalaz._
import Scalaz._
type StringPair = (String, String)

val f: Int => List[String]        = (i: Int) => List((i |+| 1).toString, (i |+| 2).toString)
val g: String => List[StringPair] = (s: String) => List("X" -> s, s -> "Y")

val k = kleisli(f) >=> kleisli(g) //this gives me a function: Int => List[(String, String)]

Calling the function k with the value of 2 gives: 使用值2调用函数k给出:

println( k(2) ) //Prints: List((X,3), (3,Y), (X,4), (4,Y))

My question is: how would I use Scalaz to combine f and g to get a function m such that the output of m(2) would be: 我的问题是: 我如何使用Scalaz将f和g组合起来得到函数m,使m(2)的输出为:

val m = //??? some combination of f and g
println( m(2) ) //Prints: List((X,3), (X,4), (3,Y), (4,Y))

Is this even possible? 这甚至可能吗?

Seems like you want transpose . 好像你想要transpose Scalaz doesn't supply this, but it should be easy to write. Scalaz不提供这个,但它应该很容易写。 The function m that you want is val m = f andThen (_.map(g)) andThen transpose andThen (_.join) 你想要的函数mval m = f andThen (_.map(g)) andThen transpose andThen (_.join)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM