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

Should product() return 0 for an empty array? #159

Open
stevensacks opened this issue May 26, 2016 · 4 comments
Open

Should product() return 0 for an empty array? #159

stevensacks opened this issue May 26, 2016 · 4 comments

Comments

@stevensacks
Copy link

stevensacks commented May 26, 2016

https://github.com/1-liners/1-liners/tree/master/documentation#product

Currently, it returns 1. Shouldn't an empty array product be 0 not 1? After all, nothing from nothing leaves nothing. 0 * 0 = 0.

It's easy to fix with a ternary check for length.

export default (array) => array.length ? array.reduce((a, b) => (a * b), 1) : 0;

https://www.youtube.com/watch?v=aj5DccgBYeM 😉

@tomek-he-him
Copy link
Member

While 0 is the “nothing” value for addition (anything + 0 == anything), I really feel 1 is the “nothing” value for multiplication (anything × 1 == anything). I’m sure there’s some academic papers backing up my feelings. 😄

@stoeffel
Copy link
Contributor

jop, 1 is the identity property for multiplication.

@stevensacks
Copy link
Author

Not sure what "jop" means. I was unaware that 1 is the identity property for multiplication. #TIL

@stoeffel
Copy link
Contributor

jop = yes 😸

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

3 participants