Is there any scala library that treat tuples as monads

349 views Asked by At

Is there any scala library that enriches basic scala tuples with monad syntax. Something similar to the Writer monad but adjusted for usage with tuples.

What I look for:

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

should equal to (3, "as"). As well as

for (p <- pair) yield (p+1)
1

There are 1 answers

1
Travis Brown On BEST ANSWER

Yep, Scalaz provides monad instances for tuples (up 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 the type alias isn't necessary—it just makes using point a little easier.)