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