Skip to content

Commit a66bf34

Browse files
authored
proposal: adding zipby + unzipby (#449)
* feat: adding zipby + unzipby * Update README.md
1 parent 8c0dc3c commit a66bf34

File tree

3 files changed

+575
-10
lines changed

3 files changed

+575
-10
lines changed

README.md

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ In the future, 5 to 10 helpers will overlap with those coming into the Go standa
2424

2525
**Why this name?**
2626

27-
I wanted a **short name**, similar to "Lodash" and no Go package currently uses this name.
27+
I wanted a **short name**, similar to "Lodash" and no Go package uses this name.
2828

2929
![lo](img/logo-full.png)
3030

@@ -54,7 +54,7 @@ import (
5454
Then use one of the helpers below:
5555

5656
```go
57-
names := lo.Uniq[string]([]string{"Samuel", "John", "Samuel"})
57+
names := lo.Uniq([]string{"Samuel", "John", "Samuel"})
5858
// []string{"Samuel", "John"}
5959
```
6060

@@ -894,7 +894,7 @@ Returns a slice of all non-zero elements.
894894
```go
895895
in := []string{"", "foo", "", "bar", ""}
896896

897-
slice := lo.Compact[string](in)
897+
slice := lo.Compact(in)
898898
// []string{"foo", "bar"}
899899
```
900900

@@ -929,7 +929,7 @@ slice := lo.IsSortedByKey([]string{"a", "bb", "ccc"}, func(s string) int {
929929
Creates an array of the map keys.
930930

931931
```go
932-
keys := lo.Keys[string, int](map[string]int{"foo": 1, "bar": 2})
932+
keys := lo.Keys(map[string]int{"foo": 1, "bar": 2})
933933
// []string{"foo", "bar"}
934934
```
935935

@@ -951,10 +951,10 @@ values := lo.Values[string, int](map[string]int{"foo": 1, "bar": 2})
951951
Returns the value of the given key or the fallback value if the key is not present.
952952

953953
```go
954-
value := lo.ValueOr[string, int](map[string]int{"foo": 1, "bar": 2}, "foo", 42)
954+
value := lo.ValueOr(map[string]int{"foo": 1, "bar": 2}, "foo", 42)
955955
// 1
956956

957-
value := lo.ValueOr[string, int](map[string]int{"foo": 1, "bar": 2}, "baz", 42)
957+
value := lo.ValueOr(map[string]int{"foo": 1, "bar": 2}, "baz", 42)
958958
// 42
959959
```
960960

@@ -1089,7 +1089,7 @@ m2 := lo.Invert(map[string]int{"a": 1, "b": 2, "c": 1})
10891089
Merges multiple maps from left to right.
10901090

10911091
```go
1092-
mergedMaps := lo.Assign[string, int](
1092+
mergedMaps := lo.Assign(
10931093
map[string]int{"a": 1, "b": 2},
10941094
map[string]int{"b": 3, "c": 4},
10951095
)
@@ -1344,6 +1344,19 @@ tuples := lo.Zip2([]string{"a", "b"}, []int{1, 2})
13441344

13451345
[[play](https://go.dev/play/p/jujaA6GaJTp)]
13461346

1347+
### ZipBy2 -> ZipBy9
1348+
1349+
ZipBy creates a slice of transformed elements, the first of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on.
1350+
1351+
When collections have different size, the Tuple attributes are filled with zero value.
1352+
1353+
```go
1354+
items := lo.ZipBy2([]string{"a", "b"}, []int{1, 2}, func(a string, b int) string {
1355+
return fmt.Sprintf("%s-%d", a, b)
1356+
})
1357+
// []string{"a-1", "b-2"}
1358+
```
1359+
13471360
### Unzip2 -> Unzip9
13481361

13491362
Unzip accepts an array of grouped elements and creates an array regrouping the elements to their pre-zip configuration.
@@ -1356,6 +1369,18 @@ a, b := lo.Unzip2([]Tuple2[string, int]{{A: "a", B: 1}, {A: "b", B: 2}})
13561369

13571370
[[play](https://go.dev/play/p/ciHugugvaAW)]
13581371

1372+
### UnzipBy2 -> UnzipBy9
1373+
1374+
UnzipBy2 iterates over a collection and creates an array regrouping the elements to their pre-zip configuration.
1375+
1376+
```go
1377+
a, b := lo.UnzipBy2([]string{"hello", "john", "doe"}, func(str string) (string, int) {
1378+
return str, len(str)
1379+
})
1380+
// []string{"hello", "john", "doe"}
1381+
// []int{5, 4, 3}
1382+
```
1383+
13591384
### ChannelDispatcher
13601385

13611386
Distributes messages from input channels into N child channels. Close events are propagated to children.
@@ -1924,6 +1949,9 @@ min := lo.Min([]int{1, 2, 3})
19241949

19251950
min := lo.Min([]int{})
19261951
// 0
1952+
1953+
min := lo.Min([]time.Duration{time.Second, time.Hour})
1954+
// 1s
19271955
```
19281956

19291957
### MinBy
@@ -1958,6 +1986,9 @@ max := lo.Max([]int{1, 2, 3})
19581986

19591987
max := lo.Max([]int{})
19601988
// 0
1989+
1990+
max := lo.Max([]time.Duration{time.Second, time.Hour})
1991+
// 1h
19611992
```
19621993

19631994
### MaxBy
@@ -2712,7 +2743,7 @@ lo.Must0(ok, "'%s' must always contain '%s'", myString, requiredChar)
27122743

27132744
list := []int{0, 1, 2}
27142745
item := 5
2715-
lo.Must0(lo.Contains[int](list, item), "'%s' must always contain '%s'", list, item)
2746+
lo.Must0(lo.Contains(list, item), "'%s' must always contain '%s'", list, item)
27162747
...
27172748
```
27182749

@@ -2949,7 +2980,7 @@ make watch-test
29492980

29502981
Give a ⭐️ if this project helped you!
29512982

2952-
[![support us](https://c5.patreon.com/external/logo/become_a_patron_button.png)](https://www.patreon.com/samber)
2983+
[![GitHub Sponsors](https://img.shields.io/github/sponsors/samber?style=for-the-badge)](https://github.com/sponsors/samber)
29532984

29542985
## 📝 License
29552986

0 commit comments

Comments
 (0)