Arrays 如何将带选项int-elements的Array2D转换为带int-elements的Array2D
我正在尝试将带有选项int元素的Array2DArrays 如何将带选项int-elements的Array2D转换为带int-elements的Array2D,arrays,f#,option,Arrays,F#,Option,我正在尝试将带有选项int元素的Array2DoptionArr转换为带有int元素的Array2Darr: let arr = optionArr |> Array2D.map (fun x -> match Option.toArray x with | [| |] -> -1 | [| v |] -> v) 但是,Visual Studio 2013强调了从Array2D.map…到的所有内容v)
optionArr
转换为带有int元素的Array2Darr
:
let arr =
optionArr
|> Array2D.map (fun x ->
match Option.toArray x with
| [| |] -> -1
| [| v |] -> v)
但是,Visual Studio 2013强调了从Array2D.map…
到的所有内容v)
用红色表示,并表示:
Type mismatch. Expecting a
int [,] option -> 'a
but given a
'b [,] -> 'c [,]
The type 'int [,] option' does not match the type ''a [,]'
我一直试图“修复”我的代码,但我不知道我做错了什么,也不知道上面的错误消息暗示了什么
编辑
我应用了(它本身使用),但当我意识到消息清楚地表明Array2D
arr
类型为int[,]option
而不是int option[,]
时,仍然得到了上述错误消息。应用相同的逻辑,我更正的代码如下:
let arr = defaultArg optionArr (Array2D.zeroCreate 0 0)
将选项
值视为“正常”值似乎非常有用
let arr optionArr =
optionArr
|> Array2D.map (fun x ->
match x with
| Some(y) -> y
| None -> -1)
用法
用法
很好。也可以更简单地直接使用:
// Create our array
let optionArr = Array2D.create 10 10 (Some(1))
let noneToMinusOne x = defaultArg x -1
let result = optionArr |> Array2D.map noneToMinusOne
很好。也可以更简单地直接使用:
// Create our array
let optionArr = Array2D.create 10 10 (Some(1))
let noneToMinusOne x = defaultArg x -1
let result = optionArr |> Array2D.map noneToMinusOne
您如何声明optionArr?它真的是2D数组还是锯齿数组?它不是锯齿数组,而是2D数组。有关声明,请参阅Tomas Petricek的答案。如何声明
optionArr
?它真的是2D数组还是锯齿数组?它不是锯齿数组,而是2D数组。有关声明,请参阅Tomas Petricek的答案。您也可以执行|>Array2D.map(fun x->defaultArg x-1)
-请参阅:您也可以执行|>Array2D.map(fun x->defaultArg x-1)
-请参阅: