Skip to content

Commit

Permalink
0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
wwaayyaa committed Mar 22, 2022
1 parent 98b66ac commit 03470bb
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 6 deletions.
72 changes: 70 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
`go collection` is a tool implemented using generic, it can help you process slice/map data quickly and easily convert
between them.

### Installation
Note: To use this project, you need to upgrade to go1.18 version.
Some methods cannot be implemented in the 1.18 version,
and will be supported after version 1.19 supports type methods.

## 🚀 Install

```go
go get -u github.com/wwaayyaa/go-collection
```

Then import it
## ✏️ Usage

```go
import collect "github.com/wwaayyaa/go-collection"
Expand Down Expand Up @@ -46,3 +50,67 @@ NewMapCollection(map[string]int{"a": 1, "z": 100}).Keys()
NewMapCollection(map[string]int{"a": 1, "z": 100}).Values()
```

## 📖 API

### Slice

- `Len`
- `First`
- `Last`
- `Get`
- `Put`
- `Prepend`
- `Shift`
- `Push`
- `Pop`
- `Find`
- `Index`
- `Each`
- `Map`
- `Transform`
- `All`
- `Contains`
- `Filter`
- `Reject`
- `Concat`
- `Join`
- `Clone`
- `Tap`
- `ToJson`
- `Empty`
- `Diff`
- `Merge`
- `Reverse`
- `Slice`
- `Delete`
- `Chunk`
- `Uniq`
- `Shuffle`
- `Keys`
- `Values`
- `Only`
- `Except`
- `Reduce`
- `FlatMap`
- `GroupBy`
- `KeyBy`
- `Flatten`

### Map

- `All`
- `Count`
- `Empty`
- `Keys`
- `Values`
- `Entries`
- `FromEntries`
- `Has`
- `Get`
- `Put`
- `Pull`
- `Union`
- `Intersect`
- `Diff`
- `SymmetricDiff`

5 changes: 5 additions & 0 deletions maps/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@ func NewMapCollection[K comparable, V any](v map[K]V) *MapCollection[K, V] {
func (co *MapCollection[K, V]) All() map[K]V {
return co.items
}

func (co *MapCollection[K, V]) Count() int {
return len(co.items)
}

func (co *MapCollection[K, V]) Empty() bool {
return co.Count() == 0
}

func (co *MapCollection[K, V]) Keys() (keys []K) {
for k, _ := range co.items {
keys = append(keys, k)
}
return keys
}

func (co *MapCollection[K, V]) Values() (values []V) {
for _, v := range co.items {
values = append(values, v)
Expand Down Expand Up @@ -67,6 +71,7 @@ func (co *MapCollection[K, V]) Get(key K) (value V, _ bool) {
return value, false
}
}

func (co *MapCollection[K, V]) Put(key K, value V) *MapCollection[K, V] {
co.items[key] = value
return co
Expand Down
6 changes: 2 additions & 4 deletions slices/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ import (
/*
TODO
The following functions are achievable and will be updated soon:
forget delete concat sortBy sortByDesc splice pluck
delete sortBy sortByDesc splice pluck
sum avg max min median
The following features require version 1.19 to allow methods to have type parameters. Because most of them return arbitrary types on demand.
Example:
func (co *SliceCollection[T]) Reduce[R any](fn func(R, T) R, R) R{
* This R is the type we want
}
when keyBy
*/

type SliceCollection[T any] struct {
Expand Down Expand Up @@ -78,7 +76,6 @@ func (co *SliceCollection[T]) Index(fn func(T, int) bool) int {
return -1
}

//Each cannot change self
func (co *SliceCollection[T]) Each(fn func(T, int) bool) *SliceCollection[T] {
for i, v := range co.items {
if !fn(v, i) {
Expand Down Expand Up @@ -120,6 +117,7 @@ func (co *SliceCollection[T]) Filter(fn func(T, int) bool) *SliceCollection[T] {
}
return NewSliceCollection(ret)
}

func (co *SliceCollection[T]) Reject(fn func(T) bool) *SliceCollection[T] {
var ret []T
for _, v := range co.items {
Expand Down

0 comments on commit 03470bb

Please sign in to comment.