如何简化kotlin函数的默认实现

时间:2017-12-09 03:24:09

标签: kotlin

  

以下代码,我总觉得有点啰嗦。

//judging employee's salary level,  and set a default implemention of the function:whether it > amount
fun paidMore(amount: Int, employee: Employee,
     predicate: (Int, Employee) -> Boolean = { amount, employee -> employee.salary > amount }
): Boolean = predicate(amount, employee)
  

我想我可以像这样简化代码:

fun paidMore(amount: Int, employee: Employee, 
      predicate: (Int, Employee) -> Boolean = employee.salary > amount
): Boolean = predicate(amount, employee)

当然,我是自以为是的。它无法运行。

//This code is treated as a Boolean expression, not a function.
employee.salary > amount
  

为什么呢?我该怎么做才能简化代码?

1 个答案:

答案 0 :(得分:0)

  

哦,是的。我简化了一些代码。

fun paidMore(amount: Int, employee: Employee,
                     predicate: (Int, Employee) -> Boolean = { _, _ -> employee.salary > amount }
        ): Boolean = predicate(amount, employee)
  

现在,我再次简化了代码

import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.describe
import org.jetbrains.spek.api.dsl.it
import kotlin.test.assertFalse
import kotlin.test.assertTrue

object ClosureSpec : Spek({
    describe("Test, add a default implementation to the function") {
        data class Employee(val name: String, val salary: Int) //the model used to test

        fun paidMore(amount: Int, employee: Employee,
                     predicate: (Int) -> Boolean = { _ -> employee.salary > amount }
        ): Boolean = predicate(amount)

        it("judging employee's salary level") {
            fun isHighPaid(employee: Employee): Boolean = paidMore(100, employee)
            fun isHighPaid2(employee: Employee): Boolean = paidMore(100, employee) {amount ->
                employee.salary > amount * 2
            }

            val employee = Employee("yuri", 101)
            assertTrue(isHighPaid(employee))
            assertFalse(isHighPaid2(employee))

            assertTrue(isHighPaid2(Employee("peter", 201)))
            assertFalse(isHighPaid2(Employee("jerry", 200)))
        }
    }

})
  

或者

import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.describe
import org.jetbrains.spek.api.dsl.it
import kotlin.test.assertFalse
import kotlin.test.assertTrue

object ClosureSpec : Spek({
    describe("Test, add a default implementation to the function") {
        data class Employee(val name: String, val salary: Int) //the model used to test

        //judging employee's salary level
        fun Employee.paidMore(amount: Int, predicate: (Int) -> Boolean = { 
            this.salary > it 
        }): Boolean = predicate(amount)

        on("use the default implementation") {
            val amount = 100
            fun Employee.isHighPaid() = paidMore(amount)

            it("should all be passed") {
                assertTrue(Employee("Alice", amount + 1).isHighPaid())
                assertFalse(Employee("Alice", amount - 1).isHighPaid())
            }
        }

        on("override the default implementation") {
            val amount = 100
            fun Employee.isHighPaid() = paidMore(amount) {
                this.salary > it * 2
            }

            it("should all be passed") {
                assertTrue(Employee("Bob", amount * 2 + 1).isHighPaid())
                assertFalse(Employee("Bob", amount * 2 - 1).isHighPaid())
            }
        }
    }
})
  

哪一个更好?别的什么?期待您的好主意

相关问题