Skip to content

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

Closed as not planned
@sinhashubham95

Description

@sinhashubham95

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions