Skip to content

Instantly share code, notes, and snippets.

@Szer
Last active September 2, 2021 21:45
Show Gist options
  • Save Szer/3465245cc643dfb87600d7856d641ca7 to your computer and use it in GitHub Desktop.
Save Szer/3465245cc643dfb87600d7856d641ca7 to your computer and use it in GitHub Desktop.
Mutable Dictionary DSL
open System.Collections.Generic
[<Struct>]
type DictBuilder<'a, 'b when 'a: equality> =
val d: Dictionary<'a,'b>
new(()) = { d = Dictionary<'a, 'b>() }
member this.Yield (key, value) =
this.d.Add(key, value)
member this.Zero() = this.d
member _.Combine(_, _) = ()
member _.Delay f = f()
member this.Run _ = this.d
let mutDict<'a,'b when 'a: equality> = DictBuilder<'a, 'b>(())
let a =
mutDict {
1,3
2,4
3,5
}
val a : Dictionary<int,int> = dict [(1, 3); (2, 4); (3, 5)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment