Scalaz Kleisli question
Asked Answered
A

1

7

There is a trait called Kleisli in the scalaz library. 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:

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:

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?

Aldercy answered 1/4, 2010 at 10:56 Comment(0)
G
3

Seems like you want transpose. Scalaz doesn't supply this, but it should be easy to write. The function m that you want is val m = f andThen (_.map(g)) andThen transpose andThen (_.join)

Garlic answered 1/4, 2010 at 17:52 Comment(2)
I'm not able to try this right now, but can't we use MA#sequence to perform transposition? scalaz.googlecode.com/svn/continuous/latest/browse.sxr/scalaz/…Ames
You're right. You totally can, given the "zippy" Applicative instance for streams (or ZipStream).Garlic

© 2022 - 2024 — McMap. All rights reserved.