【发布时间】:2019-05-21 13:49:55
【问题描述】:
让我在 C# 上有这样的代码。如何用 F# 以函数样式重写这个早春逻辑?我应该使用什么模式匹配?主动模式匹配?可区分联合?
public class DataBase
{
public List<string> GetEmployees(string id, string email)
{
if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(email))
{
return GetByIdAndEmail(id, email);
}
else if (!string.IsNullOrEmpty(email))
{
return GetByEmail(email);
}
else if (!string.IsNullOrEmpty(id))
{
return GetById(id);
}
else
{
return new List<string>();
}
}
private List<string> GetByIdAndEmail(string id, string email)
{
// request something in db
return new List<string>() { "First" };
}
private List<string> GetByEmail(string email)
{
//request something in db
return new List<string>() { "Second" };
}
private List<string> GetById(string id)
{
// request something in db
return new List<string>() { "Third" };
}
}
class Program
{
static void Main(string[] args)
{
DataBase DB = new DataBase();
string id = null;
string email = null;
DB.GetEmployees(id, email);
}
}
F#
let GetEmployees (id:string)(email:string) =
match (id,email) with
...
【问题讨论】:
标签: f#