首页 > 解决方案 > 更改给定 scala 函数中的类型

问题描述

我有一个功能

f : Point2D => Point2D = ???

我想将它转换为元组的函数:

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

我知道如何映射Point2DTuple2

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

但我不知道如何映射函数本身

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

标签: scala

解决方案


你也需要tupleToPoint,不只是pointToTuple

现在,只要你有这个,你可以这样做:

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

推荐阅读