Skip to content

Commit e8fd36d

Browse files
committed
+ HashSet operations
1 parent 1e2f488 commit e8fd36d

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

src/FSharpPlus/Control/Monoid.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type Plus =
2929
static member ``+`` (x: Set<_> , y , [<Optional>]_mthd: Plus ) = Set.union x y
3030

3131
#if !FABLE_COMPILER
32+
static member ``+`` (x: HashSet<_> , y , [<Optional>]_mthd: Plus ) = HashSet.union x y
3233
static member ``+`` (x: StringBuilder , y: StringBuilder , [<Optional>]_mthd: Plus ) = StringBuilder().Append(x).Append(y)
3334
static member ``+`` (_: Id0 , _: Id0 , [<Optional>]_mthd: Plus ) = Id0 ""
3435
static member ``+`` (x: AggregateException, y: AggregateException, [<Optional>]_mthd: Plus ) = Exception.add x y

src/FSharpPlus/Control/Numeric.fs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace FSharpPlus.Control
66

77
open System.Runtime.InteropServices
88
open FSharpPlus.Internals
9+
open FSharpPlus
910
#if FABLE_COMPILER
1011
/// NOTE
1112
type OptionalAttribute ()=
@@ -143,6 +144,7 @@ type Zero =
143144
static member Zero (_: unit , _: Zero ) = ()
144145
static member Zero (_: bool , _: Zero ) = false
145146
static member Zero (_: Set<'a> , _: Zero ) = Set.empty : Set<'a>
147+
static member Zero (_: HashSet<'a> , _: Zero ) = HashSet.empty : Set<'a>
146148
static member Zero (_: Map<'a,'b> , _: Zero ) = Map.empty : Map<'a,'b>
147149

148150
static member inline Invoke () =
@@ -213,7 +215,7 @@ type Zero with
213215
static member inline Zero (_: ^R , _: Default5) = Implicit.Invoke 0 : ^R
214216

215217
static member Zero (_: seq<'a> , _: Default4) = Seq.empty : seq<'a>
216-
static member Zero (_: IEnumerator<'a> , _: Default4) = FSharpPlus.Enumerator.Empty () : IEnumerator<'a>
218+
static member Zero (_: IEnumerator<'a> , _: Default4) = Enumerator.Empty () : IEnumerator<'a>
217219
static member Zero (_: IDictionary<'a,'b> , _: Default4) = Dictionary<'a,'b> () :> IDictionary<'a,'b>
218220
static member Zero (_: IReadOnlyDictionary<'a,'b> , _: Default4) = Dictionary<'a,'b> () :> IReadOnlyDictionary<'a,'b>
219221
static member inline Zero (_: 't , _: Default3) = (^t : (static member Empty: ^t) ()) : 't
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
namespace FSharpPlus
2+
3+
/// Additional operations on HashSet<'T>
4+
[<RequireQualifiedAccess>]
5+
module HashSet =
6+
open System.Collections.Generic
7+
open System.Collections.ObjectModel
8+
9+
10+
/// <summary>The empty set for the type 'T.</summary>
11+
[<GeneralizableValue>]
12+
[<CompiledName("Empty")>]
13+
let empty<'T> : HashSet<'T> = HashSet<'T> ()
14+
15+
/// <summary>The set containing the given element.</summary>
16+
/// <param name="value">The value for the set to contain.</param>
17+
/// <returns>The set containing <c>value</c>.</returns>
18+
[<CompiledName("Singleton")>]
19+
let singleton (value: 'T) : HashSet<'T> =
20+
let set =
21+
#if FABLE_COMPILER
22+
HashSet<'T> ()
23+
#else
24+
HashSet<'T> 1
25+
#endif
26+
set.Add value |> ignore
27+
set
28+
29+
/// <summary>Computes the union of the two sets.</summary>
30+
/// <param name="set1">The first input set.</param>
31+
/// <param name="set2">The second input set.</param>
32+
/// <returns>The union of <c>set1</c> and <c>set2</c>.</returns>
33+
[<CompiledName("Union")>]
34+
let union (x: HashSet<'T>) (y: HashSet<'T>) : HashSet<'T> =
35+
let union =
36+
#if FABLE_COMPILER
37+
HashSet<'T> ()
38+
#else
39+
HashSet<'T> (max x.Count y.Count)
40+
#endif
41+
for item in x do union.Add item |> ignore
42+
for item in y do union.Add item |> ignore
43+
union

src/FSharpPlus/FSharpPlus.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<Compile Include="Extensions/Map.fs" />
5151
<Compile Include="Extensions/Dictionary.fs" />
5252
<Compile Include="Extensions/Dict.fs" />
53+
<Compile Include="Extensions/HashSet.fs" />
5354
<Compile Include="Extensions/IReadOnlyDictionary.fs" />
5455
<Compile Include="Extensions/Enumerator.fs" />
5556
<Compile Include="Extensions/Task.fs" />

0 commit comments

Comments
 (0)