Skip to content

Instantly share code, notes, and snippets.

@colomboe
Last active December 6, 2019 08:42
Show Gist options
  • Save colomboe/0121f7d6ff4b6f0f0d69d28ed64b4166 to your computer and use it in GitHub Desktop.
Save colomboe/0121f7d6ff4b6f0f0d69d28ed64b4166 to your computer and use it in GitHub Desktop.
Possible followedBy operator
// 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
)
@JYCabello
Copy link

If you can find a clever way to make followedBy look like a pipe, you just created pipes in kotlin.

@colomboe
Copy link
Author

colomboe commented Dec 6, 2019

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