Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: slices: implement common utility functions for cardinality, transformation, filtering and venn operations #69529

Closed
sinhashubham95 opened this issue Sep 19, 2024 · 2 comments
Labels
Milestone

Comments

@sinhashubham95
Copy link

Proposal Details

Description

This proposal enhances the slices package(slices/slices.go) by adding the essential functions around cardinality, transformation, filtering and venn operations which are as follows.

  • Cardinality: This is used to get the number of occurrences of the given element in the provided slice.
  • CardinalityFunc: This is used to get the number of occurrences of the element satisfying the function in the provided slice.
  • CardinalityMap: This is used to get a Map mapping each unique element in the given slice to an Integer representing the number of occurrences of that element in the slice.
  • Transform: This is used to transform all elements of the input slice by the given transformer.
  • Filter: This is used to filter the slice with elements that satisfy the predicate.
  • Disjunction: This is used to get a slice containing the exclusive disjunction (symmetric difference) of the given slices. This means the elements which are in either one of the slice but not in both.
  • Intersection: This is used to get a slice containing the intersection of the given slices. This means the elements which are in both the given slices.
  • Union: This is used to do the union of the 2 provided collections. The cardinality of each element in the returned collection will be equal to the maximum of that element in the given 2 collections.
  • Subtract: This is used to get a new slice containing s1 - s2 i.e. all elements of s2 removed from s1.

Motivation

These new functions added, are the common functions available in a lot of languages like Java, Python, etc in their standard packages, and are used very commonly in Go based applications, though implemented repeatedly everywhere. Including these functions in the Go standard slices package, provides robustness and reliability around such use cases.

Design

The functions will be added to the existing slices package, ensuring they are easy to use and can be integrated seamlessly. Each of these functions will handle for nil or empty slices, instead of a panic in such scenarios.

Examples

  • Cardinality:
a := []int{1,2,3,2,1}
count := slices.Cardinality(a, 2)   // 2 is the result
  • CardinalityFunc:
a := []int{1,2,3,2,1}
f := func(a int) bool { return a == 2 }
count := slices.CardinalityFunc(a, f)   // 2 is the result
  • CardinalityMap:
a := []int{1,2,3,2,1}
m := slices.CardinalityMap(a)   // {1: 2, 2: 2, 3: 1} is the result
  • Transform:
a := []int{1,2,3,2,1}
t := func(a int) string { return fmt.Sprintf("%d", a) }
b := slices.Transform(a, t) // ["1", "2", "3", "2", "1"] is the result
  • Filter:
a := []int{1,2,3,2,1}
f := func(a int, i int) { return a == 1 || i > 2 }
b := slices.Filter(a, f)    // [1, 2, 1] is the result
  • Disjunction:
a := []int{1,2,1,3,1,2}
b := []int{1,2,2,3}
c := slices.Disjunction(a, b)   // [1, 1] is the result
  • Intersection:
a := []int{1,2,1,3,1,2}
b := []int{1,2,2,3}
c := slices.Intersection(a, b)   // [1, 2, 3, 2] is the result
  • Union:
a := []int{1,2,1,3,1,2}
b := []int{1,2,2,3}
c := slices.Union(a, b)   // [1, 2, 1, 3, 1, 2] is the result
  • Subtract:
a := []int{1,2,1,3,1,2}
b := []int{1,2,2,3}
c := slices.Subtract(a, b)   // [1, 1] is the result
@gopherbot gopherbot added this to the Proposal milestone Sep 19, 2024
@gophun
Copy link

gophun commented Sep 19, 2024

Cardinality, Disjunction, Intersection, Union, Subtract belong in the container/set proposal: #69230

Transform, Filter in the x/exp/xiter proposal: #61898

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants