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 main() { | |
println(processRequest(1)) | |
println(processRequest(10)) | |
println(processRequest(-1)) | |
} | |
// 2 | |
// 20 | |
// -1 |
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 processRequest(input: Int): Int { | |
val initial = State.RequestReceived(input) | |
val result = transitions.fold(initial) | |
return if (result.validationResult) { | |
result.previousState.response | |
} else { | |
-1 | |
} | |
} |
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
val transitions = listOf( | |
ValidateRequest(), | |
ProcessRequest(), | |
ValidateResponse() | |
) |
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 List<Transition<State, State>>.fold(initial: State.RequestReceived): State.ResponseValidated { | |
var currentState: State = initial | |
forEach { | |
currentState = it.invoke(currentState) | |
} | |
if (currentState is State.ResponseValidated) { | |
return currentState as State.ResponseValidated | |
} else { | |
throw IllegalStateException("currentState is not of State.ResponseValidated type") | |
} |
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
abstract class Transition<InputState: State, OutputState: State> { | |
abstract fun invoke(state: InputState) : OutputState | |
} | |
class ValidateRequest: Transition<State.RequestReceived, State.RequestValidated>() { | |
override fun invoke(state: State.RequestReceived): State.RequestValidated { | |
// let's just check something here | |
return State.RequestValidated(state.request > 0, state) | |
} | |
} |
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
sealed class State(val previousState: State?) { | |
class RequestReceived(val request: Int): State(null) | |
class RequestValidated(val validationResult: Boolean, previousState: RequestReceived): State(previousState) | |
class RequestProcessed(val response: Int, previousState: RequestValidated): State(previousState) | |
class ResponseValidated(val validationResult: Boolean, previousState: RequestProcessed): State(previousState) | |
} |
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
val teaPotTurnedOn = if (turnOnTheTeaPot) { | |
// region Turning teapot on | |
teaPotService.turnOn() | |
true | |
// endregion | |
} else { | |
// region Turning teapot off | |
teaPotService.turnOff() | |
false | |
// endregion |
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 turnOnTheTeaPot(teapotService: TeaPotService) : Boolean { | |
teaPotService.turnOn() | |
return true | |
} | |
fun turnOffTheTeaPot(teapotService: TeaPotService) : Boolean { | |
teaPotService.turnOff() | |
return false | |
} |
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
if (turnOnTheTeaPot) { | |
// Turning teapot on | |
teaPotService.turnOn() | |
teaPotTurnedOn = true | |
} else { | |
// Turning teapot off | |
teaPotService.turnOff() | |
teaPotTurnedOn = false | |
} |
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 TeaPotService { | |
fun turnOn() { | |
//do something | |
} | |
fun turnOff() { | |
//do something | |
} | |
} | |
fun main() { |
NewerOlder