Add a Join() function to the lo package#899
Conversation
|
Fixes #351. |
|
The WDYT ? Do you have some use case to demonstrate ? |
I already had to join a myBools := []bool{true, false, true}
joined := strings.Join(lo.Map(myBools, fmt.Sprint), ", ")
fmt.Println(joined)I don't find it very clear, and I'ld prefer my method, which lets me to do like this: myBools := []bool{true, false, true}
joined := lo.Join(myBools, ", ")
fmt.Println(joined)Don't you think this method is more readable?
If the user wants to convert each element to string manually, they can do it with your method ( |
|
tl;dr - This function doesn't seem generic enough for I agree that serialization format is subjective and should be done by the caller rather than embedded inside the myBools := []bool{true, false, true}
joined := strings.Join(lo.Map(myBools, strconv.FormatBool), ", ")It's a pretty big assumption to make that the user want's the list of bools to be |
Describe your changes
I added a
Join()function to thelopackage.It takes a collection and a separator (that can be any type) and returns a string that contains
every item, separated by the specified separator.
It uses
fmtto convert list items and separator to string, and astrings.Builderto construct it.I created an example on
pkg.go.devbut because the import path is github.com/samber/lo, it will be available only if you accept the PR.I wrote various tests in ./slice_test.go. Feel free to look at them if you want to verify, I tried to cover every case where my function could bug.
Checklist before requesting a review
Conventions
(ok bool)is often better than(err error)panic(...)must be limitedlo.Must0,lo.Must1,lo.Must2, ...lo.SliceToMapvslo.SliceToMapIlo.Ternaryvslo.TernaryF