Skip to content
/ typez Public

GoLang types extension library

Notifications You must be signed in to change notification settings

intezya/typez

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Typez

Generic data structures and utilities for Go.

Installation

go get github.com/intezya/typez

Iterator

Lazy iterator with method chaining for functional-style data processing.

Creating Iterators

// From slice
iter := typez.IteratorFromSlice([]int{1, 2, 3, 4, 5})

// From iter.Seq
seq := slices.Values([]string{"a", "b", "c"})
iter := typez.IteratorFromSeq(seq)

Methods

// Transform elements
iter.Map(func(x int) int { return x * 2 })

// Filter elements  
iter.Filter(func(x int) bool { return x > 3 })

// Reverse order
iter.Reverse()

// Execute for each element
iter.Each(func(x int) { fmt.Println(x) })

// Collect to slice
result := iter.Collect()

// Count elements
count := iter.Count()
count := iter.CountWithPredicate(func(x int) bool { return x > 5 })

Type Conversion

// Map to different type
stringIter := typez.MapIterator(intIter, func(x int) string {
return strconv.Itoa(x)
})

Example

typez.IteratorFromSlice([]int{1, 2, 3, 4}).
Reverse().
Map(func(x int) int { return x * x }).
Filter(func(x int) bool { return x%2 == 0 }).
Each(func(x int) { fmt.Println(x) })
// Output: 16, 4

Set

Generic set implementation with common set operations.

Creating Sets

// Empty set
set := typez.NewSet[int]()

// From slice
set := typez.SetFromSlice([]string{"a", "b", "c"})

Basic Operations

set.Add("value")
set.Remove("value")
exists := set.Contains("value")
size := set.Size()
values := set.Values()

Set Operations

set1 := typez.SetFromSlice([]int{1, 2, 3})
set2 := typez.SetFromSlice([]int{3, 4, 5})

// Union (modifies set1)
set1.Union(set2) // set1 now contains {1, 2, 3, 4, 5}

// Intersection (modifies set2)
set1.Intersect(set2)

// Clone
clone := set.Clone()

// Comparison
equal := set1.Equal(set2)
isSubset := set1.IsSubsetOf(set2)
isSuperset := set1.IsSupersetOf(set2)

Integration with Iterator

set := typez.SetFromSlice([]int{1, 2, 3, 4, 5})
result := set.Iter().
Filter(func(x int) bool { return x > 2 }).
Map(func(x int) int { return x * 2 }).
Collect()

OneOf

Generic union type that can hold one of two possible typez.

Creating OneOf

// Empty OneOf
oneOf := typez.NewOneOf[string, int]()

// Set values
oneOf.SetT1("hello")
oneOf.SetT2(42)

Getting Values

// Check which type is present
isT1, isT2 := oneOf.Present()

// Get values with presence check
if str, ok := oneOf.GetT1(); ok {
    fmt.Println("String:", str)
}

if num, ok := oneOf.GetT2(); ok {
    fmt.Println("Number:", num)
}

Example

oneOf := typez.NewOneOf[string, error]()

// Success case
oneOf.SetT1("operation successful")
if result, ok := oneOf.GetT1(); ok {
    fmt.Println(result)
}

// Error case
oneOf.SetT2(errors.New("something went wrong"))
if err, ok := oneOf.GetT2(); ok {
    fmt.Println("Error:", err)
}

Requirements

  • Go 1.22+

About

GoLang types extension library

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages