I have scalaZ available.
I have an (A, B)
and a (A => C, B => D)
, I'd like to get a (C, D)
in a simple and readable way.
I feel like there's something I can do with applicatives but I can't find the right methods.
Concatenation is done with the + operator, and multiplication is done with the * operator. Because the + operator can concatenate, it can be used to combine tuples to form a new tuple, though it cannot modify an existing tuple. The * operator can be used to multiply tuples.
tuple is immutable, but you can concatenate multiple tuples with the + operator. At this time, the original object remains unchanged, and a new object is generated. Only tuples can be concatenated. It cannot be concatenated with other types such as list .
To concatenate two tuples we will use ” + ” operator to concatenate in python.
Didn't get it at first, that the OP has tuple of functions. In such case as suggested in comments this should work:
val in = ("1", 2)
val fnT = ((s: String) => s.toInt, (i: Int) => i.toString)
val out = (in.bimap[Int, String] _).tupled(fnT)
If you have two functions and want to apply them on tuple, you should be able to do:
import scalaz._
import Scalaz._
val in = ("1", 2)
val sToi = (s: String) => s.toInt
val iTos = (i: Int) => i.toString
val out = sToi <-: in :-> iTos
// or
val out1 = in.bimap(sToi, iTos)
// or
val out2 = (sToi *** iTos)(in)
Arrows? Something like:
(f *** g)(a, b)
http://eed3si9n.com/learning-scalaz/Arrow.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With