Categorygithub.com/code-gorilla-au/mewl
repositorypackage
0.0.0-20231228020340-311539b80fa8
Repository: https://github.com/code-gorilla-au/mewl.git
Documentation: pkg.go.dev

# README

mewl

import "github.com/code-gorilla-au/mewl"

Index

func Chunk

func Chunk[T any](list []T, chunkSize int) [][]T

Chunk - creates a new nested slice with slice elements chunked

Example

list := []int{1, 2, 3, 4, 5, 6}

got := Chunk(list, 2)

fmt.Println(got)
// Output: [[1 2] [3 4] [5 6]]

Output

[[1 2] [3 4] [5 6]]

func Difference

func Difference[T comparable](lists ...[]T) []T

Difference - Creates an array of array values not included in the other given arrays.

Example

list1 := []int{1, 2, 3}
list2 := []int{2, 3, 4}
got := Difference(list1, list2)

fmt.Println(got)
// Output: [1 4]

Output

[1 4]

func Every

func Every[T any](list []T, fn PredicateSliceFunc[T]) bool

Every - tests whether all elements in the array pass the test implemented by the provided function.

Example

list := []KeyVal{
	{
		Key:   "foo",
		Value: "bar",
	},
	{
		Key:   "bin",
		Value: "baz",
	},
}
// Note: the original slice is passed into the predicate function
ok := Every(list, func(item KeyVal, i int, originalSlice []KeyVal) bool {
	return item.Value != ""
})

fmt.Println(ok)
// Output: true

Output

true

func Filter

func Filter[T any](list []T, fn PredicateFunc[T]) []T

Filter - return a new list of elements that return true on the predicate func.

Example

list := []KeyVal{
	{
		Key:   "foo",
		Value: "bar",
	},
	{
		Key:   "bin",
		Value: "baz",
	},
}

got := Filter(list, func(item KeyVal) bool {
	return item.Key == "foo"
})
fmt.Println(got)
// Output: [{foo bar}]

Output

[{foo bar}]

func Find

func Find[T any](list []T, fn PredicateSliceFunc[T]) (T, bool)

Find - returns the first element in the provided array that satisfies the provided testing function. If item is not found return nil value.

Example

list := []KeyVal{
	{
		Key:   "foo",
		Value: "bar",
	},
	{
		Key:   "bin",
		Value: "baz",
	},
}

got, ok := Find(list, func(item KeyVal, _ int, _ []KeyVal) bool {
	return item.Value == "bar"
})
fmt.Println(got, ok)
// Output: {foo bar} true

Output

{foo bar} true

func ForEach

func ForEach[T comparable](list []T, fn CallbackSliceFunc[T])

ForEach - iterates over the list and invokes the function on the element.

Example

list := []int{1, 1, 2}

total := 0

ForEach(list, func(item int, _ int, _ []int) {
	total += item
})

fmt.Println(total)
// Output: 4

Output

4

func Map

func Map[T comparable, K any](list []T, fn MapperFunc[T, K]) []K

Map - creates a new array populated with the results of calling a provided function on every element in the calling array.

Example

list := []KeyVal{
	{
		Key:   "foo",
		Value: "bar",
	},
	{
		Key:   "bin",
		Value: "baz",
	},
}

got := Map(list, func(item KeyVal) Val {
	return Val{
		Value: item.Value,
	}
})

fmt.Println(got)
// Output: [{bar} {baz}]

Output

[{bar} {baz}]

func MapClone

func MapClone[T comparable, K any](obj map[T]K) map[T]K

MapClone clones provided map

Example

obj := map[string]int{
	"hello": 1,
	"world": 2,
}
got := MapClone(obj)

fmt.Println(got)
// Output: map[hello:1 world:2]

Output

map[hello:1 world:2]

func MapKeys

func MapKeys[T comparable, K any](obj map[T]K) []T

Keys - return map's keys

Example

obj := map[int]string{
	1: "1",
	2: "flash",
}

got := MapKeys(obj)
sort.Ints(got)

fmt.Println(got)
// Output: [1 2]

Output

[1 2]

func MapOmitBy

func MapOmitBy[T comparable, K any](obj map[T]K, fn PredicateFunc[K]) map[T]K

MapOmitBy - returns a partial copy of an object omitting values based on a predicate func.

Example

obj := map[string]int{
	"hello": 1,
	"world": 2,
}
got := MapOmitBy(obj, func(item int) bool {
	return item == 1
})

fmt.Println(got)
// Output: map[world:2]

Output

map[world:2]

func MapOmitKeys

func MapOmitKeys[T comparable, K any](obj map[T]K, omits ...T) map[T]K

MapOmitKeys - returns a partial copy of an object omitting the keys specified. If the key does not exist, the property is ignored.

Example

obj := map[string]int{
	"hello": 1,
	"world": 2,
	"bin":   3,
}

got := MapOmitKeys(obj, "hello", "world")

fmt.Println(got)
// Output: map[bin:3]

Output

map[bin:3]

func MapPickBy

func MapPickBy[T comparable, K any](obj map[T]K, fn PredicateFunc[K]) map[T]K

MapPickKeys- Returns a partial copy of an object containing only the keys specified by a predicate func.

Example

obj := map[string]int{
	"hello": 1,
	"world": 2,
}
got := MapPickBy(obj, func(item int) bool {
	return item == 1
})

fmt.Println(got)
// Output: map[hello:1]

Output

map[hello:1]

func MapPickKeys

func MapPickKeys[T comparable, K any](obj map[T]K, picks ...T) map[T]K

MapPickKeys- Returns a partial copy of an object containing only the keys specified. If the key does not exist, the property is ignored.

Example

obj := map[int]string{
	1: "hello",
	2: "world",
	3: "bin",
}

got := MapPickKeys(obj, 1)

fmt.Println(got)
// Output: map[1:hello]

Output

map[1:hello]

func MapValues

func MapValues[T comparable, K any](obj map[T]K) []K

MapValues - return map's values

Example

obj := map[int]string{
	1: "hello",
	2: "world",
}

got := MapValues(obj)
sort.Strings(got)

fmt.Println(got)
// Output: [hello world]

Output

[hello world]

func Reverse

func Reverse[T comparable](list []T) []T

Reverse - return slice in reverse order

Example

list := []int{1, 2, 3}

got := Reverse(list)

fmt.Println(got)
// Output: [3 2 1]

Output

[3 2 1]

func Some

func Some[T any](list []T, fn PredicateSliceFunc[T]) bool

Some - Checks if predicate returns truthy for any element of a list. Iteration is stopped once predicate returns truthy

Example

list := []int{1, 2, 3}

got := Some(list, func(item, index int, slice []int) bool {
	return item == 2
})

fmt.Println(got)
// Output: true

Output

true

func Union

func Union[T comparable](lists ...[]T) []T

Union - merges two lists into a slice with no duplicates composed of the elements of each list.

Example

id := "1"
value := "some value"
lists := [][]KeyVal{
	{
		{
			Key:   id,
			Value: value,
		},
	},
	{
		{
			Key:   id,
			Value: value,
		},
	},
}

got := Union(lists...)

fmt.Println(got)
// Output: [{1 some value}]

Output

[{1 some value}]

func Unique

func Unique[T comparable](list []T) []T

Unique - return unique items from a provided list

Example

list := []KeyVal{
	{
		Key:   "foo",
		Value: "bar",
	},
	{
		Key:   "foo",
		Value: "bar",
	},
	{
		Key:   "bin",
		Value: "baz",
	},
}

got := Unique(list)

fmt.Println(got)
// Output: [{foo bar} {bin baz}]

Output

[{foo bar} {bin baz}]

func Without

func Without[T comparable](list []T, omit ...T) []T

Without - Creates an array excluding all given values

Example

list := []KeyVal{
	{Key: "foo", Value: "bar"},
	{Key: "bin", Value: "baz"},
}

got := Without(list, KeyVal{Key: "bin", Value: "baz"})

fmt.Println(got)
// Output: [{foo bar}]

Output

[{foo bar}]

type AnyFunc

AnyFunc - function that receives n arguments

type AnyFunc[T any] func(args ...T) T

func Before

func Before[T any](n int, fn AnyFunc[T]) AnyFunc[T]

Creates a function that invokes func while it's called less than n times. Subsequent calls to the created function return the result of the last func invocation.

type CallbackSliceFunc

CallbackSliceFunc - function that receives an index of the current item, current item being iterated on and the array Every was called upon.

type CallbackSliceFunc[T any] func(item T, index int, slice []T)

type ComposeFunc

ComposeFunc - function that receives an input and returns an input of the same type.

type ComposeFunc[T any] func(T) T

func Once

func Once[T any](fn ComposeFunc[T]) ComposeFunc[T]

Once - Creates a function that is restricted to invoking func once. Repeat calls to the function return the value of the first invocation

func Pipe

func Pipe[T any](fns ...ComposeFunc[T]) ComposeFunc[T]

Pipe - left to right function composition

func Reduce

func Reduce[T any](list []T, fn func(prev T, next T) T) ComposeFunc[T]

Reduce - executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value

Example

list := []int{1, 2, 3}

add := Reduce(list, func(prev, current int) int {
	return prev + current
})

got := add(1)

fmt.Println(got)
// Output: 7

Output

7

type MapperFunc

MapperFunc - transform function that receives an input and returns a new type.

type MapperFunc[T any, K any] func(item T) K

type PredicateFunc

PredicateFunc - function that receives an input and returns a boolean value.

type PredicateFunc[T any] func(item T) bool

type PredicateSliceFunc

PredicateSliceFunc - function that receives an index of the current item, current item being iterated on and the array Every was called upon.

type PredicateSliceFunc[T any] func(item T, index int, slice []T) bool

type Txn

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

type testState struct {
	Name string
}

state := testState{Name: "hello"}

txn := NewTxn(state)
result, err := txn.Step(
	func(ts testState) (testState, error) {
		ts.Name = "world"
		return ts, nil
	},
	func(ts testState) (testState, error) {
		ts.Name = "failed"
		return ts, nil
	},
).Run()
if err != nil {
	panic(err)
}

fmt.Println(result.Name)
// Output: world

Output

world

func NewTxn

func NewTxn[T any](state T, opts ...TxnOpts[T]) *Txn[T]

NewTxn - creates a new transaction. Txn implements a basic saga pattern which manages state between steps and rollback.

func (*Txn[T]) Run

func (t *Txn[T]) Run() (T, error)

Errors caught within the steps and rollback funcs will be able to be unwrapped and inspected using Unwrap() []error.

func (*Txn[T]) Step

func (t *Txn[T]) Step(handler TxnFunc[T], rollback TxnFunc[T]) *Txn[T]

Step - adds a step to the transaction workflow. All steps must have a handler and a rollback func.

type TxnFunc

type TxnFunc[T any] func(T) (T, error)

type TxnOpts

type TxnOpts[T any] func(*Txn[T])

func TxnOptFailFast

func TxnOptFailFast[T any]() TxnOpts[T]

TxnOptFailFast - if set to true, the transaction will stop at the first error.

func TxnOptVerbose

func TxnOptVerbose[T any]() TxnOpts[T]

TxnOptVerbose - if set to true, the transaction will log out the steps as they are run.

type TxnState

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

type TxnStep

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

Generated by gomarkdoc