Skip to content

Commit 43934c6

Browse files
authored
Add HashSet operations (#626)
1 parent 5baaf36 commit 43934c6

File tree

13 files changed

+98
-4
lines changed

13 files changed

+98
-4
lines changed

docsrc/content/abstraction-foldable.fsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ From .Net/F#
5858
- ``seq<'T>``
5959
- ``list<'T>``
6060
- ``'T []``
61+
- ``Set<'T>``
62+
- ``HashSet<'T>``
6163
- ``option<'T>``
6264
- ``voption<'T>``
6365
- ``Result<'T, 'Error>``

docsrc/content/abstraction-functor.fsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ Restricted:
104104
- ``string``
105105
- ``StringBuilder``
106106
- ``Set<'T>``
107+
- ``HashSet<'T>``
107108
- ``IEnumerator<'T>``
108109
[Suggest another](https://github.com/fsprojects/FSharpPlus/issues/new) concrete implementation
109110

docsrc/content/abstraction-monoid.fsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ From .Net/F#
5757
- ``StringBuilder``
5858
- ``unit``
5959
- ``Set<'T>``
60+
- ``HashSet<'T>``
6061
- ``Map<'T,'Monoid>``
6162
- ``TimeSpan``
6263
- ``Tuple<'Monoid1* ... *'MonoidN>``

docsrc/content/abstraction-semigroup.fsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ From .Net/F#
4242
- ``StringBuilder``
4343
- ``unit``
4444
- ``Set<'T>``
45+
- ``HashSet<'T>``
4546
- ``Map<'T, 'U>``
4647
- ``TimeSpan``
4748
- ``Tuple<*>``

src/FSharpPlus/Control/Functor.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ type Map =
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
108108
static member Map ((_: Set2<'T> , _: 'T->'U), _mthd: Map) = Set2<'U>()
109+
static member Map ((x: HashSet<_ > , f ), _mthd: Map) = HashSet.map f x
109110

110111

111112
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

0 commit comments

Comments
 (0)