how to make a right of either in c functional programming?

// 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())

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[]{ });

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top