-
Notifications
You must be signed in to change notification settings - Fork 376
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
Suggestion to add "CFoldable" #245
Comments
The problem with this is that we can't impose commutativity on the function (which is what would need to be done for this to work). Honestly, I think the best option is to agree unanimously that objects simple aren't stable enough to be considered |
I don't think an addition to the spec is necessary. Objects are foldable despite what the consensus in the linked Ramda thread is. It's easy to prove that Objects are foldable since you can easily implent
Why not? Keys are stored in insertion order? I find that to be quite intuitive behavior and it certainly meens that a law abiding |
you just need to sort keys first https://github.com/sanctuary-js/sanctuary-type-classes/blob/master/index.js#L902 |
@paldepind This is certainly not guaranteed https://stackoverflow.com/a/5525820/4022180, but @safareli's suggestion is the one that I have previously suggested as a best answer to this issue. However, given the lack of standard-enforced order, I do think that we can't (well, shouldn't) rely on the functor laws holding. Of course, a slightly lax instance is massively beneficial, so I think we should probably let it slide with a caveat - especially given that we can't (shouldn't) do equivalence with ordering anyway. The (lawful) practical incongruence is nil. |
Luckily for us that answer is wrong 😄 It's very unfortunate that misinformation is what people get from a Google search. SO is not a good source for such questions—looking directly at the spec is much better.
The standard is very clear about enforcing order. It's described right here. The order described is the one you get from So, the correct answer to the SO question you linked is actually yes, if you define an object like this: var obj = {};
obj.prop1 = "Foo";
obj.prop2 = "Bar";` The spec absolutely guarantees that you will get the keys in insertion order if you iterate over them with That is fantastic 😄 It means that if we want to we can make objects both foldables and functors. Here is a nice article that breaks down what the spec states and confirms what I'm saying above. |
@paldepind These appear to be ES2015-specific links, which would suggest that this isn't the case before ES2015, which means this is surely still unreliable behaviour for BC reasons? |
@i-am-tom It has been implemented like that in all engines for a long time. Way before it found its way into the spec. So there is no issue with relying on it in code. In fact the reason why it was added to the standard is that all engines had that behavior anyway and much code relied on it. So adding it to the spec was a win-win. |
@paldepind Oh, awesome! I had no idea :D Well, in that case, 👍 to your original message <3 |
Nice, thank you 😄 |
Should there perhaps be an extra laws only subclass of |
Abelian (semi)group is another common name in mathematics. |
I don't understand CFoldable though. Usually we'd just choose an order and move on. Can that not be done? |
@puffnfresh Are you asking why |
The problem with choosing the order, var obj = {a:1, b:2} to var obj = {b:2, a:1} However, with The |
@dmitriz you are not safe to make that refactor. How does CFoldable change that? |
@puffnfresh Of course, this only affects the |
@dmitriz I don't understand what is gained. You can refactor I have motivations for |
In line with the discussion in ramda/ramda#2160, I wonder about the consensus to add
CFoldable
aka Commutative-Foldable, wherereduce
only needs to be defined and obey the laws for the commutative accumulators.The text was updated successfully, but these errors were encountered: