sets

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2023 License: MIT Imports: 3 Imported by: 0

README

This is what an idiomatic Go set might look like.

Note the lack of exported receiver methods.

Go is not Java.

Documentation

Overview

Package sets is a package that represents as idiomatic `Set` implemenation as I can conceive of as of the time of the last commit. Most `Set` implementations focus on a more C++/Java/C# false "OOP" approach. Defining an `interface` that some `struct` then has a bunch of receiver methods that manipulate the internal state of the `Set` and ensure its contractual behavior.

Receiver methods on value types, is not the most idiomatic Go. So my approach is package level functions that apply the `Set` logic and maintain high cohesion and loose coupling from my implementation details.

This way client code does not get tightly coupled with the interface and implementation. This is mainly because `Set` semantics are only really enforced when building or adding to the `Set`. Thus, once you finish creating the `Set` you can easily call `ToSlice()` get an insertion order slice of the values and used them other code that expects `[]T` and not `Set`.

There is no `SortedSet` implementation because that mixes concerns, a `Set` concerns are simple, no duplicates. If you need a `SortedSet` then you build your `Set` and then sort the resulting `ToSlice()` results.

I am also trying to implement some ideas from Alan Kay.

"OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things."

That is why there is a Set interface with only one exported function, the one that lets you retrieve the `Set` as a `slice`. That is why the `set` struct that tracks state nothing is exported. Late binding of all things is really hard in a staticly typed language, so we have to do with Generics.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add[T any](s Set[T], ts ...T)

Add this mutates the internal state of the Set s

func ContainsExactly

func ContainsExactly[T any](s1 Set[T], s2 Set[T]) bool

ContainsExactly tests to see if the Sets contain exactly the same things in any order. This can be used as a basic "equals" test as well.

func Remove

func Remove[T any](s Set[T], ts ...T)

Remove this mutates the internal state of the Set

Types

type Set

type Set[T any] interface {
	ToSlice() []T
	// contains filtered or unexported methods
}

Set unique list of instances of a type T is the type that should be contrained to being unique C is the type of unsigned int that should be used for counting virtual duplicates

func Intersection

func Intersection[T any](s1 Set[T], s2 Set[T]) Set[T]

Intersection returns a new Set with only the items that exist in both Sets

func New

func New[T any](s ...T) Set[T]

New creates a new Set using the identity.HashStructIdentity function for determining uniqueness of non-comparable T any

func Union

func Union[T any](s1 ...Set[T]) Set[T]

Union returns a new Set with all the items from all the Sets

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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