1

我已经seq<Nullable<int>>并且需要创建漂亮的情节,但没有空值。这是我的代码:

open System
#r """..\packages\FSharp.Charting.0.90.14\lib\net40\FSharp.Charting.dll"""
#load """..\packages\FSharp.Charting.0.90.14\FSharp.Charting.fsx"""
open FSharp.Charting

//in a real world replaced by .csv with empty values
let seqWithNullInt = seq[Nullable 10 ; Nullable 20  ; Nullable (); Nullable 40; Nullable 50] 
//let seqWithNullInt = seq[ 10 ; 20  ;  30;  40; 50]  //works fine

let bothSeq = seqWithNullInt |> Seq.zip {1..5}

Chart.Line bothSeq // Error because of nullable int 

这是我的愿景:

预期图表

如何跳过空值?我不想用最近的东西替换它们,我需要从图表中跳过它们。有什么解决方案吗?

4

1 回答 1

2

像这样的东西可能会起作用(请注意,我使用Option了值而不是 nullables,因为这在 F# 中更惯用):

let neitherPairHasNoneInValue (pair1, pair2) =
    pair1 |> snd |> Option.isSome && pair2 |> snd |> Option.isSome
let seqWithNone = Seq.ofList [Some 10; Some 20; None; Some 40; Some 50]
let pairsWithoutNone = seqWithNone
                       |> Seq.zip {1..5}
                       |> Seq.pairwise
                       |> Seq.filter neitherPairHasNoneInValue
printfn "%A" pairsWithoutNone

这将输出[(1,10),(2,20) ; (4,40),(5,50)]. 我不知道 FSharp.Charting API,所以我不能告诉你哪个函数会获取 X、Y 对的列表并绘制你想要的图表,但从那里到你的图表应该相对简单。

于 2017-04-21T03:57:16.433 回答