def couple_train(train, train_set):
return train + (train_set,)
def decouple_train(train):
return train[:-1]
def train_length(train):
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Train: | |
def __init__(self, composition): | |
self.composition = composition | |
def couple(self, train_set): | |
self.composition.append(train_set) | |
def decouple(self): | |
self.composition.pop() |
class Train:
def __init__(self, composition):
self.composition = composition
def couple(self, train_set):
self.composition.append(train_set)
def decouple(self):
self.composition.pop()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun Int.toWeight(): Weight = Weight(this) | |
fun easyConstruction() = 1000.toWeight() // Weight(1000) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@JvmInline | |
value class Weight(val grams: Int) { | |
operator fun plus(other: Weight) = Weight(grams + other.grams) | |
} | |
fun operatorStuff(): Weight = Weight(1000) + Weight(500) // Weight(1500) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum class Bean { ARABICA, ROBUSTA, BLEND } | |
data class Coffee(val type: Bean, val intensity: Int, val weight: Int) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum class Bean { ARABICA, ROBUSTA, BLEND } | |
enum class Intensity { ONE, TWO, THREE, FOUR, FIVE } | |
data class Weight(val grams: Int) { /* implementation details */ } | |
data class Coffee(val type: Bean, val intensity: Intensity, val weight: Weight) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class Weight(val grams: Int) { | |
init { | |
require(grams >= 0) { "Weight should be a positive number" } | |
} | |
fun toKilograms() = grams / 1000.0 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class Weight(val grams: Int) { | |
init { | |
require(grams >= 0) { "Weight should be a positive number" } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Checks validity of weight | |
fun inputA(weight: Int) { | |
require(weight >= 0) { "Weight should be a positive number" } | |
// Logic with weight | |
} | |
// Also checks the validity of weight: duplication! | |
fun inputB(weight: Int) { | |
// Because the checks are implemented in different ways it's harder to spot. | |
if (weight < 0) throw IllegalArgumentException("Weight should be a positive number") |
NewerOlder