Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I combine a tuple of values with a tuple of functions?

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.

like image 725
Daenyth Avatar asked Feb 18 '15 22:02

Daenyth


People also ask

How do I combine two tuples?

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.

How do I add a tuple to a tuple?

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 .

How do you concatenate elements of a tuple in Python?

To concatenate two tuples we will use ” + ” operator to concatenate in python.


Video Answer


2 Answers

Edit

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)

Old

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)
like image 150
lpiepiora Avatar answered Nov 08 '22 17:11

lpiepiora


Arrows? Something like:

(f *** g)(a, b)

http://eed3si9n.com/learning-scalaz/Arrow.html

like image 21
Victor Moroz Avatar answered Nov 08 '22 18:11

Victor Moroz