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

后端 未结 2 1025
甜味超标
甜味超标 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:12

    You can use simply 'a compose b'.

    scala> val c = a compose b
    c: String => java.lang.String = <function1>
    
    0 讨论(0)
  • 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 = <function1>
    
    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.

    0 讨论(0)
提交回复
热议问题