scala - 更改给定 scala 函数中的类型
问题描述
我有一个功能
f : Point2D => Point2D = ???
我想将它转换为元组的函数:
(Double,Double) => (Double,Double)
我知道如何映射Point2D
到Tuple2
:
implicit def pointToTuple(p : Point2D) = (p.x,p.y)
但我不知道如何映射函数本身:
def convertFunction(f : Point2D => Point2D) : (Double,Double) => (Double,Double) = {
???
}
解决方案
你也需要tupleToPoint
,不只是pointToTuple
。
现在,只要你有这个,你可以这样做:
def convertFunction(f: Point2D => Point2D) =
tupleToPoint andThen f andThen pointToTuple
推荐阅读
- ruby - 根据列表的元素查找元素字典
- javascript - 使用 CORS 问题从 S3 获取图像
- node.js - locahost 在 React 组件编辑时返回 ERR_CONNECTION_REFUSED
- php - php中的正则表达式:多种可能性
- javascript - Odoo V12 JavaScript 框架
- time - 如何在工作流中将 Oozie 的协调器时间传递给 SLA 参数nominal_time?
- typescript - NUXT 在项目位置之外使用共享样式和脚本
- java - 如果我使用 Azure APIM,我应该如何在我的 REST API 中添加授权?
- oop - 如果我创建了一个 A 类的对象,但也存在同名的 A 的重复类,会发生什么?
- html - 在边缘浏览器数据列表下拉字体不同,但在 chrome 中它类似于网站字体