Last active
March 11, 2020 07:53
-
-
Save MichalBrylka/6f352829b73a167d73ede065d68e5eae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Head+Tail for ROS<T> | |
static void Deconstruct<T>(this ReadOnlySpan<T> span, out T head, out ReadOnlySpan<T> tail) | |
{ | |
switch (span.Length) | |
{ | |
case 0: | |
head = default; | |
tail = default; | |
return; | |
case 1: | |
head = span[0]; | |
tail = default; | |
return; | |
default: | |
head = span[0]; | |
tail = span.Slice(1, span.Length - 1); | |
return; | |
} | |
} | |
static int Sum(ReadOnlySpan<int> list) => list switch | |
{ | |
{ Length: 0 } => 0, | |
var (head, tail) => head + Sum(tail), | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment