Documentation
¶
Index ¶
- Constants
- type Mapper
- func (m *Mapper[K, V]) Keys() []K
- func (m *Mapper[K, V]) Map() map[K]V
- func (m *Mapper[K, V]) MapSliceKeys(f func(key K, value V) (K, bool)) []K
- func (m *Mapper[K, V]) MapSliceKeysImmutable(f func(key K, value V) bool) []K
- func (m *Mapper[K, V]) MapSliceValues(f func(key K, value V) (V, bool)) []V
- func (m *Mapper[K, V]) MapSliceValuesImmutable(f func(key K, value V) bool) []V
- func (m *Mapper[K, V]) Values() []V
- type Slicer
Constants ¶
const VERSION = "0.0.2"
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Mapper ¶
type Mapper[K comparable, V any] struct { // contains filtered or unexported fields }
Mapper is the backend object for generic map operations
func NewMapper ¶
func NewMapper[K comparable, V any](input map[K]V) *Mapper[K, V]
NewMapper returns a new Mapper. Parameter input is a map.
## Examples
mp := map[string]int{"huh","hah","foo"}
mapper := mapmap.NewMapper(mp)
func (*Mapper[K, V]) Keys ¶
func (m *Mapper[K, V]) Keys() []K
Keys returns a slice of the keys of the underlying map.
## Examples
Retrieve the backing map keys:
mp := map[string]int{"Hund": 2, "Hanne": 4, "Foo": 8}
mapper := mapmap.NewMapper(mp)
keys := mapper.Keys()
returns:
[]string{"Hund", "Hanne", "Foo"}
func (*Mapper[K, V]) Map ¶
func (m *Mapper[K, V]) Map() map[K]V
Map returns the underlying map.
## Examples
Retrieve the backing map values:
mp := map[string]int{"Hund": 2, "Hanne": 4, "Foo": 8}
mapper := mapmap.NewMapper(mp)
backing := mapper.Map()
returns:
map[string]int{"Hund": 2, "Hanne": 4, "Foo": 8}
func (*Mapper[K, V]) MapSliceKeys ¶
MapSliceKeys returns a new slice of keys matching the supplied function. The function must return a key of the underlying map key type and a boolean indicating a match. The key might be modified.
## Examples
Get a slice of keys matching `h` and lower case them
mp := map[string]int{"Huh": 2, "Hah": 4, "Foo": 8}
mapper := mapmap.NewMapper(mp)
lc := mapper.MapSliceKeys(func (key string, val int) (string, bool){
if strings.Contains(key, "h") {
return strings.ToLower(key), true
}
return "", false
})
returns:
[]string{"huh","hah"}
func (*Mapper[K, V]) MapSliceKeysImmutable ¶
MapSliceKeysImmutable returns a new slice of keys matching the supplied function. The function must return a boolean indicating a match. The key cannot be modified.
## Examples
Get a slice of keys matching case insensitively `h`
mp := map[string]int{"Hund": 2, "Hanne": 4, "Foo": 8}
mapper := mapmap.NewMapper(mp)
lc := mapper.MapSliceKeys(func (key string, val int) (string, bool){
return key, strings.Contains(strings.ToLower(key), "h")
})
returns:
[]string{"Hund","Hanne"}
func (*Mapper[K, V]) MapSliceValues ¶
MapSliceValues returns a new slice of values matching the supplied function. The function must return a value of the underlying map value type and a boolean indicating a match. The value might be modified.
## Examples
Get a slice of values larger than 5 and multiply each with ten:
mp := map[string]int{"huh": 2, "hah": 4, "foo": 8}
mapper := mapmap.NewMapper(mp)
large := mapper.MapSliceValues(func(key string, val int) (string, bool) {
return val*10, val>5
})
returns:
[]int{80}
func (*Mapper[K, V]) MapSliceValuesImmutable ¶
MapSliceValuesImmutable returns a new slice of values matching the supplied function. The function must return a boolean indicating a match. The value cannot be modified.
## Examples
Get a slice of values larger than 5
mp := map[string]int{"huh": 2, "hah": 4, "foo": 8}
mapper := mapmap.NewMapper(mp)
large := mapper.MapSliceValues(func(key string, val int) (bool) {
return val>5
})
returns:
[]int{8}
func (*Mapper[K, V]) Values ¶
func (m *Mapper[K, V]) Values() []V
Values returns a slice of the values of the underlying map.
## Examples
Retrieve the backing map values:
mp := map[string]int{"Hund": 2, "Hanne": 4, "Foo": 8}
mapper := mapmap.NewMapper(mp)
values := mapper.Keys()
returns:
[]int{2,4,8}
type Slicer ¶
type Slicer[V any] struct { // contains filtered or unexported fields }
Slicer is the backend object for generic slice operations
func NewSlicer ¶
NewSlicer returns a new Slicer. Parameter input is a slice.
## Example
list := []int{1,2,3,4,5}
slicer := mapmap.NewSlicer(list)
func (*Slicer[V]) FindSliceInSlice ¶
FindSliceInSlice returns true if one or more elements of the ref slice can be found in the input slice using the function f.
## Examples
list := []int{1,2,3,4,5}
slicer := mapmap.NewSlicer(list)
slicer.FindSliceInSlice([]int{1,3}, func(value, find int) bool {
return value == find
})
returns:
true
func (*Slicer[V]) MapSliceValues ¶
MapSliceValues returns a new slice of values matching the supplied function. The function must return a value of the underlying slice type and a boolean indicating a match. The value might be modified.
## Examples
Get new slice with all ints doubled:
list := []int{1,2,3,4,5}
slicer := mapmap.NewSlicer(list)
double := slicer.MapSliceValues(func(val int) (val, bool) {
return val*2, true
})
returns:
[]int{2,4,6,8,10}
func (*Slicer[V]) MapSliceValuesImmutable ¶
MapSliceValuesImmutable returns a new slice of values matching the supplied function. The function must return a boolean indicating a match. The value cannot be modified.
## Examples
Get even numbers of an int slice:
list := []int{1,2,3,4,5}
slicer := mapmap.NewSlicer(list)
even := slicer.MapSliceValuesImmutable(func(val int) (bool) {
return val%2==0
})
returns:
[]int{2,4}