2
我想確定我的計算表達一些運營商定製定義自定義操作,但不能使它工作如何計算表達式
type ZipSeq() =
[<CustomOperation("<*>")>]
member this.Apply f s =
f |> Seq.zip s |> Seq.map (fun (y, x) -> x(y))
member this.Return x =
Seq.initInfinite (fun _ -> x)
// (a -> b) -> seq<a> -> seq<b>
[<CustomOperation("<!>")>]
member this.Map f s =
this.Apply (this.Return f) s
let zipSeq = new ZipSeq()
let f (a : float) = a * a
let s = seq { yield 1. }
// seq<b>
let h1 = zipSeq.Map f s
//thinking h1 should be the same as h2
//but compilation error : ` This value is not a function and cannot be applied`
let h2 = zipSeq { return f <!> s }
順便說一句,改變member this.Map f s ...
到member this.Map (f, s) ...
給人同樣的錯誤。
我不認爲這是可能的。不過,這很酷。 – Tarmil
您應該能夠通過單獨定義這些運算符(即,不作爲計算生成器的一部分)來實現這種語法,並使它們返回一個monad實例,然後它可以是'let!'-ed或'return!' - 編輯。 –
你能建議任何替代方案嗎? – baio