https://github.com/yah01/container
Generic containers collection for golang
https://github.com/yah01/container
Last synced: 2 months ago
JSON representation
Generic containers collection for golang
- Host: GitHub
- URL: https://github.com/yah01/container
- Owner: yah01
- Created: 2022-03-21T18:11:40.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-05-28T17:05:55.000Z (about 4 years ago)
- Last Synced: 2025-01-26T00:41:33.401Z (over 1 year ago)
- Language: Go
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Container
## Get Started
### Iterator
```golang
slice := cslice.NewSlice(5, 4, 3, 2, 1)
for iter := slice.Iter(); iter.Valid(); iter.Next() {
fmt.Print(iter.Value()," ")
}
// output:
// 5 4 3 2 1
```
Some utils are very useful, use Map() to double all elements:
```golang
slice := cslice.NewSlice(5, 4, 3, 2, 1)
iter := Map(slice.Iter(), func(elem int) int {
return elem * 2
})
for ; iter.Valid(); iter.Next() {
fmt.Print(iter.Value()," ")
}
// output:
// 10 8 6 4 2
```
Use Reduce() to get bit-OR of all elements:
```golang
slice := cslice.NewSlice(5, 4, 3, 2, 1)
result := Reduce(slice.Iter(), func(result, elem int) int {
return result|elem
})
fmt.Print(result)
// output:
// 7
```
Use Enumerate() to get an iterator with index:
```golang
slice := cslice.NewSlice(5, 4, 3, 2, 1)
for iter := Enumerate(slice.Iter()); iter.Valid(); iter.Next() {
v := iter.Value()
fmt.Println(v.Idx, v.Value)
}
// output:
// 0 5
// 1 4
// 2 3
// 3 2
// 4 1
```