mirror of gomap just cuz
ファイルへ移動
bfu 8bee0afdc7
(repo): update ownership and benchmarks
2022-04-14 11:53:32 -04:00
.gitignore initial commit 2021-08-18 04:03:33 -04:00
LICENSE (repo): update ownership and benchmarks 2022-04-14 11:53:32 -04:00
README.md (repo): update ownership and benchmarks 2022-04-14 11:53:32 -04:00
box_list.go initial commit 2021-08-18 04:03:33 -04:00
callable.go feat: slice support 2021-08-19 17:13:22 -04:00
filter.go (feat): filter function, list#Mappable() 2021-09-06 14:34:17 -04:00
filter_test.go (repo): update ownership and benchmarks 2022-04-14 11:53:32 -04:00
go.mod update mod 2022-02-07 18:16:18 -05:00
go.sum initial commit 2021-08-18 04:03:33 -04:00
handles.go feat: slice support 2021-08-19 17:13:22 -04:00
iterator.go (iterator): utilise a pointer to avoid copying of array elements 2021-08-18 19:30:55 -04:00
iterator_test.go initial commit 2021-08-18 04:03:33 -04:00
list.go (feat): filter function, list#Mappable() 2021-09-06 14:34:17 -04:00
map.go (feat): filter tests, docs, list#Mappable() 2021-09-06 14:34:46 -04:00
map_test.go (repo): update ownership and benchmarks 2022-04-14 11:53:32 -04:00
remap.go feat: slice support 2021-08-19 17:13:22 -04:00
slice.go feat: slice support 2021-08-19 17:13:22 -04:00
slice_test.go (repo): update ownership and benchmarks 2022-04-14 11:53:32 -04:00

gomap

Map functions utilizing basic interfaces, assertions, and ptypes.

This code and ptypes are both artistic bloat, utilised for DX concepts and/or type-evading for operations.

Usage

Basic Setup
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)
    
    // 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)
}
Filter
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"
}
Boxed
import (
	"github.com/prequist/gomap"
	"github.com/prequist/ptypes"
)

func MakeBoxedAndConvert() []int {
	e := gomap.NewBoxed(1, 2, 3, 4, 5)
	mappable := e.Mappable()
	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
}
Using interface{}s
import (
	"github.com/prequist/gomap"
	"github.com/prequist/ptypes"
)

func MakeAndConvert() []int {
	e := gomap.New(1, 2, 3, 4, 5)
	mappable := e.Mappable()
	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
}

Benchmarks

These are probably a bit bloat over time, however, they're not horrendous (compared to other) implementations.

On Mac OSX (Big Sur, M1)

map_test.go

goos: darwin
goarch: arm64
pkg: git.tcp.direct/bfu/gomap
BenchmarkPlain-8          	 5423301	       221.0 ns/op
BenchmarkBox-8            	  937978	      1240 ns/op
BenchmarkString-8         	 3070027	       394.6 ns/op
BenchmarkBoxedString-8    	  762390	      1489 ns/op
BenchmarkAdd-8            	27079604	        52.38 ns/op

iterator_test.go

BenchmarkIteratorNext-8   	1000000000	         0.7843 ns/op

filter_test.go

BenchmarkFilter
BenchmarkFilter/Filter
BenchmarkFilter/Filter-8  	1000000000	         0.0000008 ns/op