Golang 1.18+ Generics implementation of Set methods

Golang Generics: Set

A golang 1.18+ implementation of Set using Go generics

Installation

$ go get -u github.com/chrispappas/golang-generics-set

Quick Start

https://go.dev/play/p/gxB1xreWFwM

package main

import (
	"fmt"

	"github.com/chrispappas/golang-generics-set/set"
)

func main() {
	setA := set.FromSlice([]int{1, 2, 3, 1, 2, 3})
	setB := set.FromSlice([]int{3, 4, 5})

	// remember, sets are unordered, so the output might be too!
	fmt.Println("setA", setA.Values())
	fmt.Println("setB", setB.Values())

	fmt.Println("A.Len() == len(A)", setA.Len(), len(setA))
	fmt.Println("A.Has(1), A.Has(999)", setA.Has(1), setA.Has(999))

	// add 8, 9, 10 to A (1, 2, 3, 8, 9, 10)
	setA.Add([]int{8, 9, 10}...)
	fmt.Println("Added 8, 9, 10", setA.Values())

	// delete 8, 9, 10 from A (1, 2, 3)
	setA.Delete([]int{8, 9, 10}...)
	fmt.Println("Removed 8, 9, 10", setA.Values())

	// combination of both sets (1, 2, 3, 4, 5)
	fmt.Println("Union A ∪ B", setA.Union(setB).Values())

	// things that exist in both sets (3)
	fmt.Println("Intersection A ∩ B", setA.Intersection(setB).Values())

	// produces the things in setA that are not in setB (1, 2)
	fmt.Println("Difference A-B", setA.Difference(setB).Values())
	// and vice versa (4, 5)
	fmt.Println("Difference B-A", setB.Difference(setA).Values())

	// things that are _not_ in both sets - opposite of Intersection (1, 2, 4, 5)
	fmt.Println("Symm Diff A △ B", setA.SymmetricalDifference(setB).Values())

	// Clone A as C, edit C, C != A
	setC := setA.Clone()
	setC.Add(999)
	fmt.Println("A, C", setA.Values(), setC.Values())

	// run a callback on each element of A, multiplying by 10
	setX := make(set.Set[int], len(setA))
	setA.ForEach(
		func(v int) {
			setX.Add(v * 10)
		},
	)
	fmt.Println("A values * 10", setX.Values())
}

License

golang-generics-set is released under the MIT License.

Owner
Chris Pappas
Senior Prod Eng @ Shopify
Chris Pappas
Similar Resources

Package truthy provides truthy condition testing with Go generics

Package truthy provides truthy condition testing with Go generics

Truthy Truthy is a package which uses generics (Go 1.18+) to create useful boolean tests and helper functions. Examples // truthy.Value returns the tr

Nov 11, 2022

Go Library for Competitive Programming with Generics

Go Library for Competitive Programming with Generics Go used to be a difficult language to use for competitive programming. However, with the introduc

Dec 21, 2022

Extended library functions using generics in Go.

Just few extended standard library functions for Golang using generics.

Dec 16, 2021

A library that provides Go Generics friendly "optional" features.

go-optional A library that provides Go Generics friendly "optional" features. Synopsis some := optional.Some[int](123) fmt.Printf("%v\n", some.IsSome(

Dec 20, 2022

experimental promises in go1.18 with generics

async go a prototype of "promises" in go1.18. note: this is just an experiment used to test alternate patterns for dealing with asynchronous code in g

Dec 23, 2022

Go 1.18 generics use cases and examples

Go 1.18 generics use cases What are generics? See Type Parameters Proposal. How to run the examples? As of today, gotip is the simplest way to run the

Jan 10, 2022

Functional tools in Go 1.18 using newly introduced generics

functools functools is a simple Go library that brings you your favourite functi

Dec 5, 2022

Benchmarks to compare Go Generics

This is a collection of various sorts implemnted both as []int only and as const

Dec 8, 2022

Utility library that uses Go generics mechanism

golang-generics-util Utility library that explores Go generics (1.18) xsync Sync

Dec 11, 2022
An application written in Go to generate fractals like the Mandelbrot set and the Julia set.
An application written in Go to generate fractals like the Mandelbrot set and the Julia set.

Fractals An application written in Go to generate fractals like the Mandelbrot set and the Julia set. Screenshots Mandelbrot set Julia set Prerequisit

May 9, 2022
Robust & Easy to use struct mapper and utility methods for Go

go-model Robust & Easy to use model mapper and utility methods for Go struct. Typical methods increase productivity and make Go development more fun ?

Dec 30, 2022
This is a small utility that finds unused exported Go symbols (functions, methods ...) in Go

This is a small utility that finds unused exported Go symbols (functions, methods ...) in Go. For all other similar use cases

Nov 8, 2022
Code Generation for Functional Programming, Concurrency and Generics in Golang

goderive goderive derives mundane golang functions that you do not want to maintain and keeps them up to date. It does this by parsing your go code fo

Dec 25, 2022
Experimenting with golang generics to implement functional favorites like filter, map, && reduce.

funcy Experimenting with golang generics to implement functional favorites like filter, map, && reduce. 2021-12 To run the tests, you need to install

Dec 29, 2021
A collection of functional operators for golang with generics

fn fn is a collection of go functional operators with generics Getting Started P

Jul 8, 2022
Higher Order Functions using Golang Generics (Hack Days 2022)

hoff: Higher Order Functions (and Friends) Golang 1.18+ implementations of common methods/data structures using Go Generics Requirements Go 1.18 or ne

Jan 4, 2023
Experiments with Go generics

generics Quick experiments with Go generics algebra, a generic square root function for float, complex and and rational. future, a concurrent cache ("

Dec 31, 2022
Example code for Go generics

go-generics-example Example code for Go generics. Usage $ go build -gcflags=-G=3 Requirements Go 1.17 or later Advertise Go 言語にやってくる Generics は我々に何をも

Dec 30, 2022
Collection of unusual generics usecases in Go

Unusual Generics Type parameters or Generics in Go designed to reduce boilerplate for container data types like lists, graphs, etc. and functions like

Dec 14, 2022