我写了一个简单的函数来平均序列的一部分。第一个"列"有日期,第二个值。我有三个要检查的变量,开始日期(std)结束日期(edd)和businesshour(定义为8-20 Mo-Fr = pob:string)。
这是功能:
let internal aggrAvg data std edd pob =
data
|> Seq.averageBy (fun(a,b) ->
match a with
| r when isbetween r std edd ->
match pob with
|"Peak" when (ispeak a) -> b
|"Offpeak" when (not (ispeak a)) -> b
| _ -> b
| _ -> 0.0
)
这有两个问题:
match pob with
总是失败我有什么想法可以解决这两个问题?感谢
根据Matthew的回答更新工作解决方案
let internal aggrAvg data std edd pob =
let ndata = data |> Seq.filter (fun (date, _) -> isbetween date std edd)
match pob with
| "Peak" -> ndata |> Seq.filter (fun (date, _) -> ispeak date) |> Seq.averageBy (fun (_, value) -> value)
| "Offpeak" -> ndata |> Seq.filter (fun (date, _) -> not(ispeak date)) |> Seq.averageBy (fun (_, value) -> value)
| _ -> ndata |> Seq.averageBy (fun (_, value) -> value)
答案 0 :(得分:2)
因为pob不是序列的一部分,所以匹配pob总是如此 失败
无论如何,总是会产生价值b,它的目的是什么?
要跳过不在您的范围之间的日期,您可以在执行平均值之前过滤序列:
let internal aggrAvg data std edd pob =
data
|> Seq.filter (fun (date, _) -> isbetween date std edd)
|> Seq.averageBy (fun (_, value) -> value)
更新回答:
let internal aggrAvg data std edd pob =
data
|> Seq.filter (fun (date, _) ->
let dateInRange = isbetween date std edd
match pob with
| "Peak" -> ispeak date && dateInRange
| "Offpeak" -> not(ispeak date) && dateInRange
| _ -> dateInRange)
|> Seq.averageBy (fun (_, value) -> value)