mapmap

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2026 License: BSD-3-Clause Imports: 2 Imported by: 0

README

License GoDoc

mapmap - a golang module to apply functions to maps and slices

Logo

Introduction

This module is inspired by perl's map() function:

our @even = map { if($_ % 2 == 0_) { $_ }} @source;

With the mapmap module you can extract slices of keys or values or maps out of maps or slices.

Example

Take a look at sample code.

Installation

Execute this to add the module to your project:

go get codeberg.org/scip/mapmap

LICENSE

This module is published under the terms of the BSD 3-Clause License. Please read the file LICENSE for details.

Author

Thomas von Dein <git |AT| daemon.de>

Documentation

Index

Constants

View Source
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

func (m *Mapper[K, V]) MapSliceKeys(f func(key K, value V) (K, bool)) []K

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

func (m *Mapper[K, V]) MapSliceKeysImmutable(f func(key K, value V) bool) []K

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

func (m *Mapper[K, V]) MapSliceValues(f func(key K, value V) (V, bool)) []V

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

func (m *Mapper[K, V]) MapSliceValuesImmutable(f func(key K, value V) bool) []V

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

func NewSlicer[V any](input []V) *Slicer[V]

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

func (s *Slicer[V]) FindSliceInSlice(ref []V, f func(value, ref V) bool) bool

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

func (s *Slicer[V]) MapSliceValues(f func(value V) (V, bool)) []V

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

func (s *Slicer[V]) MapSliceValuesImmutable(f func(value V) bool) []V

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}

func (*Slicer[V]) Values

func (m *Slicer[V]) Values() []V

Values returns a slice of the values of the underlying map.

## Examples

Retrieve the backing slice:

list := []int{1,2,3,4,5}
slicer := mapmap.NewSlicer(list)
backing := slicer.Values()

returns:

[]int{1,2,3,4,5}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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