我正在尝试使用c#学习函数式编程
我正在使用https://github.com/louthy/language-ext
如何才能在不使用.Match的情况下从这两个值中提取正确的值
如果没有临时变量,如何返回UpdateLocations的结果?
任一
public static Either<Exception, string[]> ExtractStringArray(IQueryCollection query, string filter)
{
try
{
return query.First(x => x.Key.ToLower() == filter).Value.ToString().Split(',').ToArray();
}
catch (Exception ex)
{
return ex;
}
}异常情况
static void HandleException(Exception ex) => Console.WriteLine(ex);我正在使用下面的代码提取输出,有没有其他选择?
private static string[] UpdateLocations(IQueryCollection query)
{
// creating a temporary value here
string[] locations = new string[] { };
ExtractStringArray(query, "location")
.Match(
Left: ex => HandleException(ex),
Right: loc => locations = loc);
return locations;
}更新变量
var query = request.Query;
result.Locations = UpdateLocations(query);发布于 2020-11-02 17:23:10
// simplify ExtractStringArray using Try
static Either<Exception, string[]> ExtractStringArray(IQueryCollection query, string filter) =>
Try(() => query.First(x => x.Key.ToLower() == filter).Value.ToString().Split(',').ToArray())
.ToEither();
static void HandleException(Exception ex) => Console.WriteLine(ex);
var query = request.Query;
// now get result or exception (Either<...>)
var resultOrException = ExtractStringArray(query, "location");
// now execute side-effect if exception
resultOrException.IfLeft(HandleException); // side-effect (log error)
// now use result and supply default value in case of exception
result.Locations = resultOrException.IfLeft(new string[]{ });https://stackoverflow.com/questions/64636634
复制相似问题