Skip to content

Commit abf8fb3

Browse files
authored
Move code up
1 parent 33fdc1e commit abf8fb3

File tree

1 file changed

+88
-89
lines changed

1 file changed

+88
-89
lines changed

src/FSharpPlus/Extensions/Dict.fs

Lines changed: 88 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,94 @@ module Dict =
66
open System.Collections.Generic
77
open System.Collections.ObjectModel
88

9+
/// <summary>Creates a conceptually infinite dictionay containing the same value for all possible keys.</summary>
10+
/// <param name="source">The value for all possible keys.</param>
11+
let initInfinite<'TKey,'TValue> (source: 'TValue) : IDictionary<'TKey,'TValue> =
12+
13+
let icollection value =
14+
{
15+
new ICollection<'t> with
16+
member __.Contains (item: 't) = obj.ReferenceEquals (item, value)
17+
member __.GetEnumerator () = (Seq.initInfinite (fun _ -> value)).GetEnumerator () :> System.Collections.IEnumerator
18+
member __.GetEnumerator () = (Seq.initInfinite (fun _ -> value)).GetEnumerator () : IEnumerator<'t>
19+
member __.IsReadOnly = true
20+
21+
member __.Add (_item: 't) : unit = raise (System.NotImplementedException())
22+
member __.Clear () : unit = raise (System.NotImplementedException())
23+
member __.CopyTo (_array: 't [], _arrayIndex: int) : unit = raise (System.NotImplementedException())
24+
member __.Count : int = -1
25+
member __.Remove (_item: 't): bool = raise (System.NotImplementedException())
26+
}
27+
28+
{
29+
new IDictionary<'TKey,'TValue> with
30+
member __.TryGetValue (_key: 'TKey, value: byref<'TValue>) = value <- source; true
31+
member __.Count = -1
32+
member __.ContainsKey (_key: 'TKey) = true
33+
member __.Contains (item: KeyValuePair<'TKey,'TValue>) = obj.ReferenceEquals (item.Value, source)
34+
member __.GetEnumerator () = invalidOp "Key set is potentially infinite." : System.Collections.IEnumerator
35+
member __.GetEnumerator () = invalidOp "Key set is potentially infinite." : IEnumerator<KeyValuePair<'TKey,'TValue>>
36+
member __.IsReadOnly = true
37+
member __.Values = icollection source
38+
member __.Item
39+
with get (_key: 'TKey) : 'TValue = source
40+
and set (_key: 'TKey) (_: 'TValue) : unit = raise (System.NotImplementedException())
41+
42+
member __.Add (_key: 'TKey, _value: 'TValue) : unit = raise (System.NotImplementedException())
43+
member __.Add (_item: KeyValuePair<'TKey,'TValue>) : unit = raise (System.NotImplementedException())
44+
member __.Clear () : unit = raise (System.NotImplementedException())
45+
member __.CopyTo (_arr: KeyValuePair<'TKey,'TValue> [], _arrayIndex: int) : unit = raise (System.NotImplementedException())
46+
member __.Keys : ICollection<'TKey> = raise (System.NotImplementedException())
47+
member __.Remove (_key: 'TKey) : bool = raise (System.NotImplementedException())
48+
member __.Remove (_item: KeyValuePair<'TKey,'TValue>) : bool = raise (System.NotImplementedException())
49+
}
50+
51+
let initHybrid<'TKey,'TValue> (konst: 'TValue) (source: IDictionary<'TKey,'TValue>) : IDictionary<'TKey,'TValue> =
52+
53+
let icollection (konst: 'TValue) (source: IDictionary<'TKey,'TValue>) =
54+
{
55+
new ICollection<'t> with
56+
member __.Contains (item: 't) = source.Values.Contains item || obj.ReferenceEquals (item, konst)
57+
member __.GetEnumerator () = (seq { yield! source.Values; yield! (Seq.initInfinite (fun _ -> konst))}).GetEnumerator () :> System.Collections.IEnumerator
58+
member __.GetEnumerator () = (seq { yield! source.Values; yield! (Seq.initInfinite (fun _ -> konst))}).GetEnumerator () : IEnumerator<'t>
59+
member __.IsReadOnly = true
60+
61+
member __.Add (_item: 't) : unit = raise (System.NotImplementedException())
62+
member __.Clear () : unit = raise (System.NotImplementedException())
63+
member __.CopyTo (_array: 't [], _arrayIndex: int) : unit = raise (System.NotImplementedException())
64+
member __.Count : int = -source.Count-1
65+
member __.Remove (_item: 't): bool = raise (System.NotImplementedException())
66+
}
67+
{
68+
new IDictionary<'TKey,'TValue> with
69+
member __.TryGetValue (key: 'TKey, value: byref<'TValue>) =
70+
match source.TryGetValue key with
71+
| true, v -> value <- v
72+
| _ -> value <- konst
73+
true
74+
member __.Count = -source.Count-1
75+
member __.ContainsKey (_key: 'TKey) = true
76+
member __.Contains (item: KeyValuePair<'TKey,'TValue>) =
77+
match source.TryGetValue item.Key with
78+
| true, v -> obj.ReferenceEquals (item.Value, v)
79+
| _ -> obj.ReferenceEquals (item.Value, konst)
80+
member __.GetEnumerator () = source.GetEnumerator () : System.Collections.IEnumerator
81+
member __.GetEnumerator () = source.GetEnumerator () : IEnumerator<KeyValuePair<'TKey,'TValue>>
82+
member __.IsReadOnly = true
83+
member __.Values = icollection konst source
84+
member __.Item
85+
with get (key: 'TKey) : 'TValue = match source.TryGetValue key with (true, v) -> v | _ -> konst
86+
and set (_key: 'TKey) (_: 'TValue) : unit = raise (System.NotImplementedException())
87+
88+
member __.Add (_key: 'TKey, _value: 'TValue) : unit = raise (System.NotImplementedException())
89+
member __.Add (_item: KeyValuePair<'TKey,'TValue>) : unit = raise (System.NotImplementedException())
90+
member __.Clear () : unit = raise (System.NotImplementedException())
91+
member __.CopyTo (_arr: KeyValuePair<'TKey,'TValue> [], _arrayIndex: int) : unit = raise (System.NotImplementedException())
92+
member __.Keys : ICollection<'TKey> = raise (System.NotImplementedException())
93+
member __.Remove (_key: 'TKey) : bool = raise (System.NotImplementedException())
94+
member __.Remove (_item: KeyValuePair<'TKey,'TValue>) : bool = raise (System.NotImplementedException())
95+
}
96+
997
#if !FABLE_COMPILER
1098
open System.Linq
1199

@@ -129,95 +217,6 @@ module Dict =
129217
dct2.Add (k, vy)
130218
dct1 :> IDictionary<'Key, 'T1>, dct2 :> IDictionary<'Key, 'T2>
131219

132-
133-
/// <summary>Creates a conceptually infinite dictionay containing the same value for all possible keys.</summary>
134-
/// <param name="source">The value for all possible keys.</param>
135-
let initInfinite<'TKey,'TValue> (source: 'TValue) : IDictionary<'TKey,'TValue> =
136-
137-
let icollection value =
138-
{
139-
new ICollection<'t> with
140-
member __.Contains (item: 't) = obj.ReferenceEquals (item, value)
141-
member __.GetEnumerator () = (Seq.initInfinite (fun _ -> value)).GetEnumerator () :> System.Collections.IEnumerator
142-
member __.GetEnumerator () = (Seq.initInfinite (fun _ -> value)).GetEnumerator () : IEnumerator<'t>
143-
member __.IsReadOnly = true
144-
145-
member __.Add (_item: 't) : unit = raise (System.NotImplementedException())
146-
member __.Clear () : unit = raise (System.NotImplementedException())
147-
member __.CopyTo (_array: 't [], _arrayIndex: int) : unit = raise (System.NotImplementedException())
148-
member __.Count : int = -1
149-
member __.Remove (_item: 't): bool = raise (System.NotImplementedException())
150-
}
151-
152-
{
153-
new IDictionary<'TKey,'TValue> with
154-
member __.TryGetValue (_key: 'TKey, value: byref<'TValue>) = value <- source; true
155-
member __.Count = -1
156-
member __.ContainsKey (_key: 'TKey) = true
157-
member __.Contains (item: KeyValuePair<'TKey,'TValue>) = obj.ReferenceEquals (item.Value, source)
158-
member __.GetEnumerator () = invalidOp "Key set is potentially infinite." : System.Collections.IEnumerator
159-
member __.GetEnumerator () = invalidOp "Key set is potentially infinite." : IEnumerator<KeyValuePair<'TKey,'TValue>>
160-
member __.IsReadOnly = true
161-
member __.Values = icollection source
162-
member __.Item
163-
with get (_key: 'TKey) : 'TValue = source
164-
and set (_key: 'TKey) (_: 'TValue) : unit = raise (System.NotImplementedException())
165-
166-
member __.Add (_key: 'TKey, _value: 'TValue) : unit = raise (System.NotImplementedException())
167-
member __.Add (_item: KeyValuePair<'TKey,'TValue>) : unit = raise (System.NotImplementedException())
168-
member __.Clear () : unit = raise (System.NotImplementedException())
169-
member __.CopyTo (_arr: KeyValuePair<'TKey,'TValue> [], _arrayIndex: int) : unit = raise (System.NotImplementedException())
170-
member __.Keys : ICollection<'TKey> = raise (System.NotImplementedException())
171-
member __.Remove (_key: 'TKey) : bool = raise (System.NotImplementedException())
172-
member __.Remove (_item: KeyValuePair<'TKey,'TValue>) : bool = raise (System.NotImplementedException())
173-
}
174-
175-
let initHybrid<'TKey,'TValue> (konst: 'TValue) (source: IDictionary<'TKey,'TValue>) : IDictionary<'TKey,'TValue> =
176-
177-
let icollection (konst: 'TValue) (source: IDictionary<'TKey,'TValue>) =
178-
{
179-
new ICollection<'t> with
180-
member __.Contains (item: 't) = source.Values.Contains item || obj.ReferenceEquals (item, konst)
181-
member __.GetEnumerator () = (seq { yield! source.Values; yield! (Seq.initInfinite (fun _ -> konst))}).GetEnumerator () :> System.Collections.IEnumerator
182-
member __.GetEnumerator () = (seq { yield! source.Values; yield! (Seq.initInfinite (fun _ -> konst))}).GetEnumerator () : IEnumerator<'t>
183-
member __.IsReadOnly = true
184-
185-
member __.Add (_item: 't) : unit = raise (System.NotImplementedException())
186-
member __.Clear () : unit = raise (System.NotImplementedException())
187-
member __.CopyTo (_array: 't [], _arrayIndex: int) : unit = raise (System.NotImplementedException())
188-
member __.Count : int = -source.Count-1
189-
member __.Remove (_item: 't): bool = raise (System.NotImplementedException())
190-
}
191-
{
192-
new IDictionary<'TKey,'TValue> with
193-
member __.TryGetValue (key: 'TKey, value: byref<'TValue>) =
194-
match source.TryGetValue key with
195-
| true, v -> value <- v
196-
| _ -> value <- konst
197-
true
198-
member __.Count = -source.Count-1
199-
member __.ContainsKey (_key: 'TKey) = true
200-
member __.Contains (item: KeyValuePair<'TKey,'TValue>) =
201-
match source.TryGetValue item.Key with
202-
| true, v -> obj.ReferenceEquals (item.Value, v)
203-
| _ -> obj.ReferenceEquals (item.Value, konst)
204-
member __.GetEnumerator () = source.GetEnumerator () : System.Collections.IEnumerator
205-
member __.GetEnumerator () = source.GetEnumerator () : IEnumerator<KeyValuePair<'TKey,'TValue>>
206-
member __.IsReadOnly = true
207-
member __.Values = icollection konst source
208-
member __.Item
209-
with get (key: 'TKey) : 'TValue = match source.TryGetValue key with (true, v) -> v | _ -> konst
210-
and set (_key: 'TKey) (_: 'TValue) : unit = raise (System.NotImplementedException())
211-
212-
member __.Add (_key: 'TKey, _value: 'TValue) : unit = raise (System.NotImplementedException())
213-
member __.Add (_item: KeyValuePair<'TKey,'TValue>) : unit = raise (System.NotImplementedException())
214-
member __.Clear () : unit = raise (System.NotImplementedException())
215-
member __.CopyTo (_arr: KeyValuePair<'TKey,'TValue> [], _arrayIndex: int) : unit = raise (System.NotImplementedException())
216-
member __.Keys : ICollection<'TKey> = raise (System.NotImplementedException())
217-
member __.Remove (_key: 'TKey) : bool = raise (System.NotImplementedException())
218-
member __.Remove (_item: KeyValuePair<'TKey,'TValue>) : bool = raise (System.NotImplementedException())
219-
}
220-
221220
/// Returns the union of two dictionaries, using the combiner function for duplicate keys.
222221
let unionWith combiner (source1: IDictionary<'Key, 'Value>) (source2: IDictionary<'Key, 'Value>) =
223222
let combine () =

0 commit comments

Comments
 (0)