Skip to content

Commit 2226f2b

Browse files
committed
+ HashSet operations
1 parent 1e2f488 commit 2226f2b

File tree

8 files changed

+66
-8
lines changed

8 files changed

+66
-8
lines changed

src/FSharpPlus/Control/Functor.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ type Map =
105105
static member Map ((x: string , f ), _mthd: Map) = String.map f x
106106
static member Map ((x: StringBuilder , f ), _mthd: Map) = StringBuilder (String.map f (string x))
107107
static member Map ((x: Set<_> , f ), _mthd: Map) = Set.map f x
108-
static member Map ((_: Set2<'T> , _: 'T->'U), _mthd: Map) = Set2<'U>()
108+
static member Map ((x: HashSet<_ > , f ), _mthd: Map) = HashSet.map f x
109109

110110

111111
static member inline Invoke (mapping: 'T->'U) (source: '``Functor<'T>``) : '``Functor<'U>`` =

src/FSharpPlus/Control/Monad.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ type Return =
167167
static member Return (_: string , _: Return ) = fun (x: char) -> string x : string
168168
static member Return (_: StringBuilder , _: Return ) = fun (x: char) -> new StringBuilder (string x) : StringBuilder
169169
static member Return (_: 'a Set , _: Return ) = fun (x: 'a ) -> Set.singleton x
170-
static member Return (_: 'a Set2 , _: Return ) = fun (_: 'a ) -> Set2() : 'a Set2
170+
static member Return (_: 'a HashSet , _: Return ) = fun (x: 'a ) -> HashSet.singleton x
171171

172172

173173
type Delay =

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 ()=
@@ -206,14 +207,15 @@ type Zero with
206207
static member inline Zero (_: Lazy<'a> , _: Zero) = let (v: 'a) = Zero.Invoke () in lazy v
207208
static member Zero (_: Dictionary<'a,'b> , _: Zero) = Dictionary<'a,'b> ()
208209
static member Zero (_: ResizeArray<'a> , _: Zero) = ResizeArray () : ResizeArray<'a>
210+
static member Zero (_: HashSet<'a> , _: Zero ) = HashSet.empty : HashSet<'a>
209211

210212
type Zero with
211213
static member inline Zero (_: ^R , _: Default6) = FromInt64.Invoke 0L : ^R
212214

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

src/FSharpPlus/Control/ZipApplicative.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ type Pure =
6161
[<CompilerMessage(MessagePure + "ResizeArray<'t>.", Code, IsError = true)>]
6262
static member Pure (x: ResizeArray<'a>, _: Pure ) = Return.Return (x, Unchecked.defaultof<Return>)
6363

64-
//Restricted
6564
[<CompilerMessage(MessagePure + "string.", Code, IsError = true)>]
6665
static member Pure (_: string , _: Pure ) = fun (x: char) -> string x : string
6766
[<CompilerMessage(MessagePure + "StringBuilder.", Code, IsError = true)>]
6867
static member Pure (_: StringBuilder , _: Pure ) = fun (x: char) -> new StringBuilder (string x) : StringBuilder
6968
[<CompilerMessage(MessagePure + "Set.", Code, IsError = true)>]
7069
static member Pure (_: 'a Set , _: Pure ) = fun (x: 'a ) -> Set.singleton x
71-
static member Pure (_: 'a Set2 , _: Pure ) = fun (_: 'a ) -> Set2() : 'a Set2
70+
[<CompilerMessage(MessagePure + "HashSet.", Code, IsError = true)>]
71+
static member Pure (_: 'a HashSet , _: Pure ) = fun (x: 'a ) -> HashSet.singleton x
7272

7373
#endif
7474

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
namespace FSharpPlus
2+
3+
/// Additional operations on HashSet<'T>
4+
[<RequireQualifiedAccess>]
5+
module HashSet =
6+
open System.Collections.Generic
7+
open FSharpPlus.Internals.Errors
8+
9+
/// <summary>The empty set for the type 'T.</summary>
10+
[<GeneralizableValue>]
11+
[<CompiledName("Empty")>]
12+
let empty<'T> : HashSet<'T> = HashSet<'T> ()
13+
14+
/// <summary>The set containing the given element.</summary>
15+
/// <param name="value">The value for the set to contain.</param>
16+
/// <returns>The set containing <c>value</c>.</returns>
17+
[<CompiledName("Singleton")>]
18+
let singleton (value: 'T) : HashSet<'T> =
19+
let set =
20+
#if FABLE_COMPILER
21+
HashSet<'T> ()
22+
#else
23+
HashSet<'T> 1
24+
#endif
25+
set.Add value |> ignore
26+
set
27+
28+
/// <summary>Computes the union of the two sets.</summary>
29+
/// <param name="source1">The first input set.</param>
30+
/// <param name="source2">The second input set.</param>
31+
/// <returns>The union of <c>set1</c> and <c>set2</c>.</returns>
32+
[<CompiledName("Union")>]
33+
let union (source1: HashSet<'T>) (source2: HashSet<'T>) : HashSet<'T> =
34+
raiseIfNull (nameof source1) source1
35+
raiseIfNull (nameof source2) source2
36+
let union =
37+
#if FABLE_COMPILER
38+
HashSet<'T> ()
39+
#else
40+
HashSet<'T> (max source1.Count source2.Count)
41+
#endif
42+
for item in source1 do union.Add item |> ignore
43+
for item in source2 do union.Add item |> ignore
44+
union
45+
46+
/// <summary>Returns a new collection containing the results of applying the
47+
/// given function to each element of the input set.</summary>
48+
/// <param name="mapping">The function to transform elements of the input set.</param>
49+
/// <param name="source">The input set.</param>
50+
/// <returns>A set containing the transformed elements.</returns>
51+
[<CompiledName("Map")>]
52+
let map (mapping: 'T -> 'U) (source: HashSet<'T>) : HashSet<'U> =
53+
raiseIfNull (nameof source) source
54+
let result = empty<'U>
55+
for item in source do
56+
result.Add (mapping item) |> ignore
57+
result

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" />

src/FSharpPlus/Internals.fs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,6 @@ type KeyValuePair2<'TKey, 'TValue> = struct
122122
new (key, value) = { Key = key; Value = value }
123123
end
124124

125-
[<Sealed>]
126-
type Set2<'T when 'T: comparison >() = class end
127-
128125
// BitConverter
129126

130127
#nowarn "9"

0 commit comments

Comments
 (0)