Domanda

C'è un trait chiamato Kleisli nel scalaz biblioteca. Guardando il codice:

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)]

La chiamata alla funzione k con il valore di 2 dà:

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

La mia domanda è: Come potrei usare Scalaz per combinare F e G per ottenere una funzione m tale che l'uscita di m (2) potrebbe essere:

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

Questo è anche possibile?

È stato utile?

Soluzione

Sembra che si desidera transpose . Scalaz non fornisce questo, ma dovrebbe essere facile da scrivere. La funzione m che si vuole è val m = f andThen (_.map(g)) andThen transpose andThen (_.join)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top