-
-
Notifications
You must be signed in to change notification settings - Fork 95
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.mean that calculates mean of values of a foldable #234
Conversation
Thank you for the pull request, @jdudek! It's exciting to receive a pull request from a new contributor. :)
It's not currently possible, but we'll be able to add this constraint in #232 thanks to introduction of We can simplify the implementation quite a bit. If we define var result = reduce_(
function(acc, n) { return {total: acc.total + n, count: acc.count + 1}; },
{total: 0, count: 0},
foldable
);
return result.count === 0 ? Nothing() : Just(result.total / result.count); |
Thanks for the reply. I thought about accumulating both sum and count in one fold, but it seemed like it would introduce unnecessary object allocations. So today I ran a simple benchmark and it turns out your solution is not only more concise, but also significantly faster (both with typechecking on and off)—assuming my microbenchmark makes any sense :) I also tested with much larger arrays and the results where similar. I’ve updated the implementation and added a test for the case with infinite arguments. |
I think this is still fair to say. Any objections from anyone to something like var result = reduce_(
function(acc, n) {
acc.total += n;
acc.count += 1;
return acc;
},
{total: 0, count: 0},
foldable
); ? |
//. > S.mean(S.Nothing()) | ||
//. S.Nothing() | ||
//. ``` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👾
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💥
I couldn’t resist running the benchmark with @kedashoe’s version and it actually is faster:
|
Thanks! I'd like to request one final change. Could you move the definition below that of |
✅ |
🌳 Very nice pull request. Thanks again! |
|
I’ve implemented the
mean
function, as it’s been suggested in #194.The tests are roughly based on
product
tests. One thing I couldn’t figure out is how to put a type constraint on the argument of typef
, i.e. how to specify the function accepts only foldables of finite numbers. I’ve noticed neitherproduct
norsum
enforce this restriction on their argument, only on their results (even though the comments suggest they do:product :: Foldable f => f FiniteNumber -> FiniteNumber
).Passing a foldable with an infinite number to
mean
does not typecheck, but it fails on the call tosum
, which makes the message a bit misleading.