我有一个返回一系列记录的函数。在该函数中,我以空白的虚拟记录开始列表构建(可能有更好的方法),因为我需要累积相似的记录,所以我用空白记录“启动泵”。这是我的代码:
let consolidate(somethings:seq<Something>) =
let mutable results = ResizeArray()
let mutable accumulatedSomething = {Foo = ""; Bar = ""; Count = 0;}
for s in somethings do
if s.Foo = accumulatedSomething.Foo && s.Bar = accumulatedSomething.Bar then
accumulatedSomething <- {Foo = s.Foo; Bar = s.Bar;
Count = s.Count + accumulatedSomething.Count}
else
results.Add(accumulatedSomething)
accumulatedSomething <- e
results |> Seq.cast |> Seq.skip 1
如果你有办法让它变得更好,我会全力以赴(我仍在程序上思考),但我仍然对这个特定问题的答案感兴趣。稍后在我的代码中,我尝试打印出列表:
somethings |> Seq.iter( fun s -> printfn "%A" s)
当列表中有东西时,这可以正常工作。但是,如果列表为空并且列表中的唯一记录是跳过的空白起始记录,则此行将失败并InvalidOperationException
显示消息The input sequence has an insufficient number of elements?
为什么会发生这种情况,我该如何解决?