【发布时间】:2016-06-26 01:34:59
【问题描述】:
我一直在关注this F# ROP article,并决定尝试用 C# 重现它,主要是看看我是否可以。抱歉这个问题太长了,但如果你熟悉 ROP,它会很容易理解。
他从一个 F# 有区别的联合开始......
type Result<'TSuccess, 'TFailure> =
| Success of 'TSuccess
| Failure of 'TFailure
...我将其翻译成一个抽象的 RopValue 类,以及两个具体的实现(请注意,我已将类名更改为我更好理解的类名)...
public abstract class RopValue<TSuccess, TFailure> {
public static RopValue<TSuccess, TFailure> Create<TSuccess, TFailure>(TSuccess input) {
return new Success<TSuccess, TFailure>(input);
}
}
public class Success<TSuccess, TFailure> : RopValue<TSuccess, TFailure> {
public Success(TSuccess value) {
Value = value;
}
public TSuccess Value { get; set; }
}
public class Failure<TSuccess, TFailure> : RopValue<TSuccess, TFailure> {
public Failure(TFailure value) {
Value = value;
}
public TFailure Value { get; set; }
}
我添加了一个静态 Create 方法,允许您从 TSuccess 对象创建一个 RopValue,该对象将被输入到第一个验证函数中。
然后我开始编写一个绑定函数。 F#版本如下...
let bind switchFunction twoTrackInput =
match twoTrackInput with
| Success s -> switchFunction s
| Failure f -> Failure f
...与 C# 等价物相比,这简直是轻而易举!我不知道是否有更简单的方法来写这个,但这是我想出的......
public static RopValue<TSuccess, TFailure> Bind<TSuccess, TFailure>(this RopValue<TSuccess, TFailure> input, Func<RopValue<TSuccess, TFailure>, RopValue<TSuccess, TFailure>> switchFunction) {
if (input is Success<TSuccess, TFailure>) {
return switchFunction(input);
}
return input;
}
请注意,我将其编写为扩展函数,因为它允许我以更实用的方式使用它。
以他验证一个人的用例,然后我写了一个 Person 类...
public class Person {
public string Name { get; set; }
public string Email { get; set; }
public int Age { get; set; }
}
...并编写了我的第一个验证函数...
public static RopValue<Person, string> CheckName(RopValue<Person, string> res) {
if (res.IsSuccess()) {
Person person = ((Success<Person, string>)res).Value;
if (string.IsNullOrWhiteSpace(person.Name)) {
return new Failure<Person, string>("No name");
}
return res;
}
return res;
}
通过几个类似的电子邮件和年龄验证,我可以编写如下的整体验证函数...
private static RopValue<Person, string> Validate(Person person) {
return RopValue<Person, string>
.Create<Person, string>(person)
.Bind(CheckName)
.Bind(CheckEmail)
.Bind(CheckAge);
}
这很好用,让我可以做这样的事情......
Person jim = new Person {Name = "Jim", Email = "", Age = 16};
RopValue<Person, string> jimChk = Validate(jim);
Debug.WriteLine("Jim returned: " + (jimChk.IsSuccess() ? "Success" : "Failure"));
但是,我在执行此操作时遇到了一些问题。首先是验证函数要求你传入一个 RopValue,检查它是否成功或失败,如果成功,则取出 Person 然后验证它。如果失败,直接返回即可。
相比之下,他的验证函数接受(相当于)一个 Person,并返回(一个 Result,相当于)一个 RopValue...
let validateNameNotBlank person =
if person.Name = "" then Failure "Name must not be blank"
else Success person
这要简单得多,但我无法弄清楚如何在 C# 中执行此操作。
另一个问题是我们以 Success 开始验证链,因此第一个验证函数将始终从“if”块返回一些内容,如果验证失败则返回 Failure,如果验证失败则返回 Success我们通过了检查。如果一个函数返回 Failure,那么验证链中的下一个函数永远不会被调用,因此我们知道这些方法永远不能传递一个 Failure。因此,永远无法到达这些函数的最后一行(除了您手动创建一个 Failure 并在开始时将其传递的奇怪情况,但那将毫无意义)。
然后他创建了一个开关运算符 (>=>) 来连接验证函数。我试过这样做,但无法让它工作。为了链接对函数的连续调用,看起来我必须在 Func 上有一个扩展方法,我认为你不能这样做。我已经做到了……
public static RopValue<TSuccess, TFailure> Switch<TSuccess, TFailure>(this Func<TSuccess, RopValue<TSuccess, TFailure>> switch1, Func<TSuccess, RopValue<TSuccess, TFailure>> switch2, TSuccess input) {
RopValue<TSuccess, TFailure> res1 = switch1(input);
if (res1.IsSuccess()) {
return switch2(((Success<TSuccess, TFailure>)res1).Value);
}
return new Failure<TSuccess, TFailure>(((Failure<TSuccess, TFailure>)res1).Value);
}
...但不知道如何使用它。
那么,谁能解释我将如何编写 Bind 函数,以便它可以接受一个 Person 并返回一个 RopValue(就像他的那样)?另外,如何编写一个允许我连接简单验证功能的开关功能?
欢迎使用我的代码中的任何其他 cmets。我不确定它是否尽可能简洁。
【问题讨论】:
-
这一切都让我觉得类似于
Error<T>monad。你觉得它是,但TFailure类型实际上等同于Exception? -
@Enigmativity 引用马克吐温的话,“我很高兴能够快速回答,我说我不知道!” - 我是一名 C# 程序员,同时学习 F# 和函数式编程。我见过讨论单子,但还没有弄清楚它们是什么。
-
你的
RopValue类型是一个单子。基本上单子赋予普通类型超能力。 -
@AvrohomYisroel 您可以将 monad 视为函数式程序员使用的一种设计模式。您已经熟悉的一个简单示例是 List monad,它基本上是 LINQ 的一个实现。这里
bind函数称为SelectMany,如果您查看该函数的类型签名,您会发现它与我在答案中显示的形式相匹配。 Monad 还允许您做一些更有趣的事情,例如创建一个引用透明的跟踪状态的方式。 -
@Enigmativity "基本上单子赋予普通类型超能力" - 有点像编码世界的 X-Kryptonite 嗯?
标签: c# f# functional-programming