Skip to content

feat: add Product and ProductBy functions#566

Merged
samber merged 1 commit intosamber:masterfrom
JohnDevitt:master
Jan 27, 2025
Merged

feat: add Product and ProductBy functions#566
samber merged 1 commit intosamber:masterfrom
JohnDevitt:master

Conversation

@JohnDevitt
Copy link
Contributor

@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
Contributor 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
Contributor 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

@samber
Copy link
Owner

samber commented Jan 26, 2025

I vote for product 😇

@samber
Copy link
Owner

samber commented Jan 27, 2025

itertools libs in python and rust use product

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.

3 participants