un

package module
v0.0.0-...-d993858 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2023 License: MIT Imports: 2 Imported by: 2

README

Underscore.go

Move Fast; Optimize Late

A useful collection of Go utilities. Designed for programmer happiness.

TL;DR Sort-of like underscore.js, but for Go

API Documentation

GoDoc is WorkInProgress

⚠ Warning

This package is in heavy flux at the moment as I work to incorporate feedback from various sources.

:squirrel: Todo

  • godoc
  • contains
  • indexOf
  • worker pools
  • parallel each
  • parallel map with worker pool
  • refactor to make functions first parameter (eg Each func(func(A), []A))
  • handle maps & slices
  • all
  • any
  • none

Typed Functions

Any

Each

Each func(func(A int), []A) Each func(func(A B), []A)

Applies the given iterator function to each element of a collection (slice or map).

If the collection is a Slice, the iterator function arguments are value, index

If the collection is a Map, the iterator function arguments are value, key

EachP is a Parallel implementation of Each and concurrently applies the given iterator function to each element of a collection (slice or map).

  // var Each func(func(value interface{}, i interface{}), interface{})

  var buffer bytes.Buffer

  fn := func(s, i interface{}) {
    buffer.WriteString(s.(string))
  }

  s := []string{"a", "b", "c", "d", "e"}
  Each(fn, s)

  expect := "abcde"

  e := un.Each(fn, s)

  fmt.Printf("%#v\n", e) //"abcde"

Typed Each can be defined using a function type and the MakeEach helper.

Using a Typed Slice

  var EachInt func(func(value, i int), []int)
  MakeEach(&EachInt)

  var sum int

  fn := func(v, i int) {
    sum += v
  }

  i := []int{1, 2, 3, 4, 5}
  EachInt(fn, i)

  fmt.Printf("%#v\n", sum) //15

Using a Typed Map

  var EachStringInt func(func(key string, value int), map[string]int)
  var sum int

  fn := func(v int, k string) {
    sum += v
  }

  m := map[string]int{"a": 1, "b": 2, "c": 3, "d": 4, "e": 5}
  EachStringInt(fn, m)

  fmt.Printf("%#v\n", sum) //15

Of note is the ability to close over variables within the calling scope.

Every

Map

Map func([]A, func(A) B) []B

Applies the given function to each element of a slice, returning a slice of results

The base Map function accepts interface{} types and returns []interface{}

  // Map func(interface{}, func(interface{}) interface{}) []interface{}

  s := []string{"a", "b", "c", "d"}

  fn := func(s interface{}) interface{} {
    return s.(string) + "!"
  }

  m := un.Map(ToI(s), fn)
  fmt.Println(m) //["a!", "b!", "c!", "d!"]

Typed Maps can be defined using a function type and the MakeMap helper.

  Map func([]A, func(A) B) []B

  var SMap func([]string, func(string) string) []string
  un.MakeMap(&SMap)

  m := un.SMap(s, fn)
  fmt.Println(m) //["a!", "b!", "c!", "d!"]

Of note is the return value of Map is a slice of the return type of the applied function.

Partition

Partition func([]A, func(A) bool) ([]A []A)

Partition splits a slice or map based on the evaluation of the supplied function

The base Partition function accepts interface{} types and returns []interface{}

  // Partition func(interface{}, func(interface{}) bool) ([]interface{}, []interface{})

  s := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

  fn := func(i interface{}) bool {
    return (i.(int) % 2) == 1
  }

  odd, even := un.Partition(s, fn)

  fmt.Println(odd)  //[1, 3, 5, 7, 9]
  fmt.Println(even) //[2, 4, 6, 8, 10]

Typed Partitions can be defined using a function type and the MakePartition helper.

  // Partition func([]A, func(A) bool) ([]A []A)

  var IPartition func([]int, func(int) bool) ([]int, []int)

  un.MakePartition(&IPartition)

  s := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

  fn := func(i int) bool {
    return (i % 2) == 1
  }

  odd, even := un.IPartition(s, fn)

  fmt.Println(odd)  //[1, 3, 5, 7, 9]
  fmt.Println(even) //[2, 4, 6, 8, 10]

Contains returns true if an object is in a slice.

  o := "a"
  s := []string{"a", "b", "c"}

  b := un.Contains(s, o)
  fmt.Println(b) //true

ToI converts a slice of arbitrary type []T into a slice of []interfaces{}

  s := []int{1, 1, 3, 5, 8, 13}
  i := un.ToI(s)

Notes

I am aware that the whole idea is not particularly very TheGoWay™, but it is useful as a learning exercise, and it is useful for moving fast and optimising later.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Any func(fn, slice_or_map interface{}) bool

var Any func(func(value interface{}) bool, interface{}) bool

View Source
var AnyInt func(func(value int) bool, []int) bool

AnyInt Returns true if all values in a []int pass the predicate truth test Predicate function accepts an int and returns a boolean

View Source
var AnyString func(func(value string) bool, []string) bool

AnyString Returns true if all values in a []string pass the predicate truth test Predicate function accepts a string and returns a boolean

View Source
var Each func(fn interface{}, slice_or_map interface{})

Each func(func(A, B), []A) Applies the given iterator function to each element of a collection (slice or map). If the collection is a Slice, the iterator function arguments are *value, index* If the collection is a Map, the iterator function arguments are *value, key* Iterator functions accept a value, and the index or key is an optional argument. Note: each does not return a value, you may want un.Map var Each func(func(value, i interface{}), interface{})

View Source
var EachInt func(func(value, i int), []int)

EachInt Applies the given iterator function to each element of []int Iterator function arguments are *value, index*

View Source
var EachP func(fn interface{}, slice_or_map interface{})

EachP Parallel Each *Concurrently* applies the given iterator function to each element of a collection (slice or map).

View Source
var EachStringInt func(func(value int, key string), map[string]int)

EachStringInt Applies the given iterator function to each element of map[string]int Iterator function arguments are *value, key*

View Source
var Every func(fn, slice_or_map interface{}) bool

var Every func(func(value interface{}) bool, interface{}) bool

View Source
var EveryInt func(func(value int) bool, []int) bool

EveryInt Returns true if all values in a []int pass the predicate truth test Predicate function accepts an int and returns a boolean

View Source
var EveryString func(func(value string) bool, []string) bool

EveryString Returns true if all values in a []string pass the predicate truth test Predicate function accepts a string and returns a boolean

View Source
var Map func(interface{}, interface{}) []interface{}

Map func(func(A) C, []A) []C Applies the given iterator function to each element of a collection (slice or map) and returns a new slice of the computed results. If the collection is a Slice, the iterator function arguments are *value, index* If the collection is a Map, the iterator function arguments are *value, key* Iterator functions accept a value, and the index or key is an optional argument.

View Source
var MapInt func(func(int) int, []int) []int

Applies the given iterator function to each element of a []int and returns a new []int of the computed results.

View Source
var MapP func(interface{}, interface{}, ...int) []interface{}

Applies the given iterator function to each element of a collection (slice or map) and returns a new slice of the computed results.

View Source
var MapPString func(func(string) string, []string, ...int) []string

Applies the given iterator function to each element of a []string and returns a new []string of the computed results. <p>Uses a Worker Pool using either the global worker value (un.SetWorker) or as an optional parameter</p> <p>MapPString(fn, col, n)</p>

View Source
var MapString func(func(string) string, []string) []string

Applies the given iterator function to each element of a []string and returns a new []string of the computed results.

View Source
var None func(fn, slice_or_map interface{}) bool

var None func(func(value interface{}) bool, interface{}) bool

View Source
var NoneInt func(func(value int) bool, []int) bool

NoneInt Returns true if all values in a []int pass the predicate truth test Predicate function accepts an int and returns a boolean

View Source
var NoneString func(func(value string) bool, []string) bool

NoneString Returns true if all values in a []string pass the predicate truth test Predicate function accepts a string and returns a boolean

View Source
var Partition func(fn interface{}, slice_or_map interface{}) ([]interface{}, []interface{})

Partition func(func(A, B) bool, []A []A) Applies the given iterator function to partition element of a collection (slice or map). If the collection is a Slice, the iterator function arguments are *value, index* If the collection is a Map, the iterator function arguments are *value, key* Iterator functions accept a value, and the index or key is an optional argument. Note: partition does not return a value, you may want un.Map var Partition func(func(value, i interface{}), interface{})

View Source
var PartitionInt func(func(value, i int), []int) ([]int, []int)

// PartitionInt // Applies the given iterator function to partition element of []int // Iterator function arguments are *value, index*

Functions

func MakeAny

func MakeAny(fn interface{})

MakeAny: implements a typed Each function in the form Each func(func(A, B), []A)

func MakeEach

func MakeEach(fn interface{})

MakeEach implements a typed Each function in the form Each func(func(A, B), []A)

func MakeEachP

func MakeEachP(fn interface{})

MakeEachP implements a typed Parallel-Each function in the form EachP func(func(A, B), []A)

func MakeEvery

func MakeEvery(fn interface{})

MakeEvery: implements a typed Each function in the form Each func(func(A, B), []A)

func MakeMap

func MakeMap(fn interface{})

MakeMap implements a typed Map function in the form Map func(func(A) C, []A) []C

func MakeNone

func MakeNone(fn interface{})

MakeNone: implements a typed Each function in the form Each func(func(A, B), []A)

func MakePMap

func MakePMap(fn interface{})

MakePMap implements a typed Parallel Map function in the form Map func(func(A) C, []A) []C

func MakePartition

func MakePartition(fn interface{})

MakePartition implements a typed Partition function in the form Partition func(func(A, B), []A)

func Maker

func Maker(fn interface{}, impl func(args []reflect.Value) (results []reflect.Value))

Maker takes a function pointer (fn) and implements it with the given reflection-based function implementation Internally uses reflect.MakeFunc

func SetWorkers

func SetWorkers(w int)

SetWorkers sets the number of workers used by the worker pools <p>This is a global default value</p> <p>If different worker pool sizes are required, use the optional worker argument when calling Parallel Implementations</p>

func ToI

func ToI(slice interface{}) []interface{}

ToI takes a slice and converts it to type []interface[]

func Valueize

func Valueize(values ...interface{}) []reflect.Value

Valueize takes a number of arguments and returns them as []reflect.Value

Types

This section is empty.

Jump to

Keyboard shortcuts

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