Skip to content

Instantly share code, notes, and snippets.

@SLAVONchick
Created October 1, 2021 13:24
Show Gist options
  • Save SLAVONchick/44dc1e25273d9e2bd85bc8fb7dd0a9d4 to your computer and use it in GitHub Desktop.
Save SLAVONchick/44dc1e25273d9e2bd85bc8fb7dd0a9d4 to your computer and use it in GitHub Desktop.
type Hole() =
member _.Add(_, _) = ()
member _.Add _ = ()
type CollectionBuilder< ^Collection, 'b, ^Dictionary, 'c
when ^Collection : (member Add : 'b -> unit) and ^Collection : (new : unit -> ^Collection)
and ^Dictionary : (member Add : 'b * 'c -> unit) and ^Dictionary : (new : unit -> ^Dictionary)>() =
member inline _.Combine((x1, y1), (x2, y2)) =
let d = new ^Dictionary()
( ^Dictionary : (member Add : 'b * 'c -> unit) d, x1, y1 )
( ^Dictionary : (member Add : 'b * 'c -> unit) d, x2, y2 )
d
member inline _.Combine((x, y), d: ^Dictionary) =
( ^Dictionary : (member Add : 'b * 'c -> unit) d, x, y )
d
member inline _.Combine(x, y) =
let col = new ^Collection()
( ^Collection : (member Add : 'b -> unit) col, x )
( ^Collection : (member Add : 'b -> unit) col, y )
col
member inline _.Combine(x, col: ^Collection) =
( ^Collection : (member Add : 'b -> unit) col, x )
col
member inline _.Yield(x) = x
member inline _.Zero() = ()
member inline _.Delay f = f()
static member inline Instance = new CollectionBuilder< ^Collection, 'b, ^Dictionary, 'c>()
let inline collection< ^Col, 'b when ^Col : (member Add : 'b -> unit) and ^Col : (new : unit -> ^Col)> = CollectionBuilder< ^Col, 'b, Hole, _>.Instance
let inline dictionary< ^Dict, 'b, 'c when ^Dict : (member Add : 'b * 'c -> unit) and ^Dict : (new : unit -> ^Dict) > = CollectionBuilder<Hole, 'b, ^Dict, 'c>.Instance
let ra : ResizeArray<int> = collection<_, _> {
0
1
2
3
4
5
6
7
8
9
}
let d = dictionary<Dictionary<int, string>, _, _> {
1, "one"
2, "two"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment