-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dispatches to ap
method of monad if available
#3
Conversation
I test it, but it doesn't seem to work (fixing a small typo). return this.x.ap ? this.x.ap(a) : this.chain(f => a.map(f)) And changing package.json to: My code: const {Free, liftF} = require("./free")
const Task = require("data.task")
const daggy = require("daggy")
const Shout = daggy.tagged("text", "ms")
const shout = (text, ms) => liftF(Shout(text, ms))
const par3 = x => y => z => [x, y, z]
const app = shout("Last", 1000).map(par3).ap(shout("Second", 500)).ap(shout("First", 100))
const shoutToTask = ({text, ms}) => new Task((rej, res) => {
setTimeout(()=>{
console.log(text + "!!!!")
res("ok")
},ms)
})
console.log("----- Using Free -----");
app.foldMap(shoutToTask, Task.of).fork(console.log,console.log)
// Now using Task directly
const shout2 = (text, ms) => new Task((rej, res) => {
setTimeout(()=>{
console.log(text + "!!!!")
res("ok")
},ms)
})
const app2 = shout2("Last", 1000).map(par3).ap(shout2("Second", 500)).ap(shout2("First", 100))
setTimeout(()=>{
console.log("")
console.log("----- Using Task directly-----")
app2.fork(console.log,console.log)
}, 2500) |
Shoot yeah, I wasn't thinking earlier. I think the implementation is just: Free.prototype.ap = function(a) {
return this.cata({
Impure: (x, g) => Impure(x, y => g(y).ap(a)),
Pure: f => a.map(f)
})
} However, this doesn't quite work. I think the order we're witnessing is due to the nature of Free Monads being run all the way through first, then interpreted later. We might have to use |
I should say, it does "work", but the order is backwards as you've noticed. |
I think |
Here is Free Applicative from fantasy-free if that's helpfull. |
@DrBoolean if you have some good resources about this topic it will be nice if you add it to README |
Superceded by #5 |
Hi guys I have ported free-concurrent written by @srijs which is combination of free applicative functor and free monad.
|
Perfect! |
Great! |
Could you guys check if my test case for checking concurrency is correct? |
Looks good to me, @safareli |
Thanks! |
As it turns out when structure is monad applicative instance should behave as if it's derived from monad, so making free concurrent when I was thinking on that issue and camte to this and I would love to hear your thoughts. |
I don't know if you would prefer to also have
chain
rewritten to dispatch when available?