Last active
August 22, 2022 19:13
-
-
Save marenovakovic/7bd185556541c034ed3fb5ee15221bcf to your computer and use it in GitHub Desktop.
This file contains 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 Request(val endpoint: String) | |
data class Response(val body: String) | |
fun interface Processor : (Request) -> Response | |
val LoggingProcessor = { processor: Processor -> | |
Processor { request: Request -> processor(request).also(::println) } | |
} | |
private val cache = mutableMapOf<Request, Response>() | |
val CachingProcessor = { processor: Processor -> | |
Processor { request: Request -> | |
cache[request] ?: processor(request).also { cache[request] = it } | |
} | |
} | |
val RequestProcessor = Processor { request -> | |
Response("Response: ${request.endpoint}") | |
} | |
fun main() { | |
val cachingProcessor = CachingProcessor(LoggingProcessor(RequestProcessor)) | |
val request = Request("https://endpoint") | |
cachingProcessor(request) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment