2021-08-18 08:03:33 +00:00
|
|
|
# gomap
|
|
|
|
|
|
|
|
Map functions utilizing basic interfaces, assertions, and ptypes.
|
|
|
|
|
2021-08-18 08:05:16 +00:00
|
|
|
This code and [ptypes](https://github.com/prequist/ptypes) are both artistic bloat,
|
|
|
|
utilised for DX concepts and/or type-evading for operations.
|
|
|
|
|
2021-08-18 08:03:33 +00:00
|
|
|
## Usage
|
|
|
|
|
|
|
|
<details closed>
|
|
|
|
<summary>Basic Setup</summary>
|
|
|
|
|
|
|
|
```go
|
|
|
|
import (
|
|
|
|
"github.com/prequist/gomap"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Showcase() {
|
|
|
|
// gomap.Transformer
|
|
|
|
transformer := func(i interface{}) interface{} {
|
|
|
|
// type assert the interface and add 1.
|
|
|
|
return i.(int) + 1
|
|
|
|
}
|
|
|
|
// Create a new list
|
|
|
|
list := gomap.New(1, 2, 3, 4)
|
|
|
|
mappable := gomap.MappableList{list}
|
|
|
|
// The outcome.
|
|
|
|
outcome := mappable.Map(transformer)
|
2021-08-19 21:13:22 +00:00
|
|
|
|
|
|
|
// For predefined slices, we can use this flow:
|
|
|
|
slice := []int{1, 2, 3, 4, 5}
|
|
|
|
list = gomap.New(slice)
|
|
|
|
mappable = gomap.MappableList{list}
|
|
|
|
outcome := mappable.Map(transformer)
|
2021-08-18 08:03:33 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
</details>
|
|
|
|
|
2021-09-06 18:34:46 +00:00
|
|
|
<details close>
|
|
|
|
<summary>Filter</summary>
|
|
|
|
|
|
|
|
```go
|
|
|
|
import (
|
|
|
|
"github.com/prequist/gomap"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Showcase() {
|
|
|
|
e := gomap.New("a", "b", "c", "ab", "d")
|
|
|
|
predicate := func(i interface{}) bool {
|
|
|
|
if str, ok := i.(string) {
|
|
|
|
return strings.Contains(str, "a")
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Get the mappable list
|
|
|
|
mappable := e.Mappable()
|
|
|
|
// Apply the predicate, return the filtered list as a variable
|
|
|
|
mappable = e.Filter(predicate) // "a", "ab"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
</details>
|
|
|
|
|
2021-08-18 08:03:33 +00:00
|
|
|
<details closed>
|
|
|
|
<summary>Boxed</summary>
|
|
|
|
|
|
|
|
```go
|
|
|
|
import (
|
|
|
|
"github.com/prequist/gomap"
|
|
|
|
"github.com/prequist/ptypes"
|
|
|
|
)
|
|
|
|
|
|
|
|
func MakeBoxedAndConvert() []int {
|
|
|
|
e := gomap.NewBoxed(1, 2, 3, 4, 5)
|
2021-09-06 18:34:46 +00:00
|
|
|
mappable := e.Mappable()
|
2021-08-18 08:03:33 +00:00
|
|
|
mappable.Map(func(v interface{}) interface{} {
|
|
|
|
ib := v.(ptypes.Box).IntBox()
|
|
|
|
return ptypes.FromInt(*ib.Int() + 2)
|
|
|
|
})
|
|
|
|
arr := make([]int, len(e.Items()))
|
|
|
|
for index, vi := range e.Items() {
|
|
|
|
arr[index] = *vi.(ptypes.Box).IntBox().Int()
|
|
|
|
}
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
</details>
|
|
|
|
|
|
|
|
<details closed>
|
|
|
|
<summary>Using <code>interface{}</code>s</summary>
|
|
|
|
|
|
|
|
```go
|
|
|
|
import (
|
|
|
|
"github.com/prequist/gomap"
|
|
|
|
"github.com/prequist/ptypes"
|
|
|
|
)
|
|
|
|
|
|
|
|
func MakeAndConvert() []int {
|
|
|
|
e := gomap.New(1, 2, 3, 4, 5)
|
2021-09-06 18:34:46 +00:00
|
|
|
mappable := e.Mappable()
|
2021-08-18 08:03:33 +00:00
|
|
|
mappable.Map(func(v interface{}) interface{} {
|
|
|
|
return v.(int) + 1
|
|
|
|
})
|
|
|
|
arr := make([]int, len(e.Items()))
|
|
|
|
for index, vi := range e.Items() {
|
|
|
|
arr[index] = vi.(int)
|
|
|
|
}
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
</details>
|
|
|
|
|
|
|
|
## Benchmarks
|
|
|
|
|
|
|
|
These are probably a bit bloat over time, however, they're not horrendous (compared to other)
|
|
|
|
implementations.
|
|
|
|
|
|
|
|
On Mac OSX (Monterey, M1)
|
|
|
|
|
|
|
|
### map_test.go
|
|
|
|
|
|
|
|
```
|
|
|
|
goos: darwin
|
|
|
|
goarch: arm64
|
|
|
|
pkg: github.com/prequist/gomap
|
|
|
|
BenchmarkPlain-8 12474228 100.0 ns/op
|
|
|
|
BenchmarkBox-8 759380 1523 ns/op
|
|
|
|
BenchmarkString-8 4029303 277.8 ns/op
|
|
|
|
BenchmarkBoxedString-8 782708 1501 ns/op
|
|
|
|
BenchmarkAdd-8 17820822 94.33 ns/op
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
### iterator_test.go
|
|
|
|
|
|
|
|
```
|
|
|
|
BenchmarkIteratorNext-8 1000000000 0.5661 ns/op
|
2021-09-06 18:34:17 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### filter_test.go
|
|
|
|
|
|
|
|
```
|
|
|
|
BenchmarkFilter
|
|
|
|
BenchmarkFilter/Filter
|
2021-09-06 18:34:46 +00:00
|
|
|
BenchmarkFilter/Filter-8 1000000000 0.0000009 ns/op
|
2021-08-18 08:03:33 +00:00
|
|
|
```
|