Skip to content

Instantly share code, notes, and snippets.

@MichalBrylka
Last active March 11, 2020 07:53
Show Gist options
  • Save MichalBrylka/6f352829b73a167d73ede065d68e5eae to your computer and use it in GitHub Desktop.
Save MichalBrylka/6f352829b73a167d73ede065d68e5eae to your computer and use it in GitHub Desktop.
//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