Created
October 13, 2022 16:26
-
-
Save aloisdg/f3ae6cf5432430f3441217113d5c85e9 to your computer and use it in GitHub Desktop.
Deconstruct Lookup
This file contains 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
// 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