diff options
-rw-r--r-- | stdlib/source/lux/concurrency/task.lux | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/stdlib/source/lux/concurrency/task.lux b/stdlib/source/lux/concurrency/task.lux new file mode 100644 index 000000000..239572bff --- /dev/null +++ b/stdlib/source/lux/concurrency/task.lux @@ -0,0 +1,51 @@ +(;module: + lux + (lux (data ["R" result]) + (control functor + applicative + monad) + (concurrency ["P" promise]) + )) + +(type: #export (Task a) + (P;Promise (R;Result a))) + +(struct: #export _ (Functor Task) + (def: (map f fa) + (:: P;Functor<Promise> map + (function [fa'] + (case fa' + (#R;Error error) + (#R;Error error) + + (#R;Success a) + (#R;Success (f a)))) + fa))) + +(struct: #export _ (Applicative Task) + (def: functor Functor<Task>) + + (def: (wrap a) + (:: P;Applicative<Promise> wrap (#R;Success a))) + + (def: (apply ff fa) + (do P;Monad<Promise> + [ff' ff + fa' fa] + (wrap (do R;Monad<Result> + [f ff' + a fa'] + (wrap (f a))))))) + +(struct: #export _ (Monad Task) + (def: applicative Applicative<Task>) + + (def: (join mma) + (do P;Monad<Promise> + [mma' mma] + (case mma' + (#R;Error error) + (wrap (#R;Error error)) + + (#R;Success ma) + ma)))) |