Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
string.IsNullOrEmpty()的F#等价物是什么?_F# - Fatal编程技术网

string.IsNullOrEmpty()的F#等价物是什么?

string.IsNullOrEmpty()的F#等价物是什么?,f#,F#,在F#中给出以下记录: F与C的等价物是什么 TIA假设要将无值视为空值或空值,可以使用选项。折叠: let m = { Text = Some "label" } m.Text |> Option.fold (fun _ s -> String.IsNullOrEmpty(s)) true 使用折叠的一个缺点是在累加器功能中忽略累加器参数。在这种情况下,您只需要在Some的情况下应用一个函数,如果选项为None,则需要使用一个默认值,例如 let getOr

在F#中给出以下记录:

F与C的等价物是什么


TIA

假设要将
值视为空值或空值,可以使用
选项。折叠

let m = { Text = Some "label" }
m.Text |> Option.fold (fun _ s -> String.IsNullOrEmpty(s)) true
使用
折叠的一个缺点是在累加器功能中忽略累加器参数。在这种情况下,您只需要在
Some
的情况下应用一个函数,如果选项为
None
,则需要使用一个默认值,例如

let getOr ifNone f = function
    | None -> ifNone
    | Some(x) -> f x
然后你可以用

m.Text |> getOr true String.IsNullOrEmpty

Lees的答案可能是功能上最惯用的,即当您想要处理数据类型时,可以使用fold将其压缩成答案。但请注意,您可以删除“s”,使其成为

m.Text |> Option.fold (fun _ -> String.IsNullOrEmpty) true
如果折叠还不是一件让你感到轻松的事情,那么一个更加面向集合的版本会是,“它们都是空的吗?”(如果没有,那么它们都是空的)

或者简而言之

m.Text |> Option.forall String.IsNullOrEmpty

(我个人会使用这个)

选项。toObj
函数将包含引用类型的选项转换为可为空的对象。
None
将转换为
null
。这对于与预期为空的API进行互操作非常有用:

model.Text |>Option.toObj |>String.IsNullOrEmpty

See我在F#中做了很多工作,但从未见过
选项。使用了fold
,所以我认为它不是惯用的。这让我有点困惑。我必须盯着它看一段时间,才能确切地知道它在做什么。好吧,但它通常是函数式编程的惯用用法,“折叠”(和展开)是几乎所有非平凡算法的基本构建块。我只是想…这是一个数组/seq/list,其中包含1或0个元素。您的
getOr
函数几乎已经存在,尽管参数顺序相反
defaultArg(m.Text |>Option.map String.IsNullOrEmpty)true
m.Text |> Option.fold (fun _ -> String.IsNullOrEmpty) true
m.Text |> Option.forall (fun s -> String.IsNullOrEmpty s)
m.Text |> Option.forall String.IsNullOrEmpty