Pergunta

Existe um trait chamado Kleisli no scalraz biblioteca. Olhando para o código:

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

Chamando a função k com o valor de 2 dá:

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

Minha pergunta é: Como eu usaria o Scalaz para combinar F e G para obter uma função m, de modo que a saída de m (2) seja:

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

Isso é possível?

Foi útil?

Solução

Parece que você quer transpose. O Scalaz não fornece isso, mas deve ser fácil de escrever. A função m que você quer é val m = f andThen (_.map(g)) andThen transpose andThen (_.join)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top