smap

package module
v0.0.0-...-0bbc2ec Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2025 License: MIT Imports: 1 Imported by: 0

README

smap | Slice Mapper

type conversion utilities for slices that smaps 🖐️

Installation

go get github.com/ggpls/smap

Usage

Number Type Conversions

Convert slices between different numeric types (int, float32, float64, etc.):

package main

import "github.com/ggpls/smap"

func main() {
    // Convert []int to []float64
    islice := []int{1, 2, 3, 4, 5}
    f64slice := smap.N[int, float64](islice)

    // Convert []float32 to []int
    f32slice := []float32{1.1, 2.2, 3.3}
    islice = smap.N(f32slice)
}
Byte/String Type Conversions

Convert between []byte and string slices:

package main

import "github.com/ggpls/smap"

func main() {
    // convert []string to [][]byte
    strs := []string{"hello", "world"}
    byteses := smap.S[string, []byte](strs)

    // and back
    strs = smap.S(byteses)
}
Custom Function Mapping

Transform slices using custom functions with the F mapper:

package main

import (
    "strings"
    "github.com/ggpls/smap"
)

func main() {
    // Using standard library functions
    words := []string{"hello", "world"}
    upperWords := smap.F(words, strings.ToUpper)
    // Result: ["HELLO", "WORLD"]

    // Using custom transformation functions
    numbers := []int{1, 2, 3, 4}
    squared := smap.F(numbers, func(x int) int {
        return x * x
    })
    // Result: [1, 4, 9, 16]

    // Type conversion with formatting
    ids := []int{1, 2, 3}
    formatted := smap.F(ids, func(id int) string {
        return fmt.Sprintf("ID-%d", id)
    })
    // Result: ["ID-1", "ID-2", "ID-3"]
}

Type Constraints

The package uses three main approaches:

  • N: Supports all integer and floating-point types (using constraints.Integer | constraints.Float)
  • S: Supports []byte and string types
  • F: Supports any types that satisfy the provided transformation function

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func F

func F[T any, R any](from []T, f func(T) R) []R
Example (CustomTransform)
package main

import (
	"fmt"

	"github.com/ggpls/smap"
)

func main() {
	// Custom transformation function
	numbers := []int{1, 2, 3, 4}
	squared := smap.F(numbers, func(x int) int {
		return x * x
	})
	fmt.Printf("%v -> %v\n", numbers, squared)
}
Output:

[1 2 3 4] -> [1 4 9 16]
Example (StringTransform)
package main

import (
	"fmt"
	"strings"

	"github.com/ggpls/smap"
)

func main() {
	// Transform strings to uppercase
	words := []string{"hello", "world"}
	upper := smap.F(words, strings.ToUpper)
	fmt.Printf("%q -> %q\n", words, upper)
}
Output:

["hello" "world"] -> ["HELLO" "WORLD"]
Example (TypeConversion)
package main

import (
	"fmt"

	"github.com/ggpls/smap"
)

func main() {
	// Convert integers to their string representation
	numbers := []int{42, 100, 999}
	formatted := smap.F(numbers, func(n int) string {
		return fmt.Sprintf("Number: %d", n)
	})
	fmt.Printf("%v -> %q\n", numbers, formatted)
}
Output:

[42 100 999] -> ["Number: 42" "Number: 100" "Number: 999"]

func N

func N[T NType, R NType](from []T) []R
Example (Float32ToInt)
package main

import (
	"fmt"

	"github.com/ggpls/smap"
)

func main() {
	floats32 := []float32{1.9, 2.2, 3.7, 4.1, 5.9}
	ints := smap.N[float32, int](floats32)
	fmt.Printf("%v -> %v\n", floats32, ints)
}
Output:

[1.9 2.2 3.7 4.1 5.9] -> [1 2 3 4 5]
Example (Float64ToFloat32)
package main

import (
	"fmt"

	"github.com/ggpls/smap"
)

func main() {
	float64s := []float64{1.123456789, 2.123456789}
	float32s := smap.N[float64, float32](float64s)
	fmt.Printf("%.6f -> %.6f\n", float64s[0], float32s[0])
}
Output:

1.123457 -> 1.123457
Example (Int32ToInt64)
package main

import (
	"fmt"

	"github.com/ggpls/smap"
)

func main() {
	int32s := []int32{100, 200, 300}
	int64s := smap.N[int32, int64](int32s)
	fmt.Printf("%v -> %v\n", int32s, int64s)
}
Output:

[100 200 300] -> [100 200 300]
Example (IntToFloat64)
package main

import (
	"fmt"

	"github.com/ggpls/smap"
)

func main() {
	ints := []int{1, 2, 3, 4, 5}
	floats64 := smap.N[int, float64](ints)
	fmt.Printf("%v -> %v\n", ints, floats64)
}
Output:

[1 2 3 4 5] -> [1 2 3 4 5]

func S

func S[T BType, R BType](from []T) []R
Example (BytesToString)
package main

import (
	"fmt"

	"github.com/ggpls/smap"
)

func main() {
	byteSlices := [][]byte{
		[]byte("Golang"),
		[]byte("Rules"),
	}
	strings := smap.S[[]byte, string](byteSlices)
	fmt.Printf("%q\n", strings)
}
Output:

["Golang" "Rules"]
Example (StringToBytes)
package main

import (
	"fmt"

	"github.com/ggpls/smap"
)

func main() {
	strings := []string{"Hello", "World"}
	bytes := smap.S[string, []byte](strings)
	// Print length of each byte slice to avoid platform-specific byte representations
	fmt.Printf("%q -> [%d, %d] (byte lengths)\n", strings, len(bytes[0]), len(bytes[1]))
}
Output:

["Hello" "World"] -> [5, 5] (byte lengths)
Example (Utf8Handling)
package main

import (
	"fmt"

	"github.com/ggpls/smap"
)

func main() {
	// Test UTF-8 round trip conversion
	original := []string{"café", "über", "🚀"}
	bytes := smap.S[string, []byte](original)
	result := smap.S[[]byte, string](bytes)
	fmt.Printf("%q -> %q\n", original, result)
}
Output:

["café" "über" "🚀"] -> ["café" "über" "🚀"]

Types

type BType

type BType interface {
	[]byte | string
}

type NType

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

Jump to

Keyboard shortcuts

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