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

feat: add Product and ProductBy functions #566

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

JohnDevitt
Copy link

@JohnDevitt JohnDevitt commented Dec 3, 2024

Currently playing around with Go on Advent of Code and found that a Product or ProductBy function would be useful, and fit in well with the preexisting Sum and SumBy functions

Used as such:

list := []int{1, 2, 3, 4, 5}
product := lo.Product(list)
// 120
strings := []string{"foo", "bar"}
product := lo.ProductBy(strings, func(item string) int {
    return len(item)
})
// 9

Hope its useful!

@JohnDevitt JohnDevitt changed the title feat: add Product and ProductBy functions feat: add Product and ProductBy functions Dec 3, 2024
@artemnesterenko
Copy link

I think "Multiply" could be a better name, because "Product" can be related to Cartesian Product which is also a nice-to-have function

@JohnDevitt
Copy link
Author

I think "Multiply" could be a better name, because "Product" can be related to Cartesian Product which is also a nice-to-have function

I'd argue Product is the proper name for the actual functionality here. Multiply in mind in the product of 2 numbers, rather than n numbers. Even in the lodash documentation their multiply function says it "Returns the product" (Possibly a bad naming choice they don't want to introduce a breaking change for)

How would you feel about Product for these functions and a follow-up PR with a CartesianProduct or CrossJoin (I believe this is used in other libraries) function? Both seem a bit clearer to me and better describe their respective operation

@JohnDevitt
Copy link
Author

I think "Multiply" could be a better name, because "Product" can be related to Cartesian Product which is also a nice-to-have function

How would you feel about Product for these functions and a follow-up PR with a CartesianProduct or CrossJoin (I believe this is used in other libraries) function?

#567

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