diff options
author | Eduardo Julian | 2017-06-21 19:08:27 -0400 |
---|---|---|
committer | Eduardo Julian | 2017-06-21 19:08:27 -0400 |
commit | 4a94a3dab463857fb1e881d4ab835ef5351ba9ac (patch) | |
tree | da4e51ae8bc9063897979400e499fcb5141c5cb0 /stdlib | |
parent | 4c7760f5eff53d28a54360e1d29f67f620fee4ec (diff) |
- Added Task type, as a promise that can fail.
Diffstat (limited to 'stdlib')
-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)))) |