【发布时间】:2020-11-19 07:14:51
【问题描述】:
我正在尝试删除代码中的冗余。我想我可以用高阶函数来做到这一点。
我要做的是将公共位分解到f3中,然后将f1和f2之间不同的位作为传递给f3的参数。
object Example extends App {
case class action(name: String, age: Int) {
def setName(new_name: String): action = this.copy(name = new_name)
def setAge(new_age: Int): action = this.copy(age = new_age)
}
def f1(x: action, increment: Int) = {
// big block of code which does a....
// a single line in the block calling
val older_person = x setAge (x.age + increment)
// big block of code which does b....
}
def f2(x: action, new_name: String) = {
// big block of code which does a....
// a single line in the block calling
val new_name_person = x setName new_name
// big block of code which does b....
}
/* Now as there is clearly a redundancy, which can be solved by higher order functions.
I want to combine f1 and f2 into a single function. The function will take in the action, the value, and the
function to apply. It will then call the relevant function inside the method.
*/
def f3[T](x: action)(f: T => action)(value: T) = {
// big block of code which does a....
// call x.f(value)
val new_action = ???
// big block of code which does b....
}
// then in my code I can call like this:
// f3(x)(setAge)(100)
// f3(x)(setName("new_name")
}
我很困惑的是如何传入一个作为案例类中的方法的函数?有没有一种优雅的方式来做到这一点?
【问题讨论】:
标签: scala higher-order-functions abstraction case-class