diff options
author | Eduardo Julian | 2017-11-16 01:45:40 -0400 |
---|---|---|
committer | Eduardo Julian | 2017-11-16 01:45:40 -0400 |
commit | 4abfd5413b5a7aa540d7c06b387e3426ff5c532c (patch) | |
tree | 7f5ba092e7493ce5642e0dc4069913825ec97494 /stdlib/source | |
parent | 37b94ba49afb272c63ec66e42d56b8fba35cea9f (diff) |
- Added "Process" type for IO operations that can fail.
Diffstat (limited to 'stdlib/source')
-rw-r--r-- | stdlib/source/lux/io.lux | 49 |
1 files changed, 39 insertions, 10 deletions
diff --git a/stdlib/source/lux/io.lux b/stdlib/source/lux/io.lux index 8aefdf0a5..d3e08169e 100644 --- a/stdlib/source/lux/io.lux +++ b/stdlib/source/lux/io.lux @@ -1,16 +1,15 @@ (;module: {#;doc "A method for abstracting I/O and effectful computations to make it safe while writing pure functional code."} lux - (lux (control ["F" functor] - ["A" applicative] - ["M" monad #+ do Monad]) - (data (coll [list])))) + (lux (control [functor #+ Functor] + [applicative #+ Applicative] + [monad #+ do Monad]) + (data ["e" error #+ Error] + (coll [list])))) -## [Types] (type: #export (IO a) {#;doc "A type that represents synchronous, effectful computations that may interact with the outside world."} (-> Void a)) -## [Syntax] (macro: #export (io tokens state) {#;doc (doc "Delays the evaluation of an expression, by wrapping it in an IO 'thunk'." "Great for wrapping effectful computations (which will not be performed until the IO is \"run\")." @@ -25,12 +24,11 @@ _ (#;Left "Wrong syntax for io"))) -## [Structures] -(struct: #export _ (F;Functor IO) +(struct: #export _ (Functor IO) (def: (map f ma) (io (f (ma (:! Void [])))))) -(struct: #export _ (A;Applicative IO) +(struct: #export _ (Applicative IO) (def: functor Functor<IO>) (def: (wrap x) @@ -45,8 +43,39 @@ (def: (join mma) (io ((mma (:! Void [])) (:! Void []))))) -## [Functions] (def: #export (run action) {#;doc "A way to execute IO computations and perform their side-effects."} (All [a] (-> (IO a) a)) (action (:! Void []))) + +## Process +(type: #export (Process a) + (IO (Error a))) + +(struct: #export _ (Functor Process) + (def: (map f ma) + (io (:: e;Functor<Error> map f (run ma))))) + +(struct: #export _ (Applicative Process) + (def: functor Functor<Process>) + + (def: (wrap x) + (io (:: e;Applicative<Error> wrap x))) + + (def: (apply ff fa) + (io (:: e;Applicative<Error> apply (run ff) (run fa))))) + +(struct: #export _ (Monad Process) + (def: applicative Applicative<Process>) + + (def: (join mma) + (case (run mma) + (#e;Success ma) + ma + + (#e;Error error) + (io (#e;Error error))))) + +(def: #export (fail error) + (All [a] (-> Text (Process a))) + (io (#e;Error error))) |