Skip to content

Instantly share code, notes, and snippets.

@Pzixel
Created December 9, 2019 00:36
Show Gist options
  • Save Pzixel/7bfb191cc5f06fb3961214de7132482a to your computer and use it in GitHub Desktop.
Save Pzixel/7bfb191cc5f06fb3961214de7132482a to your computer and use it in GitHub Desktop.
public static class XEnumerable
{
public static (T1[], T2[]) Unzip<T1, T2>(this IEnumerable<(T1 x, T2 y)> source)
{
if (source is IReadOnlyCollection<(T1, T2)> roc)
{
T1[] xs = new T1[roc.Count];
T2[] ys = new T2[roc.Count];
int i = 0;
foreach (var (x, y) in source)
{
xs[i] = x;
ys[i] = y;
i++;
}
return (xs, ys);
}
var xsList = new List<T1>();
var ysList = new List<T2>();
foreach (var (x, y) in source)
{
xsList.Add(x);
ysList.Add(y);
}
return (xsList.ToArray(), ysList.ToArray());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment