<@U4L9S0L0J> commented on <@U4U3EK3FB>’s file <htt...
# getting-started
u
@beholder commented on @hvalls’s file https://kotlinlang.slack.com/files/U4U3EK3FB/F7C5RHFCH/how_can_refactor_this_to_not_to_duplicate_code_.kt:
Copy code
class Position(val x: Int, val y: Int) {

    fun right(units: Int) = whenPositive(units) { Position(x + units, y) }

    fun left(units: Int) = whenPositive(units) { Position(x - units, y) }

    fun up(units: Int) = whenPositive(units) { Position(x, y + units) }

    fun down(units: Int) = whenPositive(units) { Position(x, y - units) }

    private inline fun whenPositive(units: Int, function: (Int) -> Position) =
            if (units < 0) throw IllegalArgumentException() else function(units)
}