Skip to content

Commit 5123a3e

Browse files
authored
+Dict.initInfinite
1 parent c9307fb commit 5123a3e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/FSharpPlus/Extensions/Dict.fs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,29 @@ module Dict =
161161
| Some v -> dct.Add (k, v)
162162
| None -> ()
163163
dct :> IDictionary<'Key, 'U>
164+
165+
/// <summary>Creates a conceptually infinite dictionay containing the same value for all possible keys.</summary>
166+
/// <param name="source">The value for all possible keys.</param>
167+
let initInfinite<'TKey,'TValue> (source: 'TValue) : IDictionary<'TKey,'TValue> =
168+
{
169+
new IDictionary<'TKey,'TValue> with
170+
member __.TryGetValue (_key: 'TKey, value: byref<'TValue>) = value <- source; true
171+
member __.Count = System.Int32.MaxValue
172+
member __.ContainsKey (_key: 'TKey) = true
173+
member __.GetEnumerator () = Seq.empty.GetEnumerator () :> System.Collections.IEnumerator
174+
member __.GetEnumerator () = Seq.empty.GetEnumerator () : IEnumerator<KeyValuePair<'TKey,'TValue>>
175+
member __.IsReadOnly = true
176+
member __.Item
177+
with get (_key: 'TKey) : 'TValue = source
178+
and set (_key: 'TKey) (_: 'TValue) : unit = raise (System.NotImplementedException())
179+
180+
member __.Add (_key: 'TKey, _value: 'TValue) : unit = raise (System.NotImplementedException())
181+
member __.Add (_item: KeyValuePair<'TKey,'TValue>) : unit = raise (System.NotImplementedException())
182+
member __.Clear () : unit = raise (System.NotImplementedException())
183+
member __.Contains (_item: KeyValuePair<'TKey,'TValue>) : bool = raise (System.NotImplementedException())
184+
member __.CopyTo (_arr: KeyValuePair<'TKey,'TValue> [], _arrayIndex: int) : unit = raise (System.NotImplementedException())
185+
member __.Keys : ICollection<'TKey> = raise (System.NotImplementedException())
186+
member __.Remove (_key: 'TKey) : bool = raise (System.NotImplementedException())
187+
member __.Remove (_item: KeyValuePair<'TKey,'TValue>) : bool = raise (System.NotImplementedException())
188+
member __.Values : ICollection<'TValue> = raise (System.NotImplementedException())
189+
}

0 commit comments

Comments
 (0)