Raphael Roth
Raphael Roth

Reputation: 27373

Change types in a given scala function

I have a function

f : Point2D => Point2D = ???

I want to convert it to a function of tuples :

(Double,Double) => (Double,Double)

I know how to map Point2D to Tuple2 :

implicit def pointToTuple(p : Point2D) = (p.x,p.y)

but I have no idea how to map the function itself :

def convertFunction(f : Point2D => Point2D) : (Double,Double) => (Double,Double) = {
    ???
 }

Upvotes: 0

Views: 55

Answers (1)

Dima
Dima

Reputation: 40500

You also need tupleToPoint, not just pointToTuple.

Now, provided you have that, you can just do:

  def convertFunction(f: Point2D => Point2D) = 
    tupleToPoint andThen f andThen pointToTuple

Upvotes: 1

Related Questions