Road Not Taken by Robert Frost (in Kotlin)

Irfan Yas
2 min readOct 27, 2021
Photo by 김 대정 from Pexels
fun main() {
/*
* Two roads diverged in a yellow wood
* And sorry I could not travel both
* And be one traveler, long I stood
* And looked down one as far as I could
* To where it bent in the undergrowth
*
*/

val (roadOne, roadTwo) = arrayOf(Road(), Road())
val yellowWoodRoads = arrayOf(roadOne, roadTwo)
val me = Traveler()

me.roadToTravel = try {
yellowWoodRoads as Road
} catch(e: Exception) {
yellowWoodRoads[0].findUndergrowth()
yellowWoodRoads[0]
}


/*
* Then took the other, as just as fair
* And having perhaps the better claim,
* Because it was grassy and wanted wear;
* Thought as for that the passing there
* Had worn them really about the same
*
*/

val theOtherRoad =
if (me.roadToTravel == yellowWoodRoads[0]) yellowWoodRoads[1]
else yellowWoodRoads[0]

theOtherRoad
.takeIf { it.grassy >= (me.roadToTravel!!.grassy) }
?.let { me.roadToTravel = it }


/*
* And both that morning equally lay
* In leaves no step had trodden black.
* Oh, I kept the first for another day!
* Yet knowing how way leads on to way,
* I doubted if I should ever come back
*
*/

val date = System.currentTimeMillis()

me.roadToTravel = when(date) {
date -> roadTwo
else -> me.roadToTravel?.let {
roadOne
}
}


/*
* I shall be telling this with a sigh
* Somewhere ages and ages hence:
* Two roads diverged in a wood, and I-
* I took the one less traveled by,
* And that has made all the difference
*
*/

val somewhereAgesAndAges = Moment()
val lessTraveledRoad =
if(
somewhereAgesAndAges.roads[0].traveled <
somewhereAgesAndAges.roads[1].traveled
) somewhereAgesAndAges.roads[0]
else somewhereAgesAndAges.roads[1]

somewhereAgesAndAges.apply {
me.roadToTravel = lessTraveledRoad
isDifferent = true
}

}
/*
* Models
*
*/
data class Road(
val name: String = "",
var grassy: Int = 0,
var traveled: Int = 0
) {
fun findUndergrowth(): Int {
return Int.MAX_VALUE
}
}
data class Traveler(
val name: String = "",
var roadToTravel: Road? = null
)
data class Moment(
val time: Long = 0L,
val place: String = "Wood",
val roads: Array<Road> = arrayOf(Road(), Road()),
val me: Traveler = Traveler(),
var isDifferent: Boolean = false
)

--

--