aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEduardo Julian2017-06-21 19:08:27 -0400
committerEduardo Julian2017-06-21 19:08:27 -0400
commit4a94a3dab463857fb1e881d4ab835ef5351ba9ac (patch)
treeda4e51ae8bc9063897979400e499fcb5141c5cb0
parent4c7760f5eff53d28a54360e1d29f67f620fee4ec (diff)
- Added Task type, as a promise that can fail.
Diffstat (limited to '')
-rw-r--r--stdlib/source/lux/concurrency/task.lux51
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))))