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

Add S.sum and S.product #192

Merged
merged 1 commit into from
May 2, 2016
Merged

Conversation

svozza
Copy link
Member

@svozza svozza commented Apr 23, 2016

I had to move a few functions to the top of the file so that we could replace the Ramda versions used throughout the code.

function(a, b) { return a + b; });
S.add = add;

//# sum :: [FiniteNumber] -> FiniteNumber
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the type be as follows?

sum :: Foldable f => f FiniteNumber -> FiniteNumber

def('product',
{},
[$.Array($.FiniteNumber), $.FiniteNumber],
reduce(S.mult, 1));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/S[.]//

@svozza svozza force-pushed the sum-product branch 2 times, most recently from 23e868c to b08fd83 Compare April 24, 2016 13:32
@svozza
Copy link
Member Author

svozza commented Apr 24, 2016

Just realised I forgot to do git add with the tests!

@svozza svozza force-pushed the sum-product branch 2 times, most recently from 1e7cb9e to 2cb07b1 Compare April 24, 2016 13:49
{},
[$.Array($.FiniteNumber), $.FiniteNumber],
reduce(function(a, b) { return a + b; }, 0));

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks as though each of these functions can now be moved alongside its description. sum is referenced in the definition of meld, but will have been defined by the time the body of that function is executed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the tests fail if I move them and do something like

var sum = S.sum =

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, because meld is applied in the definition of mapMaybe. Let's change that definition from meld([R.map, justs]) to something along these lines:

function(f, xs) { return justs(R.map(f, xs)); }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidchambers
Copy link
Member

Let's also test these:

S.sum(S.Nothing())
S.sum(S.Just(42))
S.sum(S.Left('XXX'))
S.sum(S.Right(42))

The last two won't work until we resolve #119, so we could put these assertions in it.skip blocks.

@svozza
Copy link
Member Author

svozza commented Apr 24, 2016

I've updated it to use the Foldable type constraint. I'm not happy with the error messages for the Infinity cases. The product message now leaks that it is implemented in terms of mult and sum gives an error based on the return value so it actually allows the disallowed computation occur.

@davidchambers
Copy link
Member

The product message now leaks that it is implemented in terms of mult and sum gives an error based on the return value so it actually allows the disallowed computation occur.

I suggest replacing mult with function(a, b) { return a * b; } so we refer to the right function.

Complaining about the return value isn't ideal, but it's the best we can do until sanctuary-def permits us to express Foldable f => f FiniteNumber.

@svozza
Copy link
Member Author

svozza commented Apr 24, 2016

Yeah, it's not great but I guess there's nothing we can do.

return foldable.reduce(f, initial);
}
});
S.reduce = reduce;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can revert this change, I believe.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -2378,7 +2379,8 @@
//. > S.reduce((xs, x) => [x].concat(xs), [], [1, 2, 3, 4, 5])
//. [5, 4, 3, 2, 1]
//. ```
S.reduce =
// reduce :: Foldable f => (a -> b -> a) -> a -> f b -> a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is now redundant.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops!

@svozza
Copy link
Member Author

svozza commented Apr 29, 2016

Anything else left on this?

@davidchambers
Copy link
Member

Anything else left on this?

This looks great, Stefano. This has motivated me to resolve #119. I plan to do so tomorrow. Once that pull request is merged, you'll be able to rebase this one and remove the .skips. :)

@davidchambers davidchambers mentioned this pull request Apr 30, 2016
12 tasks
eq(S.product(S.Just(42)), 42);
});

it.skip('can be applied to Eithers', function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove the .skip now thanks to #204. :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@svozza svozza force-pushed the sum-product branch 2 times, most recently from 9982725 to 6345074 Compare May 2, 2016 16:54
def('sum',
{f: [Foldable]},
[f, $.FiniteNumber],
reduce_(function(a, b) { return a + b; }, 0));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might this be better?

reduce(function(a) { return function(b) { return a + b; }; }, 0)

reduce_ will curry the provided function, but we could remove a step by defining a curried function.

We needn't worry about this, though. Once we can express f FiniteNumber in the type system we'll be able to use S.add in this definition without references to that function appearing in error messages. :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidchambers
Copy link
Member

🌳 Lovely patch, Stefano!

@davidchambers davidchambers merged commit e27c691 into sanctuary-js:master May 2, 2016
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

Successfully merging this pull request may close these issues.

2 participants