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 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
| fun main() { | |
| println(processRequest(1)) | |
| println(processRequest(10)) | |
| println(processRequest(-1)) | |
| } | |
| // 2 | |
| // 20 | |
| // -1 |
OlderNewer