Is there a scala library that treats tuples as monads

Is there a scala library that enriches the base scala tuples with monad syntax. Something similar to a writer’s monad, but adjusted for use with tuples.

What I'm looking for:

val pair = (2, "as") pair >>= (a => point(a+1)) 

should equal (3, "as") . Like

 for (p <- pair) yield (p+1) 
+5
source share
1 answer

Yep, Scalaz provides monad instances for tuples (prior to Tuple8 ):

 import scalaz.std.anyVal._, scalaz.std.tuple._, scalaz.syntax.monad._ scala> type IntTuple[A] = (Int, A) defined type alias IntTuple scala> pair >>= (a => (a+1).point[IntTuple]) res0: (Int, String) = (2,as1) scala> for (p <- pair) yield (p + 1) res1: (Int, String) = (2,as1) 

(Note that a type alias is not needed - it simply simplifies the use of point .)

+8
source

All Articles