gomap

package module
v0.0.0-...-8bee0af Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2022 License: MIT Imports: 5 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// StringHandler global instance.
	StringHandler = PutHandler("string", ContentHandler{handleString, remapString})

	// IntHandler global instance.
	IntHandler = PutHandler("int", ContentHandler{handleInt, remapInt})

	// UintHandler global instance.
	UintHandler = PutHandler("uint", ContentHandler{handleUint, remapUint})

	// FloatHandler global instance.
	FloatHandler = PutHandler("float", ContentHandler{handleFloat, remapFloat})

	// ComplexHandler global instance.
	ComplexHandler = PutHandler("complex", ContentHandler{handleComplex, remapComplex})

	// UintptrHandler global instance.
	UintptrHandler = PutHandler("uintptr", ContentHandler{handleUintptr, remapUintptr})
)

Functions

This section is empty.

Types

type Callable

type Callable struct {
	Passed bool
	Result interface{}
}

The Callable structure allows chaining of function calls (promise like)

func (Callable) Cleanup

func (callable Callable) Cleanup(function func())

Cleanup is the call to end the callable's chain.

func (Callable) OnFail

func (callable Callable) OnFail(function func()) Callable

OnFail calls a function on the failure of a callable

func (Callable) Then

func (callable Callable) Then(function func(value interface{})) Callable

Then calls something if the callable has passed.

type ContentHandler

type ContentHandler struct {
	// Handler is the handler.
	Handler Handler
	// Mapper is the remapper to `interface{}`.
	Mapper RemapHandler
}

The ContentHandler structure is a registration structure to hold a handler and a remapper.

func PutHandler

func PutHandler(name string, handler ContentHandler) ContentHandler

PutHandler puts a handler into the registered handlers map, registered by name.

type Handler

type Handler func(i interface{}, args ...interface{}) (*List, error)

The Handler is a type for a slice handler function.

type Iterator

type Iterator struct {

	// The current value.
	Value interface{}
	// contains filtered or unexported fields
}

The Iterator is a small struct used for handling iterator-like usage. Go itself does not support the while loop, however, we can achieve similar usage recursively.

func (*Iterator) Next

func (it *Iterator) Next() (bool, interface{})

Next gets the next value in the iterator

type List

type List struct {
	// contains filtered or unexported fields
}

The List struct is the actual list.

func Map

func Map(list *List, transformer Transformer) *List

The Map function handles mapping for a raw List.

func New

func New(v ...interface{}) *List

New creates a new list with the given elements.

func NewBoxed

func NewBoxed(v ...interface{}) *List

NewBoxed creates a new boxed List.

func NewFromPrimitiveSlice

func NewFromPrimitiveSlice(slice interface{}, element interface{}) *List

NewFromPrimitiveSlice handles slice support, however, to avoid the reflect solution to this, we try to handle type assertion ourself.

The parameter element takes the first element.

Note that if the slice is of []interface{}, the New function may be used instead.

func (*List) Add

func (list *List) Add(v interface{}) *List

Add an item to the list.

func (*List) Items

func (list *List) Items() []interface{}

Items returns the items in the list.

func (*List) Iterator

func (list *List) Iterator() *Iterator

Iterator returns the list's iterator. If it does not exist, it will create a new one.

func (*List) Mappable

func (list *List) Mappable() MappableList

Mappable returns a mappable list from an existing List.

func (*List) Remove

func (list *List) Remove(index int) *List

The Remove function removes the item at the requested index.

type MappableList

type MappableList struct {
	// The embedded list.
	*List
}

A MappableList is able to apply a Transformer to it's elements.

func (*MappableList) Filter

func (l *MappableList) Filter(predicate Predicate) MappableList

func (*MappableList) Map

func (l *MappableList) Map(transformer Transformer) *List

The Map function is called on a MappableList to apply the requested Transformer.

type Predicate

type Predicate func(interface{}) bool

The Predicate is a type alias for a function that takes in a parameter and returns a boolean. The predicate is similar to Java's `Predicate<T>`, without the generic usage. This makes the usage of a predicate not necessarily type safe. This also means that the writer of the predicate has to write code that they trust when passing the predicate into the filter.

type RemapHandler

type RemapHandler func(slice interface{}, args ...interface{}) []interface{}

The RemapHandler is the function type for any handler for remapping.

type Transformer

type Transformer func(v interface{}) interface{}

The Transformer is a type alias for a mapping function.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL