set

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2023 License: MIT Imports: 4 Imported by: 0

README

Go Reference

Functional Sets

The package provides a generic set type and functions that work with it. It is based on the orderedMap.OrderedMap type which means it is not safe for concurrent usage.

Get it

go get -u github.com/flowonyx/functional/set

Use it

import "github.com/flowonyx/functional/set"

Types

The only type here is Set[T comparable]. The interesting part is in the functions that interact with it.

Functions

  • NewSet creates a new Set. If a comparison function is provided, it is used in ordering the set.
  • Singleton creates a set of exactly one item.
    • This does not actually prevent you from adding more items later, so this is a way to create a set and add the first item in one call.
  • FromSlice creates a new Set from the items in the given slice.

Methods

  • ToSlice returns the values in the Set as a slice of items.
  • Add adds an item to the Set. If it already exists in the set, nothing changes.
  • Remove removes an item from the Set.
  • Equal tests if two sets are equal.
  • Contains tests whether item is present in the Set.
  • Exists tests whether any item in the Set matches the predicate.
  • Count returns the number of items in the Set.
  • IsEmpty test whether this is an empty set.
  • IsSubsetOf tests whether this Set is a subset of the Set passed as a parameter.
    • A Set is a subset of another Set if it only contains items that are also present in the other Set.
    • An empty Set is considered a subset of any other Set. This also considers equal sets to be subsets of each other.
  • IsProperSubsetOf tests whether this Set is a subset of the Set passed as a parameter.
    • A Set is a subset of another Set if it only contains items that are also present in the other Set.
    • An empty Set is not considered a subset of any other Set. Also, if the Sets are equal, neither is considered to a subset of the other.
  • IsProperSupersetOf tests whether this Set is a superset of the Set passed as a parameter.
    • A Set is a superset of another Set if it contains all items that are present in the other Set.
    • If either Set is empty or if they are equal, this will not consider the Set to be a superset.
  • IsSupersetOf tests whether this Set is a superset of the Set passed as a parameter.
    • A Set is a superset of another Set if it contains all items that are present in the other Set.
    • If the other Set is empty or if they are equal, this will consider the Set to be a superset.
  • IndexOf finds the index of the item within the Set.
    • The order of items within the Set is the order in which the items were added or sorted order if a comparison function was supplied when the Set was created.
  • Items returns the items in the Set as a slice.
  • Difference returns a Set containing the items that are only present in one Set or the other.
  • Intersect returns a Set containing the items that are present in both Sets.
  • Union returns a Set that contains all items that are present in either Set.
  • Filter returns a Set that contains only items from this Set that match a predicate function.
  • ForAll tests whether all items in the set match a predicate function.
  • Iter applies an action function to each item in the Set.
  • Iteri applies an action function to each item in the Set, also passing the index to the action.
  • Partition returns two Sets. The first Set are those items in this Set that match a predicate function. The second Set are those items in this Set that do not match the predicate.
  • Fold applies the folder function to each item in the Set until it reaches the final state.
  • FoldBack applies the folder function in reverse order to each item in the Set until it reaches the final state.
  • DifferenceMany finds the difference between all the sets.
  • ItersectMany finds the intersection between all the sets.
  • UnionMany finds the union between all the sets.
  • Map applies the mapping function to each item in the Set s.
  • MaxElement finds the largest item in the Set s.
  • MinElement finds the smallest item in the Set s.
  • MaxElementBy finds the largest item in the Set s using the return values from a projection function for comparison.
  • MinElementBy finds the smallest item in the Set s using the return values from a projection function for comparison.

Documentation

Overview

Package set provides a generic Set type.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fold

func Fold[T comparable, State any](folder func(State, T) State, initial State, s Set[T]) State

Fold applies the folder function to each item in the Set until it reaches the final state.

func FoldBack

func FoldBack[T comparable, State any](folder func(T, State) State, s Set[T], initial State) State

FoldBack applies the folder function in reverse order to each item in the Set until it reaches the final state.

func MaxElement

func MaxElement[T constraints.Ordered](s Set[T]) (T, error)

MaxElement finds the largest item in the Set s.

Example
s := FromSlice([]int{1, 2, 3, 4})
r, _ := MaxElement(s)
fmt.Println(r)
Output:

4

func MaxElementBy

func MaxElementBy[T comparable, R constraints.Ordered](projection func(T) R, s Set[T]) (T, error)

MaxElementBy finds the largest item in the Set s using the return values from projection for comparison.

Example
s := FromSlice([]int{1, 2, 3, 4})
r, _ := MaxElementBy(func(v int) int {
	if v < 3 {
		return v * 10
	}
	return v
}, s)
fmt.Println(r)
Output:

2

func MinElement

func MinElement[T constraints.Ordered](s Set[T]) (T, error)

MinElement finds the smallest item in the Set s.

Example
s := FromSlice([]int{1, 2, 3, 4})
r, _ := MinElement(s)
fmt.Println(r)
Output:

1

func MinElementBy

func MinElementBy[T comparable, R constraints.Ordered](projection func(T) R, s Set[T]) (T, error)

MinElementBy finds the smallest item in the Set s using the return values from projection for comparison.

Example
s := FromSlice([]int{1, 2, 3, 4})
r, _ := MinElementBy(func(v int) int {
	if v < 3 {
		return v * 10
	}
	return v
}, s)
fmt.Println(r)
Output:

3

Types

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

Set is a type keeps a set of values and allows set operations on them.

func DifferenceMany

func DifferenceMany[T comparable](sets ...Set[T]) Set[T]

DifferenceMany finds the difference between all the sets.

Example
s := FromSlice([]int{1, 2, 3, 4})
s2 := FromSlice([]int{1, 2, 3, 4, 5, 6})
s3 := FromSlice([]int{6, 7})
r := DifferenceMany(s, s2, s3)
fmt.Println(r.Items())
Output:

[5 7]

func FromSlice

func FromSlice[T comparable](input []T, lessFunc ...func(T, T) int) Set[T]

FromSlice creates a new Set from the items in the given slice.

Example
s := FromSlice([]string{"one", "two", "one"})
s2 := FromSlice([]int{1, 3, 4, 2, 4, 3, 2}, func(a, b int) int {
	if a < b {
		return -1
	}
	if b < a {
		return 1
	}
	return 0
})
fmt.Println(s.Items(), s2.Items())
Output:

[one two] [1 2 3 4]

func IntersectMany

func IntersectMany[T comparable](sets ...Set[T]) Set[T]

ItersectMany finds the intersection between all the sets.

Example
s := FromSlice([]int{1, 2, 3, 4})
s2 := FromSlice([]int{1, 2, 3, 4, 5, 6})
s3 := FromSlice([]int{3, 6, 7})
r := IntersectMany(s, s2, s3)
fmt.Println(r.Items())
Output:

[3]

func Map

func Map[T comparable, R comparable](mapping func(T) R, s Set[T]) Set[R]

Map applies the mapping function to each item in the Set s.

Example
s := FromSlice([]int{1, 2, 3, 4})
s2 := Map(func(v int) string { return strconv.Quote(strconv.Itoa(v)) }, s)
fmt.Println(s2.Items())
Output:

["1" "2" "3" "4"]

func NewSet

func NewSet[T comparable](lessFunc ...func(T, T) int) Set[T]

NewSet creates a new Set. If lessFunc is provided, it is used in ordering the set.

Example
less := func(a, b int) int {
	if a > b {
		return -1
	}
	if b < a {
		return 1
	}
	return 0
}
s := NewSet(less)
s.Add(1)
s.Add(2)
s.Add(4)
s.Add(3)
fmt.Println(s.Items())
Output:

[4 3 2 1]

func Singleton

func Singleton[T comparable](item T) Set[T]

Singleton creates a set of exactly one item. This does not actually prevent you from adding more items later, so this is a way to create a set and add the first item in one call.

Example
s := Singleton(1)
fmt.Println(s.Items())
Output:

[1]

func UnionMany

func UnionMany[T comparable](sets ...Set[T]) Set[T]

UnionMany finds the union between all the sets.

Example
s := FromSlice([]int{1, 2, 3, 4})
s2 := FromSlice([]int{1, 2, 3, 4, 5, 6})
s3 := FromSlice([]int{6, 7})
r := UnionMany(s, s2, s3)
fmt.Println(r.Items())
Output:

[1 2 3 4 5 6 7]

func (*Set[T]) Add

func (s *Set[T]) Add(item T)

Add adds an item to the Set. If it already exists in the set, nothing changes.

func (Set[T]) Contains

func (s Set[T]) Contains(item T) bool

Contains tests whether item is present in the Set.

Example
s := FromSlice([]string{"one", "two", "one"})
fmt.Println(s.Contains("two"), s.Contains("three"))
Output:

true false

func (Set[T]) Count

func (s Set[T]) Count() int

Count returns the number of items in the Set.

func (Set[T]) Difference

func (s Set[T]) Difference(set2 Set[T]) Set[T]

Difference returns a Set containing the items that are only present in one Set or the other.

Example
s := FromSlice([]string{"one", "two", "one"})
s2 := FromSlice([]string{"one", "two", "one", "three"})
r := s.Difference(s2)
fmt.Println(r.Items())
Output:

[three]

func (Set[T]) Equal

func (s Set[T]) Equal(s2 Set[T]) bool

Equal tests if two sets are equal.

Example
s := FromSlice([]string{"one", "two", "one"})
s2 := FromSlice([]string{"two", "one", "one"})
fmt.Println(s.Equal(s2))
Output:

true

func (Set[T]) Exists

func (s Set[T]) Exists(predicate func(T) bool) bool

Exists tests whether any item in the Set matches the predicate.

Example
s := FromSlice([]string{"one", "two", "one"})
r := s.Exists(func(s string) bool { return strings.HasPrefix(s, "o") })
r2 := s.Exists(func(s string) bool { return strings.HasPrefix(s, "s") })
fmt.Println(r, r2)
Output:

true false

func (Set[T]) Filter

func (s Set[T]) Filter(predicate func(T) bool) Set[T]

Filter returns a Set that contains only items from this Set that match the predicate.

Example
s := FromSlice([]int{1, 2, 3, 4})
r := s.Filter(func(i int) bool { return i%2 == 0 })
fmt.Println(r.Items())
Output:

[2 4]

func (Set[T]) ForAll

func (s Set[T]) ForAll(predicate func(T) bool) bool

ForAll tests whether all items in the set match the predicate.

func (Set[T]) IndexOf

func (s Set[T]) IndexOf(item T) int

IndexOf finds the index of the item within the Set. The order of items within the Set is the order in which the items were added or sorted order if a comparison function was supplied when this Set was created.

Example
s := NewSet(func(a, b int) int {
	if a < b {
		return -1
	}
	if b < a {
		return 1
	}
	return 0
})
s.Add(1)
s.Add(3)
s.Add(2)
fmt.Println(s.IndexOf(3))
Output:

2

func (Set[T]) Intersect

func (s Set[T]) Intersect(set2 Set[T]) Set[T]

Intersect returns a Set containing the items that are present in both Sets.

Example
s := FromSlice([]string{"one", "two", "one"})
s2 := FromSlice([]string{"one", "three"})
r := s.Intersect(s2)
fmt.Println(r.Items())
Output:

[one]

func (Set[T]) IsEmpty

func (s Set[T]) IsEmpty() bool

IsEmpty test whether this is an empty set.

func (Set[T]) IsProperSubsetOf

func (s Set[T]) IsProperSubsetOf(potentialSuperset Set[T]) bool

IsProperSubsetOf tests whether this Set is a subset of the Set passed as a parameter. A Set is a subset of another Set if it only contains items that are also present in the other Set. An empty Set is not considered a subset of any other Set. Also, if the Sets are equal, neither is considered to a subset of the other. If you do want these cases to be considered a subset, use IsSubsetOf instead.

Example
s := FromSlice([]string{"one", "two", "one"})
s2 := FromSlice([]string{"one", "two", "one", "three"})
s3 := FromSlice([]string{"one", "two", "one", "three"})
fmt.Println(s.IsProperSubsetOf(s2), s2.IsProperSubsetOf(s), s2.IsProperSubsetOf(s3))
Output:

true false false

func (Set[T]) IsProperSupersetOf

func (s Set[T]) IsProperSupersetOf(potentialSubset Set[T]) bool

IsProperSupersetOf tests whether this Set is a superset of the Set passed as a parameter. A Set is a superset of another Set if it contains all items that are present in the other Set. If either Set is empty or if they are equal, this will not consider the Set to be a superset. If you want these cases to be considered as supersets, use IsSupersetOf instead.

Example
s := FromSlice([]string{"one", "two", "one"})
s2 := FromSlice([]string{"one", "two", "one", "three"})
s3 := FromSlice([]string{"one", "two", "one", "three"})
fmt.Println(s.IsProperSupersetOf(s2), s2.IsProperSupersetOf(s), s2.IsProperSupersetOf(s3))
Output:

false true false

func (Set[T]) IsSubsetOf

func (s Set[T]) IsSubsetOf(potentialSuperset Set[T]) bool

IsSubsetOf tests whether this Set is a subset of the Set passed as a parameter. A Set is a subset of another Set if it only contains items that are also present in the other Set. An empty Set is considered a subset of any other Set. This also considers equal sets to be subsets of each other. If you do not want these cases to be considered a subset, use IsProperSubsetOf instead.

Example
s := FromSlice([]string{"one", "two", "one"})
s2 := FromSlice([]string{"one", "two", "one", "three"})
fmt.Println(s.IsSubsetOf(s2), s2.IsSubsetOf(s))
Output:

true false

func (Set[T]) IsSupersetOf

func (s Set[T]) IsSupersetOf(potentialSubset Set[T]) bool

IsSupersetOf tests whether this Set is a superset of the Set passed as a parameter. A Set is a superset of another Set if it contains all items that are present in the other Set. If the other Set is empty or if they are equal, this will consider the Set to be a superset. If you do not want these cases to be considered as supersets, use IsProperSupersetOf instead.

Example
s := FromSlice([]string{"one", "two", "one"})
s2 := FromSlice([]string{"one", "two", "one", "three"})
fmt.Println(s.IsSupersetOf(s2), s2.IsSupersetOf(s))
Output:

false true

func (Set[T]) Items

func (s Set[T]) Items() []T

Items returns the items in the Set as a slice.

func (Set[T]) Iter

func (s Set[T]) Iter(action func(T))

Iter applies the action to each item in the Set.

Example
s := FromSlice([]int{1, 2, 3, 4})
s.Iter(func(item int) { fmt.Print(item) })
Output:

1234

func (Set[T]) Iteri

func (s Set[T]) Iteri(action func(int, T))

Iteri applies the action to each item in the Set, also passing the index to the action.

Example
s := FromSlice([]int{1, 2, 3, 4})
s.Iteri(func(i, item int) { fmt.Print(i, item) })
Output:

0 11 22 33 4

func (Set[T]) Partition

func (s Set[T]) Partition(predicate func(T) bool) (trueSet Set[T], falseSet Set[T])

Partition returns two Sets. The first Set are those items in this Set that match the predicate. The second Set are those items in this Set that do not match the predicate.

Example
s := FromSlice([]int{1, 2, 3, 4})
t, f := s.Partition(func(i int) bool { return i%2 == 0 })
fmt.Println(t.Items(), f.Items())
Output:

[2 4] [1 3]

func (*Set[T]) Remove

func (s *Set[T]) Remove(item T)

Remove removes an item from the Set.

func (Set[T]) ToSlice

func (s Set[T]) ToSlice() []T

ToSlice returns the values in the Set as a slice of items.

func (Set[T]) Union

func (s Set[T]) Union(set2 Set[T]) Set[T]

Union returns a Set that contains all items that are present in either Set.

Example
s := FromSlice([]string{"one", "two", "one"})
s2 := FromSlice([]string{"one", "three"})
r := s.Union(s2)
fmt.Println(r.Items())
Output:

[one two three]

Jump to

Keyboard shortcuts

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