Skip to content

Commit

Permalink
feat: adding tuple types (from 2 to 9) and zip+unzip functions
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Mar 5, 2022
1 parent 4edabde commit a482eff
Show file tree
Hide file tree
Showing 6 changed files with 549 additions and 6 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ Supported helpers for maps:
- FromEntries
- Assign (maps merge)

Supported helpers for tuples:

- Zip2 -> Zip9
- Unzip2 -> Unzip9

Supported intersection helpers:

- Contains
Expand Down Expand Up @@ -391,6 +396,27 @@ mergedMaps := lo.Assign[string, int](
// map[string]int{"a": 1, "b": 3, "c": 4}
```

### Zip2 -> Zip9

Zip creates a slice of grouped 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.

When collections have different size, the Tuple attributes are filled with zero value.

```go
tuples := lo.Zip2[string, int]([]string{"a", "b"}, []int{1, 2})
// []Tuple2[string, int]{{A: "a", B: 1}, {A: "b", B: 2}}
```

### Unzip2 -> Unzip9

Unzip accepts an array of grouped elements and creates an array regrouping the elements to their pre-zip configuration.

```go
a, b := lo.Unzip2[string, int]([]Tuple2[string, int]{{A: "a", B: 1}, {A: "b", B: 2}})
// []string{"a", "b"}
// []int{1, 2}
```

### Every

Returns true if all elements of a subset are contained into a collection.
Expand Down
1 change: 1 addition & 0 deletions constraints.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package lo

// Clonable defines a constraint of types having Clone() T method.
type Clonable[T any] interface {
Clone() T
}
6 changes: 0 additions & 6 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ func Values[K comparable, V any](in map[K]V) []V {
return result
}

// Entry defines a key/value pairs.
type Entry[K comparable, V any] struct {
Key K
Value V
}

// Entries transforms a map into array of key/value pairs.
func Entries[K comparable, V any](in map[K]V) []Entry[K, V] {
entries := make([]Entry[K, V], 0, len(in))
Expand Down
Loading

0 comments on commit a482eff

Please sign in to comment.