public void AnonymousDictionaryConstructors()
    {
        var r = new AnonymousReadOnlyDictionary <int, int>(
            () => 5,
            5.Range(),
            (int k, out int v) => {
            v = k;
            return(k >= 0 && k < 5);
        });

        new AnonymousReadOnlyDictionary <int, int>(
            5.Range(),
            (int k, out int v) => {
            v = k;
            return(k >= 0 && k < 5);
        }).AssertDictionaryEquals(r);
        r.Count.AssertEquals(5);
        r.Keys.AssertSequenceEquals(5.Range());
        int x;

        r.TryGetValue(-1, out x).AssertIsFalse();
        r.TryGetValue(5, out x).AssertIsFalse();
        for (var i = 0; i < 5; i++)
        {
            r.TryGetValue(i, out x).AssertIsTrue();
            x.AssertEquals(i);
        }
    }
 public void AnonymousDictionaryConstructors()
 {
     var r = new AnonymousReadOnlyDictionary<int, int>(
         () => 5,
         5.Range(),
         (int k, out int v) => {
             v = k;
             return k >= 0 && k < 5;
         });
     new AnonymousReadOnlyDictionary<int, int>(
         5.Range(),
         (int k, out int v) => {
             v = k;
             return k >= 0 && k < 5;
         }).AssertDictionaryEquals(r);
     r.Count.AssertEquals(5);
     r.Keys.AssertSequenceEquals(5.Range());
     int x;
     r.TryGetValue(-1, out x).AssertIsFalse();
     r.TryGetValue(5, out x).AssertIsFalse();
     for (var i = 0; i < 5; i++) {
         r.TryGetValue(i, out x).AssertIsTrue();
         x.AssertEquals(i);
     }
 }
Пример #3
0
        public MainWindow()
        {
            InitializeComponent();

            // all of these examples create lists with constant-time operations that don't get more expensive as the list grows.
            // these operations include:
            // - creating the list
            // - determining the number of items in the list
            // - accessing items by index in the list
            // - advancing an enumeration of the list
            // also, only a constant amount of memory is used per list (except for what's used to show their items as text)

            Show("Naturals less than A", (a, b, c) =>
                 a.Range());

            Show("B", (a, b, c) =>
                 b);

            Show("C", (a, b, c) =>
                 c);

            Show("Last A bytes", (a, b, c) =>
                 ReadOnlyList.AllBytes().TakeLast(a));

            Show("B reversed", (a, b, c) =>
                 b.Reverse());

            Show("Take up to A of C", (a, b, c) =>
                 c.Take(a));

            Show("Stride to a million by A", (a, b, c) =>
                 1000000.Range().Stride(a));

            Show("C with item letters sorted", (a, b, c) =>
                 c.Select(e => new String(e.OrderBy(character => character).ToArray())));

            Show("Pairwise equals of B,C", (a, b, c) =>
                 b.Zip(c, (itemB, itemC) => itemB == itemC));

            Show("Skip first and last A of B", (a, b, c) =>
                 b.Skip(a).SkipLast(a));

            Show("Skip EXACTLY last A of B", (a, b, c) =>
                 b.SkipLastRequire(a));

            Show("Partition C by A, concatenating", (a, b, c) =>
                 from g in c.Partition(a)
                 select String.Join("", g));

            Show("Cube map", (a, b, c) => {
                var count     = 100;
                var squareMap = new AnonymousReadOnlyDictionary <int, int>(
                    count.Range(),
                    (int key, out int value) => {
                    value = key * key;
                    return(key >= 0 && key < count);
                });
                var cubeMap = squareMap.SelectValue(e => e.Key * e.Value);
                return(cubeMap); // treated as an IReadOnlyCollection<KeyValuePair<int, int>>
            });
        }