英文:
How to implement an "OR" condition on two strings with Result Validation in F#?
问题
我正试图在验证字符串时实现一个“或”条件。我失败得很惨。
在F#中给出以下代码:
let isSearchLastName input =
let (fn:string,ln:string) = input
if String.IsNullOrEmpty(ln.Trim()) then Error input else Ok input
let isSearchFirstName input =
let (fn:string,ln:string) = input
if String.IsNullOrEmpty(fn.Trim()) then Error input else Ok input
let validatenamesearch = isSearchLastName ||| isSearchFirstName
let isSearchNameValid = validatenamesearch (m.SearchLastName, m.SearchFirstName) |> Result.Ok
其中,||| 是:
let oorr addSuccess addFailure switch1 switch2 x =
match (switch1 x),(switch2 x) with
| Ok s1,Ok s2 -> Ok (addSuccess s1 s2)
| Error f1,Ok s2 -> Ok (addSuccess f1 s2)
| Ok s1 ,Error f2 -> Ok (addSuccess s1 f2)
| Error f1,Error f2 -> Error (addFailure f1 f2)
// 创建一个“或”函数用于验证函数
let (|||) v1 v2 =
let addSuccess r1 r2 = r1,r2
let addFailure s1 s2 = s1,s2
oorr addSuccess addFailure v1 v2
我试图像下面这样使用它:
if isSearchNameValid then ......
但我得到编译器的投诉:
val isSearchNameValid: Result<Result<((string*string)*(string*string)),((string*string)*(string*string))>,obj>
我希望实现的是一种验证方法,如果任一字符串为空,则结果将成功(带有Ok),然后我可以使用Ok返回的值(如果失败,即两个字符串都为空,则中止进一步处理)。最终,我希望能够使用|||运算符将多个“或”条件链接在一起。
非常感谢您的帮助。
英文:
I am attempting to implement an "or" condition in validating strings. I am failing miserably.
Given the following in F#:
let isSearchLastName input = let (fn:string,ln:string) = input
if String.IsNullOrEmpty(ln.Trim()) then Error input else Ok input
let isSearchFirstName input = let (fn:string,ln:string) = input
if String.IsNullOrEmpty(fn.Trim()) then Error input else Ok input
let validatenamesearch = isSearchLastName ||| isSearchFirstName
let isSearchNameValid = validatenamesearch (m.SearchLastName, m.SearchFirstName) |> Result.Ok
Where, ||| is:
let oorr addSuccess addFailure switch1 switch2 x =
match (switch1 x),(switch2 x) with
| Ok s1,Ok s2 -> Ok (addSuccess s1 s2)
| Error f1,Ok s2 -> Ok (addSuccess f1 s2)
| Ok s1 ,Error f2 -> Ok (addSuccess s1 f2)
| Error f1,Error f2 -> Error (addFailure f1 f2)
// create a "or" function for validation functions
let (|||) v1 v2 =
let addSuccess r1 r2 = r1,r2
let addFailure s1 s2 = s1,s2
oorr addSuccess addFailure v1 v2
I am attempting to use the above something like:
if isSearchNameValid then ......
What I am getting is the compiler complaining with:
val isSearchNameValid: Result<Result<((string*string)*(string*string)),((string*string)*(string*string))>,obj>
What I am hoping to achieve is a validation method where should either string be non-empty, the result will be success (with Ok) and I can then use the returned values from the Ok. (or if fails --i.e., neither string is non-empty--then abort further processing). Eventually, I would like to be able to chain together multiple "OR" conditions using the ||| operator.
Any help is most appreciated.
TIA
答案1
得分: 2
我认为这将实现你想要的:
```fsharp
let (|||) op1 op2 arg =
match op1 arg with
| Ok value -> Ok value
| Error _ -> op2 arg
let isSearchNameValid =
isSearchLastName ||| isSearchFirstName
示例:
let john = "John", "Lennon"
isSearchNameValid john |> printfn "%A" // Ok ("John", "Lennon")
let paul = "Paul", ""
isSearchNameValid paul |> printfn "%A" // Ok ("Paul", "")
let blank = "", ""
isSearchNameValid blank |> printfn "%A" // Error ("", "")
<details>
<summary>英文:</summary>
I think this will do what you want:
```fsharp
let (|||) op1 op2 arg =
match op1 arg with
| Ok value -> Ok value
| Error _ -> op2 arg
let isSearchNameValid =
isSearchLastName ||| isSearchFirstName
Example:
let john = "John", "Lennon"
isSearchNameValid john |> printfn "%A" // Ok ("John", "Lennon")
let paul = "Paul", ""
isSearchNameValid paul |> printfn "%A" // Ok ("Paul", "")
let blank = "", ""
isSearchNameValid blank |> printfn "%A" // Error ("", "")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论