我有一个适用于列表的函数,但函数的输入来自外部系统/语言的float[,]
。
我看了this,但是当我应用此内容时,我收到错误float[,] is not compatible with Seq<a'>
。但是这个列表也只是花车。
列表功能:
let aggrArraysL data =
data
|> Seq.groupBy (fun (a, b, c) -> a)
|> Seq.map (fun (key, group) ->
group |> Seq.reduce (fun (a, b, c) (x, y, z) -> a, b + y, (b * c + y * z * 1.)/(b + y)))
数组尝试:
let aggrArrays (data2 : float[,]) =
data2
|> Seq.toList
|> Seq.groupBy (fun (a, b, c) -> a)
|> Seq.map (fun (key, group) ->
group |> Seq.reduce (fun (a, b, c) (x, y, z) -> a, b + y, (b * c + y * z * 1.)/(b + y)))
|> Seq.toArray
我哪里错了?谢谢!
答案 0 :(得分:4)
2D数组实现IEnumerable
,因此您可以使用Seq.cast<T>
let toList (data2: float[,]) = data2 |> Seq.cast<float> |> Seq.toList
您可能希望将其设为通用:
let toList<'a> (data2: 'a [,]) = data2 |> Seq.cast<'a> |> Seq.toList
编辑:看起来你想要将数组映射到行元素列表而不是展平它。在这种情况下,您可以这样做:
let aggrArrays (data2:float[,]) =
[0 .. (Array2D.length1 data2) - 1]
|> List.map (fun i -> (data2.[i, 0], data2.[i, 1], data2.[i, 2]))
|> Seq.groupBy id
|> Seq.map (fun (key, group) -> group |> Seq.reduce (fun (a,b,c) (x, y, z) -> a, b+y , (b*c+y*z*1.)/(b+y)))
|> Seq.toArray