Skip to content
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

Monoid.reduce has different signatures for objects and arrays #18

Closed
taras opened this issue Feb 26, 2018 · 4 comments
Closed

Monoid.reduce has different signatures for objects and arrays #18

taras opened this issue Feb 26, 2018 · 4 comments

Comments

@taras
Copy link
Member

taras commented Feb 26, 2018

Same monoid reduce will produce a different result for [1, 2, 3, 4] than { "1": 1, "2": 2, "3": 3, "4": 4 }. This makes it inconvenient when trying to use the same monoid for both arrays and objects. I would expect to be able to use the same monoid for both without having to make special accommodation for both types.

When reducing arrays, the append function for the array receives a value for a and b. Reducing objects, sends memo for a and b. This is caused by the difference in implementation of foldl for Arrays and Objects.

I believe that both foldl for Arrays and Objects should send data of same shape to the folding function. It should be other value or memo, not either.

Failing test: https://github.com/taras/funcadelic.js/blob/monoid-signature-failing-test/tests/funcadelic-test.js#L67

@cowboyd
Copy link
Member

cowboyd commented Feb 26, 2018

The "signature" for reduce is reduce(Monoid, list) where list is a list of monoids. Or, alternatively, Monoid.reduce(list). In both cases, what you have to reduce is a list. That's just how Monoids work.

So testcase Sum.reduce({"a": 1, "b": 2, "c": 3, "d": 4})) this is an invalid usage of the api. My intuition is to keep the moving parts as simple as possible because being hard-nosed about it thus far has yielded such amazing dividends.

To get the behavior of Sum.reduce(values({"a": 1, "b": 2, "c": 3, "d": 4})), perhaps the introduction of another typeclass in order to maximize composability without overloading existing typeclasses?

const Values = type(class Values {
  values(holder) {
    return this(holder).values(holder);
  }
);

Values.instance(Array, {
  values(array) { return array; }
});

Values.instance(Object, {
  values(object) { return Object.values(object); }
});

@taras
Copy link
Member Author

taras commented Feb 26, 2018

Yes, this would work nicely! I'll make a PR.

@cowboyd cowboyd closed this as completed Feb 26, 2018
@cowboyd cowboyd reopened this Feb 26, 2018
@taras
Copy link
Member Author

taras commented Feb 26, 2018

We can close this issue and PR. I'll make another and reference this one.

@taras taras closed this as completed Feb 26, 2018
@cowboyd
Copy link
Member

cowboyd commented Feb 26, 2018

Maybe we should try it out in libraries consuming it first, a-la Monad.

Also, I think that this highlights a weakness with error handling and messaging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants