loncha

package module
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2022 License: MIT Imports: 6 Imported by: 3

README

loncha

Build Status Go Report Card codecov GitHub go.mod Go version of a Go module GitHub release

A high-performance slice Utilities for Go.

  • high-performance slice filter/finder.
  • linked-list generics template using Gennry

Installation

slice Utility
go get github.com/kazu/loncha

QuickStart

slice Utility

slice utility dosent use reflect/interface operation.

    import "github.com/kazu/loncha"

    type GameObject struct {
        ID int
        Name string
        Pos []float
    }

    ...

    var objs []GameObject

find object from slice


    loncha.Find(&objs, func(i int) bool {
        return objs[i].ID == 6
    } 

filter/delete object via condition function


    loncha.Filter(&objs, func(obj *GameObject) bool {
        return obj.ID == 12
    } 

	loncha.Delete(&objs, func(i int) bool {
		return objs[i].ID == 555
	})

select object with condition function


    // find one object with conditions.
    obj, err := Select(&objs, func(i int) bool {
		return slice[i].ID < 50
	})

shuffle slice

    err = loncha.Shuffle(objs, 2)

    loncha.Reverse(objs)

got intersection from two slices

    var obj2 []GameObject
    intersectedObj := InsertSect(obj, obj2)


    sort.Slice(objs, func(i int) bool {
        return objs[i].ID >=  objs[j].ID 
    })
    sort.Slice(objs2, func(i int) bool {
        return objs[i].ID >=  objs[j].ID 
    })

    intersect2 := IntersectSorted(obj, obj2, func(s []GameObject, i int) int {
        return s[i].ID
    })

subtraction from two slices

    subtractObj := Sub(obj, obj2)

    subtract2 := SubSorted(obj, obj2, func(s []GameObject, i int) int {
        return s[i].ID
    })

Returns an object formed from operands via function

	slice1 := []int{10, 6, 4, 2}

	sum := Inject(slice1, func(sum *int, t int) int {
		return *sum + t
	})

generate double-linked list of linux kernel list_head type

define base struct

package game_object

import (
    "github.com/kazu/loncha/list_head"


type Player struct {
    ID int
    Name string
    Hp int
    list_head.ListHead
}

generate linked-list

    $ go get go get github.com/cheekybits/genny
    $ wget -q -O - "https://github.com/kazu/loncha/master/container_list/list.go" | genny  gen "ListEntry=Player" > player_list.go
    $ wget -q -O - "https://github.com/kazu/loncha/master/container_list/list_test.go" | genny  gen "ListEntry=Player" > player_list_test.go

benchmark Result

loncha.Uniq vs hand Uniq vs go-funk.Uniq
loncha.Uniq-16         	    			1000	    997543 ns/op	  548480 B/op	   16324 allocs/op
loncha.UniqWithSort-16 	    			1000	   2237924 ns/op	     256 B/op	       7 allocs/op
loncha.UniqWithSort(sort)-16         	1000	    260283 ns/op	     144 B/op	       4 allocs/op
hand_Uniq-16                          	1000	    427765 ns/op	  442642 B/op	       8 allocs/op
hand_Uniq_iface-16                    	1000	    808895 ns/op	  632225 B/op	    6322 allocs/op
go-funk.Uniq-16                       	1000	   1708396 ns/op	  655968 B/op	   10004 allocs/op
loncha.Filter vs go-funk.Filter
loncha.Filter-16         	     100	     89142 ns/op	   82119 B/op	       4 allocs/op
loncha.Filter_pointer-16 	     100	       201 ns/op	       0 B/op	       0 allocs/op
hand_Filter_pointer-16   	     100	     24432 ns/op	   81921 B/op	       1 allocs/op
go-funk.Filter-16        	     100	   2370492 ns/op	  640135 B/op	   20004 allocs/op
go-funk.Filter_pointer-16        100	      1048 ns/op	      64 B/op	       2 allocs/op

References

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ERR_SLICE_TYPE           error = errors.New("parameter must be slice or pointer of slice")
	ERR_POINTER_SLICE_TYPE   error = errors.New("parameter must be pointer of slice")
	ERR_NOT_FOUND            error = errors.New("data is not found")
	ERR_ELEMENT_INVALID_TYPE error = errors.New("slice element is invalid type")
	ERR_INVALID_INDEX        error = errors.New("invalid index")
)

Functions

func Contain

func Contain(slice interface{}, fn CondFunc) bool

Contain get return true which fn condition is true.

func Containable added in v0.6.2

func Containable[T comparable](fn CondFunc2[T]) func([]T) bool

Containable ... generate function of slice contain.

func Conv added in v0.5.6

func Conv[S, D any](srcs []S, convfn ConvFunc[S, D]) (dsts []D)

func Convertable added in v0.6.2

func Convertable[S, D any](fn ConvFunc[S, D]) func([]S) []D

Convertable ... generate function of slice conversion.

func DefauiltOpt added in v0.5.1

func DefauiltOpt[T any]() *opParam[T]

DefauiltOpt ... default function option

func Delete

func Delete(slice interface{}, funcs ...CondFunc) error

Delete ... Delete element with mached funcs

func Every added in v0.6.3

func Every[T any](fn CondFunc2[T]) func(...T) bool

Every ... Determines whether all the members of an array satisfy the specified test.

func EveryWithIndex added in v0.6.3

func EveryWithIndex[T comparable](fn CondFuncWithIndex[T]) func(...T) bool

EveryWithIndex ... Determines whether all the members of an array satisfy the specified test.

func Filter

func Filter[T comparable](slice []T, condFn CondFunc2[T], opts ...Opt[FilterOpt[T]]) ([]T, error)

Filter ... FIlter implementation with type parameters

func Find

func Find(slice interface{}, fn CondFunc) (interface{}, error)

Find is value of slice if fn is true. if slice is not pointer of slice or empty, return error

func IndexOf

func IndexOf(slice interface{}, fn CondFunc) (int, error)

IndexOf gets the index at which the first match fn is true. if not found. return -1. return error if slice is not pointer of the slice.

func Inject added in v0.5.5

func Inject[T any, V any](s []T, injectFn InjectFn[T, V], opts ...OptCurry[V]) (v V)

Inject ... return an object formed from operands via InjectFn

func Intersect added in v0.5.1

func Intersect[T comparable](slice1, slice2 []T, opts ...Opt[IntersectOpt]) (result []T)

Intersect ... intersection between 2 slice.

func IntersectSorted added in v0.5.1

func IntersectSorted[T any, V Ordered](slice1, slice2 []T, IdentFn IdentFunc[T, V]) (result []T)

IntersectSorted ... intersection between 2 sorted slice

func Keys added in v0.5.6

func Keys[K comparable, V any](m map[K]V) (keys []K)

func LastIndexOf added in v0.1.1

func LastIndexOf(slice interface{}, fn CondFunc) (int, error)

LastIndexOf gets the last index at which the last match fn is true. if not found. return -1. return error if slice is not pointer of the slice.

func MergeOpts added in v0.5.1

func MergeOpts[T any](opts ...Opt[T]) (*opParam[T], func(p *opParam[T]))

MergeOpts ... using innner in function with Opt[T]

this run DefaultOpt() and Options() .

func NewOpt added in v0.6.3

func NewOpt[T any](opts ...OptCurry[T]) *curryParam[T]

func OldFilter added in v0.5.6

func OldFilter(slice interface{}, funcs ...CondFunc) error

OldFilter ... OldFilter element with mached funcs Deprecated: should use FIlter

func Reduce added in v0.5.5

func Reduce[T any, V any](s []T, injectFn InjectFn[T, V]) (v V)

Reduce ... alias of Inject

func Reverse added in v0.1.2

func Reverse(slice interface{})

Reverse ... Transforms an array such that the first element will become the last, the second element will become the second to last, etc.

func Select

func Select(slice interface{}, fn CondFunc) (interface{}, error)

Select ... return all element on match of CondFunc

func SelectMap added in v0.5.6

func SelectMap[K comparable, V any](m map[K]V, fn func(k K, v V) (K, V, bool)) (result map[K]V)

SelectMap ... rewrite map each key, value pair.

func Shuffle

func Shuffle(slice interface{}, seeds ...int64) (e error)

Shuffle is shuffing slice order. if slice is not pointer of slice or not slice, return error

func Sub added in v0.5.4

func Sub[T comparable](slice1, slice2 []T, opts ...Opt[subOpt]) (result []T)

Sub .. subtraction between two slices.

func SubSorted added in v0.5.4

func SubSorted[T any, V Ordered](slice1, slice2 []T, IdentFn IdentFunc[T, V]) (result []T)

SubSorted ... subtraction in sorted slice

func Sum added in v0.5.6

func Sum[T Ordered](s []T) (v T)

Sum ... sum of slice values in Ordered type

func SumWithFn added in v0.5.6

func SumWithFn[T any, V Ordered](s []T, fn SumIdent[T, V]) (v V)

SumWithFn ... sum of slice values in non-Ordered type with SumIdent()

func ToMap added in v0.6.2

func ToMap[K, V constraints.Ordered](r map[K]V, k K, v V) map[K]V

ToMap ... function for Zipper . return map from double array.

func Uniq

func Uniq(slice interface{}, fn IdentFn) error

Uniq is deduplicate using fn . if slice is not pointer of slice or empty, return error

func Uniq2

func Uniq2(slice interface{}, fn CompareFunc) error

Uniq2 is deduplicate using fn . if slice is not pointer of slice or empty, return error

func UniqWithSort

func UniqWithSort(slice interface{}, fn CompareFunc) (int, error)

UniqWithSort is deduplicating using fn, sorting before dedup. if slice is not pointer of slice or empty, return error

func Values added in v0.5.6

func Values[K comparable, V any](m map[K]V) (values []V)

func Zipper added in v0.6.2

func Zipper[T, S, R any](fn func(R, T, S) R, start R) func([]T, []S) R

Zipper ... return tuple operation result using fn with double slice,

Types

type CompareFunc

type CompareFunc func(i, j int) bool

type CondFunc

type CondFunc func(idx int) bool

type CondFunc2 added in v0.5.6

type CondFunc2[T any] func(t *T) bool

type CondFunc3 added in v0.6.3

type CondFunc3[T any] func(t T) bool

type CondFuncWithIndex added in v0.6.3

type CondFuncWithIndex[T any] func(i int, t *T) bool

type CondFuncWithPtr added in v0.6.3

type CondFuncWithPtr[T any] interface {
	CondFunc2[T] | CondFunc3[T]
}

type ConvFunc added in v0.5.6

type ConvFunc[T any, S any] func(T) (S, bool)

type FilterFunc added in v0.6.1

type FilterFunc[T any] func([]T) []T

FilterFunc ... function generated by Filterlize()

func Deletable added in v0.6.1

func Deletable[T comparable](fns ...CondFunc2[T]) FilterFunc[T]

Deletable ... generate deleting function by fns Condition for slice

func Filterable added in v0.6.1

func Filterable[T any](fns ...CondFunc2[T]) FilterFunc[T]

Filterable ... generate filter function for slice

func Selectable added in v0.6.1

func Selectable[T any](fns ...CondFunc2[T]) FilterFunc[T]

Selectable ... generate deleting function by fns Condition for slice

type FilterOpt added in v0.5.6

type FilterOpt[T any] struct {
	// contains filtered or unexported fields
}

FilterOpt ... functional option for FIlter2()

type GetFunc added in v0.6.3

type GetFunc[T any] func([]T) T

func Gettable added in v0.6.3

func Gettable[T any](fns ...CondFunc2[T]) GetFunc[T]

Gettable ... generate deleting function by fns Condition for slice

type IdentFn

type IdentFn func(i int) interface{}

type IdentFunc added in v0.5.5

type IdentFunc[T any, V Ordered] func(slice []T, i int) V

type InjectFn added in v0.5.5

type InjectFn[T any, R any] func(R, T) R

InjectFn ... function for Inject()

type InjecterFunc added in v0.6.1

type InjecterFunc[T any, R any] func([]T) R

InjecterFunc ... condition function for Injectable

func Injectable added in v0.6.1

func Injectable[T any, V any](injectFn InjectFn[T, V], opts ...OptCurry[V]) InjecterFunc[T, V]

Injectable ... generate Inject functions

func Reducable added in v0.6.1

func Reducable[T any, V any](injectFn InjectFn[T, V], opts ...OptCurry[V]) InjecterFunc[T, V]

Reducable ... alias of Injectable

type IntersectOpt added in v0.5.6

type IntersectOpt struct {
	Uniq bool
}

type Number added in v0.6.2

type Number interface {
	constraints.Integer | constraints.Float
}

Number ... Number constraints

type Opt added in v0.5.1

type Opt[T any] func(*opParam[T]) Opt[T]

Opt ... functional options of lonch operator

func Cond added in v0.5.6

func Cond[T any, PT condFuncSetter[T]](fns ...CondFunc) Opt[T]

Cond ... set conditional function for FIlter2()

func Cond2 added in v0.5.6

func Cond2[OptT any, S comparable, OptPT condFuncSetter2[OptT, S]](fns ...CondFunc2[S]) Opt[OptT]

Cond2 ... no index variant of Cond

func Equal added in v0.5.6

func Equal[T any, S any, PT eqaulSetter[T, S]](v S) Opt[T]

func FilterVersion added in v0.5.6

func FilterVersion[T any, PT fVersionSetter[T]](v int) Opt[T]

type OptCurry added in v0.6.3

type OptCurry[T any] func(*curryParam[T]) OptCurry[T]

OptCurry ... functional option

func Default added in v0.6.3

func Default[T any](t T) OptCurry[T]

type Ordered added in v0.5.4

type Ordered interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
		~float32 | ~float64 |
		~string
}

Ordered ... copy from https://github.com/golang/go/blob/go1.18.3/test/typeparam/ordered.go

type SumIdent added in v0.5.6

type SumIdent[T any, V Ordered] func(e T) V

SumIdent ... return Ordered value onnot-Ordered type

Directories

Path Synopsis
ContainerListWriter is a base of http://golang.org/pkg/container/list/ this is tuning performancem, reduce heap usage.
ContainerListWriter is a base of http://golang.org/pkg/container/list/ this is tuning performancem, reduce heap usage.
Package loncha/list_head is like a kernel's LIST_HEAD list_head is used by loncha/gen/containers_list
Package loncha/list_head is like a kernel's LIST_HEAD list_head is used by loncha/gen/containers_list

Jump to

Keyboard shortcuts

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