Skip to content

Instantly share code, notes, and snippets.

@fekberg
Created March 19, 2020 13:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fekberg/7f0966217a7d4698149f11b0d84ae1e7 to your computer and use it in GitHub Desktop.
Save fekberg/7f0966217a7d4698149f11b0d84ae1e7 to your computer and use it in GitHub Desktop.
Pattern Matching.cs
using System;
using System.Drawing;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
namespace Recrusive_Patterns
{
class Program
{
static async Task Main(string[] args)
{
new Program().Example1();
}
#region Example 1
public void Example1()
{
object fuit = new Apple();
var food = fuit switch
{
Apple apple => MakeApplePieFrom(apple),
Orange orange => MakeJuiceFrom(orange),
_ => throw new UnsupportedFruitException()
};
Console.WriteLine(food);
#region Tuple + Property Pattern
var a1 = new Apple { HasExpired = false };
var o1 = new Orange { HasExpired = true };
#endregion
var food2 = (o1, a1) switch
{
// (Apple { HasExpired: false }, Orange { HasExpired: true }) => "Apple pie",
({ HasExpired: false }, { HasExpired: true }) => "Apple pie",
({ HasExpired: true }, { HasExpired: false }) x => $"Orange juice | HasExpired = {x.o1.HasExpired}",
({ HasExpired: false }, { HasExpired: false }) => "Fruit sallad",
_ => throw new UnsupportedFruitException()
};
Console.WriteLine(food2);
}
public double MakeApplePieFrom(Apple apple)
{
return Math.PI;
}
public double MakeJuiceFrom(Orange orange) => 0d;
#endregion
#region Example 2
static ShippingStatus GetOrderStatus(Order order)
{
return order switch
{
CancelledOrder _ => ShippingStatus.Return,
// Positional Pattern
FinalizedOrder(ShippingStatus.Shipped, _, _, _) => ShippingStatus.Shipped,
// Tuple Pattern
(ShippingStatus.WaitingForPayment, PaymentStatus.Paid, _, _) => ShippingStatus.ReadyForShippment,
(ShippingStatus.ReadyForShippment, PaymentStatus.PartiallyPaid, var amountDue, _) when amountDue < 200 => ShippingStatus.ReadyForShippment,
(_, _, 0, _) => ShippingStatus.ReadyForShippment,
// Property Pattern
FinalizedOrder { AmountDue: 0 } => ShippingStatus.ReadyForShippment,
// Default Pattern
_ => ShippingStatus.WaitingForPayment
};
}
#endregion
#region Example 3
static string VisibilityFrom(bool? state)
{
return state switch
{
true => "Visible",
false => "Hidden",
_ => "Blink"
};
}
#endregion
#region Example 4
static async Task<object> ProcessMessage(HttpResponseMessage message)
{
var shape = (message.StatusCode, message.IsSuccessStatusCode) switch
{
(HttpStatusCode.NotModified, true) => await FromCache(),
(HttpStatusCode.OK, _) => await ExtractShape(message),
(_, true) => await ExtractShape(message),
(HttpStatusCode.RequestTimeout, _) => await ProcessMessage(message),
_ => throw new Exception("Network error")
};
return shape;
}
#endregion
#region Data
private static Task<object> FromCache()
=> Task.FromResult((object)new Rectangle { Height = 200, Width = 400 });
public static Task<object> ExtractShape(HttpResponseMessage message)
=> Task.FromResult((object)new Rectangle { Height = 200, Width = 400 });
#endregion
}
class Apple
{
public Color Color { get; set; }
public bool HasExpired { get; set; }
}
class Orange
{
public Color Color { get; set; }
public bool HasExpired { get; set; }
}
public class UnsupportedFruitException : Exception {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment