Skip to content

Commit 3407b81

Browse files
authored
Implement Values and Contains
1 parent 0bd3646 commit 3407b81

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/FSharpPlus/Extensions/Dict.fs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,25 +165,41 @@ module Dict =
165165
/// <summary>Creates a conceptually infinite dictionay containing the same value for all possible keys.</summary>
166166
/// <param name="source">The value for all possible keys.</param>
167167
let initInfinite<'TKey,'TValue> (source: 'TValue) : IDictionary<'TKey,'TValue> =
168-
{
168+
169+
let icollection value =
170+
{
171+
new ICollection<'t> with
172+
member __.Contains (item: 't) = obj.ReferenceEquals (item, value)
173+
member __.GetEnumerator () = (Seq.initInfinite (fun _ -> value)).GetEnumerator () :> System.Collections.IEnumerator
174+
member __.GetEnumerator () = (Seq.initInfinite (fun _ -> value)).GetEnumerator () : IEnumerator<'t>
175+
member __.IsReadOnly = true
176+
177+
member __.Add (_item: 't) : unit = raise (System.NotImplementedException())
178+
member __.Clear () : unit = raise (System.NotImplementedException())
179+
member __.CopyTo (_array: 't [], _arrayIndex: int) : unit = raise (System.NotImplementedException())
180+
member __.Count : int = raise (System.NotImplementedException())
181+
member __.Remove (_item: 't): bool = raise (System.NotImplementedException())
182+
}
183+
184+
{
169185
new IDictionary<'TKey,'TValue> with
170186
member __.TryGetValue (_key: 'TKey, value: byref<'TValue>) = value <- source; true
171187
member __.Count = System.Int32.MaxValue
172188
member __.ContainsKey (_key: 'TKey) = true
189+
member __.Contains (item: KeyValuePair<'TKey,'TValue>) = obj.ReferenceEquals (item.Value, source)
173190
member __.GetEnumerator () = Seq.empty.GetEnumerator () :> System.Collections.IEnumerator
174191
member __.GetEnumerator () = Seq.empty.GetEnumerator () : IEnumerator<KeyValuePair<'TKey,'TValue>>
175192
member __.IsReadOnly = true
193+
member __.Values = icollection source
176194
member __.Item
177195
with get (_key: 'TKey) : 'TValue = source
178196
and set (_key: 'TKey) (_: 'TValue) : unit = raise (System.NotImplementedException())
179197

180198
member __.Add (_key: 'TKey, _value: 'TValue) : unit = raise (System.NotImplementedException())
181199
member __.Add (_item: KeyValuePair<'TKey,'TValue>) : unit = raise (System.NotImplementedException())
182200
member __.Clear () : unit = raise (System.NotImplementedException())
183-
member __.Contains (_item: KeyValuePair<'TKey,'TValue>) : bool = raise (System.NotImplementedException())
184201
member __.CopyTo (_arr: KeyValuePair<'TKey,'TValue> [], _arrayIndex: int) : unit = raise (System.NotImplementedException())
185202
member __.Keys : ICollection<'TKey> = raise (System.NotImplementedException())
186203
member __.Remove (_key: 'TKey) : bool = raise (System.NotImplementedException())
187204
member __.Remove (_item: KeyValuePair<'TKey,'TValue>) : bool = raise (System.NotImplementedException())
188-
member __.Values : ICollection<'TValue> = raise (System.NotImplementedException())
189205
}

0 commit comments

Comments
 (0)