Function composition of methods, functions, and partially applied functions in Scala

后端 未结 2 1024
甜味超标
甜味超标 2021-02-12 23:57

Somewhat similar to Stack Overflow question Compose and andThen methods, I\'ve been working through Twitter\'s Scala School tutorial and quickly ran into the same probl

2条回答
  •  情话喂你
    2021-02-13 00:19

    a(_).compose(b(_)) expands to x => { a(x).compose(y => b(y) }. Hence the error. What you want is (x => a(x)).compose(y => b(y)). Adding a pair of parentheses fixes this.

    scala> (a(_)).compose(b(_: String))
    res56: String => java.lang.String = 
    
    scala> res56("hello")
    res57: java.lang.String = helloahemumm
    

    But since a and b are functions, you can avoid all this cruft and simply do a compose b.

提交回复
热议问题