-
Notifications
You must be signed in to change notification settings - Fork 6
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
Functor broken #1
Comments
This is a tricky one: at the moment, promises with multiple values will yield them as separate arguments to map instead of together as an array. This was purely because there is no elegant destructuring syntax in node yet. However, you're right that this breaks identity as defined. I can change the behaviour of map to concatenate promise values into an array that is returned as a single argument during map. |
You shouldn't make map :: f a -> (a -> b) -> f b (i.e. you should be able to pass any function that takes an The best option I can see is making |
I've been experimenting with this a little more; an issue is what it means to concat :: Promise a -> Promise b -> Promise [a b] But this means that, strictly, speaking, p.concat(p2) //=> Promise [a b]
p.concat(p2).concat(p3) //=> Promise [[a b] c]
p.concat(p2).concat(p3).concat(p4) //=> Promise [[[a b] c] d] While consistent, my original use case was to have a way to combine multiple promises into one that only yields when all values are resolved. You would then need to make those values available and this interface was very nice to use: promisedHttpRequest.concat(anotherPromisedHttpRequest).map(function (response, anotherResponse) {
// Do something with the two response bodies.
}); As you say, this violates the Functor interface so I wanted to change it to be compliant: promisedHttpRequest.concat(anotherPromisedHttpRequest).map(function (responses) {
var response = responses[0],
anotherResponse = responses[1];
// Do something with the two response bodies.
}); However, the current definition of promisedHttpRequest.concat(anotherPromisedHttpRequest).concat(yetAnotherPromisedHttpRequest).map(function (responses) {
var response = responses[0][0],
anotherResponse = responses[0][1],
yetAnotherResponse = response[1];
// Do something with the two response bodies.
}); Perhaps defining the result of |
Ah, concat :: Promise a -> Promise a -> Promise a |
So it's only possible to concat things that can themselves be concatenated (e.g. arrays, etc.)? |
@mudge that's one solution, or you could say that Promises always represent multiple values (of the same type) and then concat them internally. So Promise could be something like an async Array, I guess. |
I like the idea of Promises as a kind of Eventual type: they might not have |
I've just pushed some changes to the interface to bring it back in line with the specification, please feel free to review and let me know if there are any issues. I added a test explicitly for the case mentioned (identity of a semigroup): https://github.com/mudge/pacta/blob/master/test/pacta_test.js#L131-L142 |
u.map(function(a) { return a; }))
is equivalent tou
(identity)Breaks for this example:
The text was updated successfully, but these errors were encountered: