Skip to content

Instantly share code, notes, and snippets.

@aloisdg
Created October 13, 2022 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aloisdg/f3ae6cf5432430f3441217113d5c85e9 to your computer and use it in GitHub Desktop.
Save aloisdg/f3ae6cf5432430f3441217113d5c85e9 to your computer and use it in GitHub Desktop.
Deconstruct Lookup
// https://stackoverflow.com/questions/42549491/deconstruction-in-foreach-over-dictionary
// https://codereview.stackexchange.com/questions/233528/deconstruct-tuple-and-store-each-element-into-a-list-of-their-respective-element
// https://dotnetfiddle.net/GWXy8i
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
var(pairs, odds) = new[]{1, 2, 3}.ToLookup(x => x % 2 == 0);
pairs.Dump();
odds.Dump();
}
}
public static class Extensions
{
public static void Deconstruct<T>(this ILookup<bool, T> lookup, out IEnumerable<T> trues, out IEnumerable<T> falses)
{
trues = lookup[true ];
falses = lookup[false ];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment