question Scalaz Kleisli
-
23-09-2019 - |
Question
Il y a un trait
appelé Kleisli dans la bibliothèque scalaz . En regardant le 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)]
L'appel de la fonction k
avec la valeur de 2 donne:
println( k(2) ) //Prints: List((X,3), (3,Y), (X,4), (4,Y))
Ma question est: comment pourrais-je utiliser Scalaz combiner f et g pour obtenir une fonction m telle que la sortie de m (2) serait:
val m = //??? some combination of f and g
println( m(2) ) //Prints: List((X,3), (X,4), (3,Y), (4,Y))
Est-ce même possible?
La solution
On dirait que vous voulez transpose
. Scalaz ne fournit pas, mais il devrait être facile à écrire. La m
fonction que vous voulez est val m = f andThen (_.map(g)) andThen transpose andThen (_.join)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow