Created
April 11, 2017 01:44
-
-
Save bleis-tift/fff0a4d18b8f9a084b67a532007a8c41 to your computer and use it in GitHub Desktop.
コレクション初期化子もどき
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System.Collections.Generic | |
type CollectionBuilder< 'TCollection, 'TElem, 'TRet when 'TCollection : (member Add : 'TElem -> 'TRet)> () = | |
[<DefaultValue>] val mutable xs : 'TCollection | |
member inline this.Yield(x: 'TElem) = | |
(^TCollection : (member Add : 'TElem -> 'TRet) (this.xs, x)) |> ignore | |
member inline this.Delay(f) = f | |
member inline this.Combine(_, rest) = rest () | |
member inline this.Run(f) = f (); this.xs | |
member inline this.Zero() = () | |
let inline collection (xs : 'TCollection when 'TCollection : (member Add : 'TElem -> 'TRet)) = | |
CollectionBuilder(xs=xs) | |
type Collection2Builder< ^TCollection, 'TElem1, 'TElem2, 'TRet when 'TCollection : (member Add : 'TElem1 * 'TElem2 -> 'TRet)> () = | |
[<DefaultValue>] val mutable xs : 'TCollection | |
member inline this.Yield(x: 'TElem1 * 'TElem2) = | |
let x1, x2 = x | |
(^TCollection : (member Add : 'TElem1 * 'TElem2 -> 'TRet) (this.xs, x1, x2)) |> ignore | |
member inline this.Delay(f) = f | |
member inline this.Combine(_, rest) = rest () | |
member inline this.Run(f) = f (); this.xs | |
member inline this.Zero() = () | |
let inline collection2 (xs : 'TCollection when 'TCollection : (member Add : 'TElem1 * 'TElem2 -> 'TRet)) = | |
Collection2Builder(xs=xs) | |
[<EntryPoint>] | |
let main argv = | |
let d = collection2 (Dictionary()) { | |
yield "hoge", 4 | |
yield "foo", 3 | |
} | |
printfn "%A" d | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment