From 238814edbb5538bc3a0d8189a09dd842210042dd Mon Sep 17 00:00:00 2001 From: Oli Sturm Date: Wed, 5 Oct 2022 11:51:55 +0100 Subject: [PATCH] adding CS11 list patterns --- samples/CS11/CS11.csproj | 10 ++++++++ samples/CS11/Program.cs | 49 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 samples/CS11/CS11.csproj create mode 100644 samples/CS11/Program.cs diff --git a/samples/CS11/CS11.csproj b/samples/CS11/CS11.csproj new file mode 100644 index 0000000..d439800 --- /dev/null +++ b/samples/CS11/CS11.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/samples/CS11/Program.cs b/samples/CS11/Program.cs new file mode 100644 index 0000000..50afaed --- /dev/null +++ b/samples/CS11/Program.cs @@ -0,0 +1,49 @@ +namespace CS11 { + class Program { + static void Main(string[] args) { + int[] numbers = { 1, 3, 42 }; + // Exact match + Console.WriteLine($"numbers is [1,3,42]: {numbers is [1, 3, 42]}"); + // Must be correct length, so this is false + Console.WriteLine($"numbers is [1,3]: {numbers is [1, 3]}"); + // Can use wildcards + Console.WriteLine($"numbers is [1,3,_]: {numbers is [1, 3, _]}"); + // Still must be correct length! + Console.WriteLine($"numbers is [1,3,_,_]: {numbers is [1, 3, _, _]}"); + + // Really don't care about length? Use .. + Console.WriteLine($"numbers is [..,42]: {numbers is [.., 42]}"); + + // Cool stuff, match with embedded patterns - this is true + Console.WriteLine($"numbers is [1,3,>10]: {numbers is [1, 3, > 10]}"); + // ... and this is false + Console.WriteLine($"numbers is [1,3,>100]: {numbers is [1, 3, > 100]}"); + + + // Docs at https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/patterns#list-patterns + // show an example with CSV import -- cool idea, but of course + // the CSV content is always a list of strings so advanced + // patterns can't be used for non-string content. + + // What the docs don't tell you (at least I didn't see it + // anywhere -- found it in GitHub), you can split head and tail + // On second read, I found this: + // "The var pattern can capture a single element, or a range of elements." + // I imagine that somebody thought this would be sufficient. + if (numbers is [var x, .. var xs]) { + Console.WriteLine($"Head: {x}"); + Console.WriteLine($"Tail: {xs}"); + } + + // So we can do nice functional-style stuff: + Console.WriteLine($"Sum of numbers: {Sum(numbers)}"); + } + + // Now we're talking + static int Sum(int[] l) => l switch + { + [] => 0, + [var x, .. var xs] => x + Sum(xs) + }; + } +}