All Projects → toby-allsopp → coroutine_monad

toby-allsopp / coroutine_monad

Licence: MIT License
Using coroutines as sugar for composing monadic operations

Programming Languages

C++
36643 projects - #6 most used programming language

Projects that are alternatives of or similar to coroutine monad

Retrofit2-Flow-Call-Adapter
A Retrofit 2 adapter for Kotlin Flows.
Stars: ✭ 41 (-30.51%)
Mutual labels:  coroutines
jda-ktx
Collection of useful Kotlin extensions for JDA
Stars: ✭ 49 (-16.95%)
Mutual labels:  coroutines
AsyncUtils
A set of utilities for Asynchronous programming in Unity.
Stars: ✭ 15 (-74.58%)
Mutual labels:  coroutines
JustJava-Android
JustJava is a mock food ordering and delivery application for a coffee shop.
Stars: ✭ 59 (+0%)
Mutual labels:  coroutines
kit
C++11 libs: await, channels, reactive/signals, timelines, alarms, logging, args, etc.
Stars: ✭ 21 (-64.41%)
Mutual labels:  coroutines
fluid-mongo
Kotlin coroutine support for MongoDB built on top of the official Reactive Streams Java Driver
Stars: ✭ 27 (-54.24%)
Mutual labels:  coroutines
Paging-3-Sample
This app is created as a sample app which loads movies from Tmdb api and uses Paging 3 library to show it in a Recycler view.
Stars: ✭ 96 (+62.71%)
Mutual labels:  coroutines
result
A lightweight C++11-compatible error-handling mechanism
Stars: ✭ 121 (+105.08%)
Mutual labels:  expected
Saga
Saga pattern implementation in Kotlin build in top of Kotlin's Coroutines.
Stars: ✭ 24 (-59.32%)
Mutual labels:  coroutines
DeezerClone
This Application using Dagger Hilt, Coroutines, Flow, Jetpack (Room, ViewModel, LiveData),Navigation based on MVVM architecture.
Stars: ✭ 81 (+37.29%)
Mutual labels:  coroutines
kotlinx-sockets
Kotlinx coroutines sockets
Stars: ✭ 54 (-8.47%)
Mutual labels:  coroutines
GitHubApplication
GitHubApplication 📱 is an Android application built to demonstrate the use of modern Android development tools - (Kotlin, Coroutines, Hilt, LiveData, View binding, Data Store, Architecture components, MVVM, Room, Retrofit, Navigation).
Stars: ✭ 11 (-81.36%)
Mutual labels:  coroutines
Kotlin-Coroutine-Flow
Search engine functionality using Kotlin Coroutines and Flow
Stars: ✭ 25 (-57.63%)
Mutual labels:  coroutines
koru
Simple coroutine wrappers for Kotlin Native. Generated from annotations. Compatible with RxSwift, Combine etc.
Stars: ✭ 141 (+138.98%)
Mutual labels:  coroutines
insta-bot
Instagram automation library to perform likes, comments, follow, story viewing and much more.
Stars: ✭ 20 (-66.1%)
Mutual labels:  coroutines
moko-errors
Automated exceptions handler for mobile (android & ios) Kotlin Multiplatform development.
Stars: ✭ 45 (-23.73%)
Mutual labels:  coroutines
eff.lua
ONE-SHOT Algebraic Effects for Lua!
Stars: ✭ 32 (-45.76%)
Mutual labels:  coroutines
Coffeegram
Android app using Jetpack Compose together with StateFlow and MVI
Stars: ✭ 155 (+162.71%)
Mutual labels:  coroutines
WanAndroidMVVM
WanAndroid 客户端,采用 Kotlin 语言编写,项目使用 JetPack-MVVM 架构,采用 Retrofit + Coroutines + Coil 等开源框架开发。
Stars: ✭ 21 (-64.41%)
Mutual labels:  coroutines
Jacob
A lightweight library to provide coroutines in Java
Stars: ✭ 14 (-76.27%)
Mutual labels:  coroutines

Using coroutines for monadic composition

This repository shows how coroutines can be used to compose operations that return monadic types.

One example of a monadic type is one that either contain a result to be used in further computation or an indication of error, such as std::optional<T>.

The goal is to make such composition as easy as Haskell's do notation.

Requirements

The implementation requires an implementation of the Coroutines TS (https://isocpp.org/files/papers/N4680.pdf). As of March 10, 2018, the only such implementation is Clang 5 or later with a corresponding libc++.

In particular, the MSVC implementation converts the object returned by get_return_object to the return type of the coroutine immediately rather than waiting until the coroutine returns to its caller.

Limitations

Only certain kinds of monads can be made to work with the approach in this repository. Because coroutines can only move forwards, only monads that invoke their continuations at most once can be supported.

Monads that are like Maybe and Either are supported fully. These monads' bind operation invokes, or not, its continuation directly inside bind, so there is no possibility of it being invoked more than once.

Monads like List cannot be supported. These monads explicitly call their continuations multiple times inside bind.

Then there are monads like State that store their continuations, building up a combined continuation that is invoked later, after bind has returned, under the control of some other code. This kind of monad is supported with the caveat that the resulting continuation may only be invoked at most once.

Expected

See test_expected.cpp for three different ways to compose a sequence of calls to functions returning expected values.

The expected in question is that from viboes' std-make repository. This definition knows nothing about coroutines; all of the coroutine machinery is in monad_promise.h and test_expected.cpp.

Here's what one can write in Haskell:

data MyError = MyError Int

f1 :: Either MyError Int
f1 = Right 7

f2 :: Int -> Either MyError Float
f2 x = Right (fromIntegral x * 2.0)

f3 :: Int -> Float -> Either MyError Int
f3 x y = Left (MyError 42)

test :: Either MyError Int
test =
  do x <- f1
     y <- f2 x
     z <- f3 x y
     return z

With the code in the repository, you can write it like this in C++:

struct error {
  int code;
};

expected<int, error> f1() { return 7; }
expected<double, error> f2(int x) { return 2.0 * x; }
expected<int, error> f3(int x, double y) { return error{42}; }

auto test_expected_coroutine() {
  return []() -> expected<int, error> {
    auto x = co_await f1();
    auto y = co_await f2(x);
    auto z = co_await f3(x, y);
    co_return z;
  }();
}

State

An implementation of the State monad can be found in state.h. Examples of its usage, both with and without coroutines, are in test_state.cpp.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].