Skip to content

Instantly share code, notes, and snippets.

@jimexist
Created September 3, 2023 14:18
Show Gist options
  • Save jimexist/0a0208208da123025328eac5b6be8015 to your computer and use it in GitHub Desktop.
Save jimexist/0a0208208da123025328eac5b6be8015 to your computer and use it in GitHub Desktop.
Example Kotlin
import java.math.BigDecimal
import java.time.YearMonth
enum class VehicleType {
SUV, Sedan
}
interface CarModel {
val vehicleType: VehicleType
}
interface Electric {
fun getNumOfMotors(): Int
fun getBattery(): BatteryInfo
}
enum class BatterType {
LiFeP, NiMnCo, Sodium
}
enum class Unit {
Watt, Ampere, Volt, Second, KiloWattHour
}
sealed interface UnitAmount {
val amount: BigDecimal
val unit: Unit
}
data class Energy(override val amount: BigDecimal) : UnitAmount {
override val unit: Unit get() = Unit.KiloWattHour
}
data class Voltage(override val amount: BigDecimal) : UnitAmount {
override val unit: Unit get() = Unit.Volt
}
infix fun BatterType.`πŸ”‹`(energy: Energy) = BatteryInfo(this, energy)
fun Number.kWh(): Energy = Energy(BigDecimal.valueOf(this.toDouble()))
data class BatteryInfo(val type: BatterType, val energy: Energy)
data class Vehicle(val model: CarModel, val yearMonth: YearMonth)
enum class TeslaTrim {
RearWheelDrive, LongRange, Performance
}
fun TeslaTrim.getNumOfMotors(): Int = when (this) {
TeslaTrim.RearWheelDrive -> 1
TeslaTrim.LongRange, TeslaTrim.Performance -> 2
}
sealed class TeslaModel(override val vehicleType: VehicleType) : CarModel, Electric
data class Model3(val trim: TeslaTrim) : TeslaModel(VehicleType.Sedan) {
override fun getNumOfMotors(): Int = trim.getNumOfMotors()
override fun getBattery() = when (trim) {
TeslaTrim.RearWheelDrive -> BatterType.LiFeP `πŸ”‹` 60.kWh()
TeslaTrim.LongRange, TeslaTrim.Performance -> BatterType.NiMnCo `πŸ”‹` 82.kWh()
}
}
data class ModelY(val trim: TeslaTrim) : TeslaModel(VehicleType.SUV) {
override fun getNumOfMotors(): Int = trim.getNumOfMotors()
override fun getBattery() = when (trim) {
TeslaTrim.RearWheelDrive -> BatterType.LiFeP `πŸ”‹` 60.kWh()
TeslaTrim.LongRange, TeslaTrim.Performance -> BatterType.NiMnCo `πŸ”‹` 82.kWh()
}
}
data class ModelS(val isPlaid: Boolean) : TeslaModel(VehicleType.Sedan) {
override fun getNumOfMotors(): Int = if (isPlaid) 3 else 2
override fun getBattery() = BatterType.NiMnCo `πŸ”‹` 100.kWh()
}
data class ModelX(val isPlaid: Boolean) : TeslaModel(VehicleType.SUV) {
override fun getNumOfMotors(): Int = if (isPlaid) 3 else 2
override fun getBattery() = BatterType.NiMnCo `πŸ”‹` 100.kWh()
}
val Vehicle.numOfCabins: Int
get() = when (model.vehicleType) {
VehicleType.SUV -> 2
VehicleType.Sedan -> 3
}
val modelY = ModelY(TeslaTrim.Performance)
val myCar = Vehicle(
model = modelY, yearMonth = YearMonth.of(2021, 3)
)
myCar.numOfCabins
modelY.getNumOfMotors()
// dsl builder example
sealed interface Parts
data class Wheel(val size: Int) : Parts
class Window : Parts
sealed class BillOfMaterials : Parts {
val parts: MutableList<Parts> = mutableListOf()
fun bodyInWhite(builder: BodyInWhite.() -> kotlin.Unit) {
}
fun underBody(builder: UnderBody.() -> kotlin.Unit) {
}
}
class Car : BillOfMaterials()
class BodyInWhite : BillOfMaterials() {
fun window(number: Int) {
(0 until number).forEach {
parts.add(Window())
}
}
}
class UnderBody : BillOfMaterials() {
fun wheels(number: Int, sizeBuilder: () -> Int) {
(0 until number).forEach {
parts.add(Wheel(sizeBuilder()))
}
}
}
fun carBom(bomBuilder: BillOfMaterials.() -> kotlin.Unit): BillOfMaterials {
val bom = Car()
bomBuilder(bom)
return bom
}
val carBom = carBom {
bodyInWhite {
window(6)
}
underBody {
wheels(4) {
19
}
}
}
class MyResource: AutoCloseable {
override fun close() {
TODO("Not yet implemented")
}
}
MyResource().use {
}
val value = mutableListOf<Int>().apply {
add(1)
add(2)
add(3)
removeAt(2)
}
carBom
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment