choose()中的数组索引器不起作用

时间:2014-06-22 03:29:08

标签: f#

在我试图学习F#时,请原谅我的尘土。

我有一个函数,它给我一个从CSV文件中读取的Seq of Arrays。这些数组的每个元素代表一个列数据。

let file = readFile("""C:\path\to\file.csv""")

第一列是我在这里尝试获取的日期是我的代码

let dates = 
file
|> Seq.skip(1)
|> Seq.choose(fun x -> x.[0])

我收到以下编译错误

error FS0001: This expression was expected to have type    'a option

我使用它错了吗?当我将鼠标指向'x'时,intellisense告诉我x的类型为string []

2 个答案:

答案 0 :(得分:4)

你真正想要的是

let dates = 
file
|> Seq.skip(1)
|> Seq.map(fun x -> x.[0])

Seq.choose也会进行过滤,但由于您没有使用过滤功能,因此您只需使用map

答案 1 :(得分:3)

我修好了。 Some()是我想要的。

let dates = 
file
|> Seq.skip(1)
|> Seq.choose(fun x -> Some(x.[0]))