Last active
December 6, 2019 08:42
-
-
Save colomboe/0121f7d6ff4b6f0f0d69d28ed64b4166 to your computer and use it in GitHub Desktop.
Possible followedBy operator
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
// followedBy operator definition | |
@JvmName("pureFollowedByAny") | |
infix fun <A, B, C> ((A) -> B).followedBy(f: (B) -> C): (A) -> C = { a -> f(this(a)) } | |
@JvmName("effFollowedByPure") | |
infix fun <R, E, A, B, C> ((A) -> KIO<R, E, B>).followedBy(f: (B) -> C): (A) -> KIO<R, E, C> = { a -> this(a).map(f) } | |
@JvmName("effFollowedByEff") | |
infix fun <R, E, A, B, C> ((A) -> KIO<R, E, B>).followedBy(f: (B) -> KIO<R, E, C>): (A) -> KIO<R, E, C> = { a -> this(a).flatMap(f) } | |
// Example usage | |
class ApiRequest | |
class ApiResponse | |
object BadRequest | |
inline class Author(val name: String) | |
data class Book(val title: String) | |
fun authorFromApiRequest(request: ApiRequest): IO<BadRequest, Author> = TODO() | |
fun retrieveBooksForAuthor(author: Author): UIO<List<Book>> = TODO() | |
fun filterOnlyCoAuthoredBooks(bs: List<Book>): List<Book> = TODO() | |
fun sortByDate(bs: List<Book>): List<Book> = TODO() | |
fun adaptForApiResponse(bs: IO<BadRequest, List<Book>>): ApiResponse = TODO() | |
val useCase: (ApiRequest) -> ApiResponse = ( | |
::authorFromApiRequest | |
followedBy ::retrieveBooksForAuthor | |
followedBy ::filterOnlyCoAuthoredBooks | |
followedBy ::sortByDate | |
followedBy ::adaptForApiResponse | |
) | |
Yes that's what I'm trying to achieve!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you can find a clever way to make followedBy look like a pipe, you just created pipes in kotlin.