F# 如何从复合泛型类型检索值?
如何从泛型中检索值 具体而言,我正在尝试以下内容:F# 如何从复合泛型类型检索值?,f#,F#,如何从泛型中检索值 具体而言,我正在尝试以下内容: // Test let result = Validate goodInput;; // How to access record?? let request = getRequest result > run goodInput;; val it : Result<unit,string> = Success unit > run badInput;; val it : Result<unit,string&
// Test
let result = Validate goodInput;;
// How to access record??
let request = getRequest result
> run goodInput;;
val it : Result<unit,string> = Success unit
> run badInput;;
val it : Result<unit,string> = Failure "Name must not be blank"
代码如下:
type Result<'TSuccess,'TFailure> =
| Success of 'TSuccess
| Failure of 'TFailure
let bind nextFunction lastFunctionResult =
match lastFunctionResult with
| Success input -> nextFunction input
| Failure f -> Failure f
type Request = {name:string; email:string}
let validate1 input =
if input.name = "" then Failure "Name must not be blank"
else Success input
let validate2 input =
if input.name.Length > 50 then Failure "Name must not be longer than 50 chars"
else Success input
let validate3 input =
if input.email = "" then Failure "Email must not be blank"
else Success input;;
let Validate =
validate1
>> bind validate2
>> bind validate3;;
// Setup
let goodInput = {name="Alice"; email="abc@abc.com"}
let badInput = {name=""; email="abc@abc.com"};;
// I have no clue how to do this...
let getRequest = function
| "Alice", "abc@abc.com" -> {name="Scott"; email="xyz@xyz.com"}
| _ -> {name="none"; email="none"}
// Test
let result = Validate goodInput;;
// How to access record??
let request = getRequest result
printfn "%A" result
类型结果=
|"成功"
|失败
让bind nextFunction lastFunctionResult=
将lastFunctionResult与
|成功输入->下一个功能输入
|故障f->故障f
类型请求={name:string;email:string}
让我们验证1个输入=
如果input.name=“”,则失败“名称不得为空”
其他成功输入
让我们验证2个输入=
如果input.name.Length>50,则失败“名称不得超过50个字符”
其他成功输入
让我们验证3个输入=
如果input.email=”“,则失败“电子邮件不得为空”
其他成功输入;;
让我们验证=
验证1
>>绑定验证2
>>绑定验证3;;
//设置
让goodInput={name=“Alice”;email=”abc@abc.com"}
让badInput={name=”“;电子邮件=”abc@abc.com"};;
//我不知道怎么做。。。
让getRequest=函数
|“爱丽丝”abc@abc.com“->{name=“Scott”;电子邮件=”xyz@xyz.com"}
|{name=“none”;email=“none”}
//试验
让结果=验证goodInput;;
//如何访问记录??
let request=getRequest结果
printfn“%A”结果
您的意思是如何从结果类型中提取记录?通过模式匹配,这就是您在bind
中已经在做的事情
let getRequest result =
match result with
| Success input -> input
| Failure msg -> failwithf "Invalid input: %s" msg
let result = Validate goodInput
let record = getRequest result
这将返回记录或引发异常。由您决定一旦获得
结果后如何处理成功和失败案例
——可能会引发异常,或将其转换为选项,或记录消息并返回默认值等。您的意思是如何从结果类型中提取记录?通过模式匹配,这就是您在bind
中已经在做的事情
let getRequest result =
match result with
| Success input -> input
| Failure msg -> failwithf "Invalid input: %s" msg
let result = Validate goodInput
let record = getRequest result
这将返回记录或引发异常。一旦得到
结果,您将如何处理成功和失败案例取决于您-这可能会引发异常,或将其转换为选项,或记录消息并返回默认值等。这似乎是一个常见问题:如何从一元值中获取值?我相信正确的答案是
一元值就是该值。
这就像在问,如何从整数列表中获取值,比如[1;3;3;7]
你没有;列表是值
那么,也许你会争辩说名单不是歧视性的工会;它们没有相互排斥的情况,如上面的结果
。考虑一下,一棵树:
type Tree<'a> = Node of Tree<'a> list | Leaf of 'a
如何从树中获取值?你没有;树就是价值
与F#中的选项类似,上面的结果
类型(实际上,它是任意一个单子)具有欺骗性,因为似乎只有一个值:成功。我们不喜欢思考的失败(就像我们不喜欢思考None
)
然而,这个类型不是这样工作的。失败案例与成功案例同样重要。这两个单子中的任何一个都经常被用来建模错误处理,它的全部要点是有一种类型安全的方法来处理错误,而不是异常,异常只不过是专门的、不确定的GOTO块
这就是原因
一元类型是什么。当您使用该类型时,您不应该从该世界中提取数据。相反,您应该将数据和功能提升到那个世界
回到上面的代码,假设给定一个有效的请求
值,您希望向该地址发送电子邮件。因此,您可以编写以下(不纯)函数:
let send { name = name; email = email } =
// Send email using name and email
此函数的类型为请求->单元
。请注意,它没有被提升到任何一个世界。尽管如此,如果请求有效,您仍然希望发送电子邮件,因此您将send
方法提升到以下两个级别:
let map f = bind (fun x -> Success (f x))
let run = validate1 >> bind validate2 >> bind validate3 >> map send
run
函数的类型为Request->Result
,因此与goodInput
和badInput
一起使用,结果如下:
// Test
let result = Validate goodInput;;
// How to access record??
let request = getRequest result
> run goodInput;;
val it : Result<unit,string> = Success unit
> run badInput;;
val it : Result<unit,string> = Failure "Name must not be blank"
此函数的类型为Result->string
,因此您可以使用它报告任何结果:
> run goodInput |> reportOnRun;;
val it : string = "Email was sent."
> run badInput |> reportOnRun;;
val it : string = "Name must not be blank"
在所有情况下,您都会返回一个可以显示给用户的字符串。这似乎是一个常见问题:如何从一元值中获取值?我相信正确的答案是
一元值就是该值。
这就像在问,如何从整数列表中获取值,比如[1;3;3;7]
你没有;列表是值
那么,也许你会争辩说名单不是歧视性的工会;它们没有相互排斥的情况,如上面的结果
。考虑一下,一棵树:
type Tree<'a> = Node of Tree<'a> list | Leaf of 'a
如何从树中获取值?你没有;树就是价值
与F#中的选项类似,上面的结果
类型(实际上,它是任意一个单子)具有欺骗性,因为似乎只有一个值:成功。我们不喜欢思考的失败(就像我们不喜欢思考None
)
然而,这个类型不是这样工作的。失败案例与成功案例同样重要。这两个单子中的任何一个都经常被用来建模错误处理,它的全部要点是有一种类型安全的方法来处理错误,而不是异常,异常只不过是专门的、不确定的GOTO块
这就是原因
一元类型是什么。当您使用该类型时,您不应该从该世界中提取数据。相反,您应该将数据和功能提升到那个世界
回到上面的代码,假设给定一个有效的请求
值,您希望向该地址发送电子邮件。因此,您可以编写以下内容(inpur