I've been exploring the folktale library and found a wealth of useful constructs. After using Tasks via control.async and data.task, I wanted to use an IO monad, but can't seem to find it. Given how rich folktale is, I am surprised and wondering whether I just am not seeing it.
Is there an IO monad in folktale?
In Haskell, the IO monad is provided by (and inherently bound to), the runtime. Folktale does not provide functional equivalents for runtime functions but otherwise Task and IO serve the same purpose. An IO action in Haskell can be asynchronous, so we can say that it is even more similar to Haskell's IO than, for example, the IO monad in monet.js.
One difference is that Task
provides error handling, while the IO monad doesn't.
You can program using Tasks in JS in the same manner that you program in Haskell using IO actions. You just need to define all impure runtime functions that you use using Tasks.
For example, take the function print
, (print :: Show a => a -> IO ()
) provided by the Haskell runtime which just prints its input and returns nothing. We can write a similar function in JS, using tasks. It will probably look something like this.
// Definition
const print = (input) => Task.task(r => {
console.log(String(input))
r.resolve(undefined)
})
// Usage
const main = Task.of("Hello world").chain(print)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With