F# 在计算表达式中展平嵌套元组
F# Flattening Nested Tuples in Computation Expression
我有一个计算表达式,我想 return 一个扁平元组作为第一个元素,int
作为第二个元素。我正在尝试使用方法重载来完成此操作。现在编译器抛出一个错误,说它找不到唯一的重载。我不知道如何帮助编译器解决这个问题。它对我来说似乎是确定性的。
type IntBuilder () =
member inline this.Yield (i:int) =
i
member inline this.For(source:seq<'a>, body:'a -> seq<'b * int>) =
source
|> Seq.collect (fun x -> body x |> Seq.map (fun (idx, i) -> (x, idx), i))
member inline this.For(source:seq<'a>, body:'a -> int) =
source |> Seq.map (fun x -> x, body x)
member inline this.Run(source:seq<('a * ('b * ('c * 'd))) * 'v>) =
source
|> Seq.map (fun ((x, (y, (z, a))), d) -> (x, y, z, a), d)
member inline this.Run(source:seq<('a * ('b * 'c)) * 'v>) =
source
|> Seq.map (fun ((x, (y, z)), d) -> (x, y, z), d)
member inline this.Run(source:seq<('a * 'b) * 'v>) =
source
|> Seq.map (fun ((x, y), d) -> (x, y), d)
member inline this.Run(source:seq<'a * 'v>) =
source
|> Seq.map (fun (x, d) -> x, d)
let intBuilder = IntBuilder ()
let c =
intBuilder {
for i in 1..2 do
for j in 1..2 do
for k in 1..2 do
for l in 1..2 ->
i + j + k + l
}
// What I get
c : seq<(int * (int * (int * int))) * int>
// What I want
c : seq<(int * int * int * int) * int>
在这种情况下 c
是类型 seq<(int * (int * (int * int))) * int>
。我希望 IntBuilder
计算为 return seq<(int * int * int * int), int>
。我该如何做到这一点?
这可以通过内联和重载决议来解决,但在一般意义上,(afaik) 不可能有一个函数可以 return 类型 int * int * ...
基于已经给出了任意类型的输入。也许其他人可以权衡一下。
尽管如此,我们可以通过 运行 时间类型测试来实现扁平化。
let flatten tuple =
let rec fold (tuple: obj) acc =
match tuple with
| :? int as v -> v::acc
| :? ITuple as rest ->
let next, value = rest.[0], rest.[1] :?> int
fold next (value::acc)
| _ -> failwith "Unexpected type"
fold tuple []
> flatten ((1, 2), 3);;
val it : int list = [1; 2; 3]
已编辑问题的答案:
编译器无法解析正确的重载,因为
seq<('a * ('b* ('c * 'd))) * 'v>)
是以下类型的子类型:
seq<'u * 'v> where 'u = 'a * ('b* ('c * 'd)))
等等,对于其他类型。因此,将某些类型约束为具体类型。约束最里面的元组类型应该就足够了。
例如:
member this.Run(source:seq<('a * ('b* ('c* int))) * 'v>) =
source
|> Seq.map (fun ((x, (y, (z, a))), d) -> (x, y, z, a), d)
member this.Run(source:seq<('a * ('b* int)) * 'v>) =
source
|> Seq.map (fun ((x, (y, z)), d) -> (x, y, z), d)
member this.Run(source:seq<('a * int) * 'v>) =
source
|> Seq.map (fun ((x, y), d) -> (x, y), d)
member this.Run(source:seq<int * 'v>) =
source
|> Seq.map (fun (x, d) -> x, d)
看起来让它工作的唯一方法是将它们全部包装在具体类型中:
type T1<'a> = | T1 of seq<'a * int>
type T2<'a,'b> = | T2 of seq<('a * 'b) * int>
type T3<'a,'b,'c> = | T3 of seq<('a * ('b * 'c)) * int>
type T4<'a,'b,'c,'d> = | T4 of seq<('a * ('b * ('c * 'd))) * int>
type T5<'a,'b,'c,'d,'e> = | T5 of seq<('a * ('b * ('c * ('d * 'e)))) * int>
type IntBuilder () =
member this.Yield (i:int) =
i
member this.For(source:seq<'a>, body:'a -> int) =
source |> Seq.map (fun x -> x, body x)
|> T1.T1
member this.For(source:seq<'a>, body:'a -> T1<'b>) =
source
|> Seq.collect (fun x ->
body x
|> fun (T1.T1 x) -> x
|> Seq.map (fun (idx, i) -> (x, idx), i))
|> T2.T2
member this.For(source:seq<'a>, body:'a -> T2<'b,'c>) =
source
|> Seq.collect (fun x ->
body x
|> fun (T2.T2 x) -> x
|> Seq.map (fun (idx, i) -> (x, idx), i))
|> T3.T3
member this.For(source:seq<'a>, body:'a -> T3<'b,'c,'d>) =
source
|> Seq.collect (fun x ->
body x
|> fun (T3.T3 x) -> x
|> Seq.map (fun (idx, i) -> (x, idx), i))
|> T4.T4
member this.For(source:seq<'a>, body:'a -> T4<'b,'c,'d,'e>) =
source
|> Seq.collect (fun x ->
body x
|> fun (T4.T4 x) -> x
|> Seq.map (fun (idx, i) -> (x, idx), i))
|> T5.T5
member inline this.Run(T1.T1 source) =
source
|> Seq.map (fun (x, d) -> x, d)
member inline this.Run(T2.T2 source) =
source
|> Seq.map (fun ((x, y), d) -> (x, y), d)
member inline this.Run(T3.T3 source) =
source
|> Seq.map (fun ((x, (y, z)), d) -> (x, y, z), d)
member inline this.Run(T4.T4 source) =
source
|> Seq.map (fun ((x, (y, (z, a))), d) -> (x, y, z, a), d)
let intBuilder = IntBuilder ()
let c =
intBuilder {
for i in 1..2 do
for j in 1..2 do
for k in 1..2 do
for l in 1..2 ->
i + j + k + l
}
FSI 输出:
val c : seq<(int * int * int * int) * int>
> c;;
val it : seq<(int * int * int * int) * int> =
seq
[((1, 1, 1, 1), 4); ((1, 1, 1, 2), 5); ((1, 1, 2, 1), 5);
((1, 1, 2, 2), 6); ...]
我有一个计算表达式,我想 return 一个扁平元组作为第一个元素,int
作为第二个元素。我正在尝试使用方法重载来完成此操作。现在编译器抛出一个错误,说它找不到唯一的重载。我不知道如何帮助编译器解决这个问题。它对我来说似乎是确定性的。
type IntBuilder () =
member inline this.Yield (i:int) =
i
member inline this.For(source:seq<'a>, body:'a -> seq<'b * int>) =
source
|> Seq.collect (fun x -> body x |> Seq.map (fun (idx, i) -> (x, idx), i))
member inline this.For(source:seq<'a>, body:'a -> int) =
source |> Seq.map (fun x -> x, body x)
member inline this.Run(source:seq<('a * ('b * ('c * 'd))) * 'v>) =
source
|> Seq.map (fun ((x, (y, (z, a))), d) -> (x, y, z, a), d)
member inline this.Run(source:seq<('a * ('b * 'c)) * 'v>) =
source
|> Seq.map (fun ((x, (y, z)), d) -> (x, y, z), d)
member inline this.Run(source:seq<('a * 'b) * 'v>) =
source
|> Seq.map (fun ((x, y), d) -> (x, y), d)
member inline this.Run(source:seq<'a * 'v>) =
source
|> Seq.map (fun (x, d) -> x, d)
let intBuilder = IntBuilder ()
let c =
intBuilder {
for i in 1..2 do
for j in 1..2 do
for k in 1..2 do
for l in 1..2 ->
i + j + k + l
}
// What I get
c : seq<(int * (int * (int * int))) * int>
// What I want
c : seq<(int * int * int * int) * int>
在这种情况下 c
是类型 seq<(int * (int * (int * int))) * int>
。我希望 IntBuilder
计算为 return seq<(int * int * int * int), int>
。我该如何做到这一点?
这可以通过内联和重载决议来解决,但在一般意义上,(afaik) 不可能有一个函数可以 return 类型 int * int * ...
基于已经给出了任意类型的输入。也许其他人可以权衡一下。
尽管如此,我们可以通过 运行 时间类型测试来实现扁平化。
let flatten tuple =
let rec fold (tuple: obj) acc =
match tuple with
| :? int as v -> v::acc
| :? ITuple as rest ->
let next, value = rest.[0], rest.[1] :?> int
fold next (value::acc)
| _ -> failwith "Unexpected type"
fold tuple []
> flatten ((1, 2), 3);;
val it : int list = [1; 2; 3]
已编辑问题的答案:
编译器无法解析正确的重载,因为
seq<('a * ('b* ('c * 'd))) * 'v>)
是以下类型的子类型:
seq<'u * 'v> where 'u = 'a * ('b* ('c * 'd)))
等等,对于其他类型。因此,将某些类型约束为具体类型。约束最里面的元组类型应该就足够了。
例如:
member this.Run(source:seq<('a * ('b* ('c* int))) * 'v>) =
source
|> Seq.map (fun ((x, (y, (z, a))), d) -> (x, y, z, a), d)
member this.Run(source:seq<('a * ('b* int)) * 'v>) =
source
|> Seq.map (fun ((x, (y, z)), d) -> (x, y, z), d)
member this.Run(source:seq<('a * int) * 'v>) =
source
|> Seq.map (fun ((x, y), d) -> (x, y), d)
member this.Run(source:seq<int * 'v>) =
source
|> Seq.map (fun (x, d) -> x, d)
看起来让它工作的唯一方法是将它们全部包装在具体类型中:
type T1<'a> = | T1 of seq<'a * int>
type T2<'a,'b> = | T2 of seq<('a * 'b) * int>
type T3<'a,'b,'c> = | T3 of seq<('a * ('b * 'c)) * int>
type T4<'a,'b,'c,'d> = | T4 of seq<('a * ('b * ('c * 'd))) * int>
type T5<'a,'b,'c,'d,'e> = | T5 of seq<('a * ('b * ('c * ('d * 'e)))) * int>
type IntBuilder () =
member this.Yield (i:int) =
i
member this.For(source:seq<'a>, body:'a -> int) =
source |> Seq.map (fun x -> x, body x)
|> T1.T1
member this.For(source:seq<'a>, body:'a -> T1<'b>) =
source
|> Seq.collect (fun x ->
body x
|> fun (T1.T1 x) -> x
|> Seq.map (fun (idx, i) -> (x, idx), i))
|> T2.T2
member this.For(source:seq<'a>, body:'a -> T2<'b,'c>) =
source
|> Seq.collect (fun x ->
body x
|> fun (T2.T2 x) -> x
|> Seq.map (fun (idx, i) -> (x, idx), i))
|> T3.T3
member this.For(source:seq<'a>, body:'a -> T3<'b,'c,'d>) =
source
|> Seq.collect (fun x ->
body x
|> fun (T3.T3 x) -> x
|> Seq.map (fun (idx, i) -> (x, idx), i))
|> T4.T4
member this.For(source:seq<'a>, body:'a -> T4<'b,'c,'d,'e>) =
source
|> Seq.collect (fun x ->
body x
|> fun (T4.T4 x) -> x
|> Seq.map (fun (idx, i) -> (x, idx), i))
|> T5.T5
member inline this.Run(T1.T1 source) =
source
|> Seq.map (fun (x, d) -> x, d)
member inline this.Run(T2.T2 source) =
source
|> Seq.map (fun ((x, y), d) -> (x, y), d)
member inline this.Run(T3.T3 source) =
source
|> Seq.map (fun ((x, (y, z)), d) -> (x, y, z), d)
member inline this.Run(T4.T4 source) =
source
|> Seq.map (fun ((x, (y, (z, a))), d) -> (x, y, z, a), d)
let intBuilder = IntBuilder ()
let c =
intBuilder {
for i in 1..2 do
for j in 1..2 do
for k in 1..2 do
for l in 1..2 ->
i + j + k + l
}
FSI 输出:
val c : seq<(int * int * int * int) * int>
> c;;
val it : seq<(int * int * int * int) * int> =
seq
[((1, 1, 1, 1), 4); ((1, 1, 1, 2), 5); ((1, 1, 2, 1), 5);
((1, 1, 2, 2), 6); ...]