Skip to content

Instantly share code, notes, and snippets.

@ninmonkey
Created September 29, 2022 19:32
Show Gist options
  • Save ninmonkey/f9a6baa8c1adc11327de17ba87786a38 to your computer and use it in GitHub Desktop.
Save ninmonkey/f9a6baa8c1adc11327de17ba87786a38 to your computer and use it in GitHub Desktop.
Compare-StringSet.ps1
using namespace System.Collections.Generic
function Compare-StringSet {
<#
.synopsis
quickly
.example
Compare-StringSet ('a'..'g') ('c'..'z')
Compare-StringSet ('a'..'g') ('A'..'E')
.example
Compare-StringSet ('a'..'g') ('A'..'E') -Insensitive
.LINK
https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1?view=net-6.0#hashset-and-linq-set-operations
.NOTES
other functions:
.ctor, Add, Clear, Comparer, Contains, CopyTo, Count, CreateSetComparer, EnsureCapacity, Enumerator, ExceptWith, GetEnumerator, GetObjectData, IntersectWith, IsProperSubsetOf, IsProperSupersetOf, IsSubsetOf, IsSupersetOf, OnDeserialization, Overlaps, Remove, RemoveWhere, SetEquals, SymmetricExceptWith, TrimExcess, TryGetValue, UnionWith
#>
[Alias(
'Set->CompareString'
)]
param(
[string[]]$ListA,
[string[]]$ListB,
[switch]$Insensitive
)
if($Insensitive) { throw "NYI: Equality comparison"}
<#
todo: future:
When empty column format string as '∅'
intersect as ..
union ∪
'∉' etc
#>
class StringSetComparisonResult {
[list[string]]$Intersect
[list[string]]$RemainingLeft
[list[string]]$RemainingRight
[list[string]]$Union
}
$results = [ordered]@{}
$SetA = [HashSet[string]]::new( [string[]]$ListA)
$SetB = [HashSet[string]]::new( [string[]]$ListB)
$SetA.IntersectWith( $setB )
$results['Intersect'] = $SetA
$SetA = [HashSet[string]]::new( [string[]]$ListA)
$SetB = [HashSet[string]]::new( [string[]]$ListB)
# $SetA -notin $results.Intersect
$results.'RemainingLeft' = $SetA | ?{
$results.'Intersect' -notcontains $_
}
$results.'RemainingRight' = $SetB | ?{
$results.'Intersect' -notcontains $_
}
$SetA = [HashSet[string]]::new( [string[]]$ListA)
$SetB = [HashSet[string]]::new( [string[]]$ListB)
$SetA.UnionWith( $SetB )
$results.'Union' = $SetA
[StringSetComparisonResult]$Results
# [hashset[string]]::new( [string[]]('a', 'b')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment