【问题标题】:Can't `compose` and method and a function不能`compose`和方法和函数
【发布时间】:2014-10-06 09:44:30
【问题描述】:

我定义了一个方法和一个函数:

def print(str:String) = println
val intToString = (n:Int) => n.toString

现在我想创作它们。

我的问题是,为什么两者都没有:

print(_) compose intToString
print(_:String) compose intToString

编译?

但是:

(print(_)) compose intToString
(print _ ) compose intToString

编译?

【问题讨论】:

  • 别忘了你可以随时写intToString andThen print
  • @Huw composeandThen 不同。 f _ compose g 等价于f(g(x))f _ andThen g 等价于g(f(x))
  • 嗯,是的,这就是我的意思;通过颠倒顺序,方法出现在可以自动转换为函数的位置。

标签: function scala methods composition


【解决方案1】:

我认为

print(_) compose intToString

去糖

x => (print(x) compose intToString)

(print(_)) compose intToString

去糖

(x => print(x)) compose intToString

【讨论】:

  • 顺便说一句,如果有人知道语言规范的哪一部分描述了占位符表达式的关联方式,我很乐意阅读。我只能通过实验来发现它......
猜你喜欢
  • 1970-01-01
  • 2019-12-20
  • 2021-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-01
  • 2012-09-20
相关资源
最近更新 更多