Exemplo n.º 1
0
        public static IOrderedEnumerable <string> PossiblePropertyNames(this PropertyInfo propertyInfo,
                                                                        IEnumerable <string> schemaNames = null)
        {
            var possibleNames = new Func <PropertyInfo, string[]>[]
            {
                p => new[] { Inflector.MakeInitialUpperCase(p.DeclaringType?.Name) + Inflector.MakeInitialUpperCase(p.Name) },
                p => PrefixWithSchemaName(p.Name)
                .ToArray(),
                p => new[] { Inflector.MakeInitialUpperCase(p.Name) },
                p => new[] { Inflector.CollapseNames(p.DeclaringType?.Name, p.Name) },
                p => Inflector.StripLeftTerms(p.Name)
                .ToArray()
            };

            // Important: The IOrderedEnumerable and OrderBy clause here preserves the naming precedence in possible names
            return(possibleNames.SelectMany(f => f(propertyInfo)).OrderBy(x => true));

            IEnumerable <string> PrefixWithSchemaName(string name)
            {
                if (schemaNames == null)
                {
                    yield break;
                }

                foreach (var schemaName in schemaNames)
                {
                    yield return(Inflector.MakeInitialUpperCase(schemaName) + name);
                }
            }
        }
Exemplo n.º 2
0
        public Presenter(
            ILogSourcesManager logSourcesManager,
            IChangeNotification changeNotification,
            IView view,
            Preprocessing.IManager sourcesPreprocessingManager,
            Preprocessing.IStepsFactory preprocessingStepsFactory,
            MRU.IRecentlyUsedEntities mru,
            QuickSearchTextBox.IPresenter searchBoxPresenter,
            IAlertPopup alerts,
            ITraceSourceFactory traceSourceFactory
            )
        {
            this.view = view;
            this.changeNotification          = changeNotification;
            this.logSourcesManager           = logSourcesManager;
            this.sourcesPreprocessingManager = sourcesPreprocessingManager;
            this.preprocessingStepsFactory   = preprocessingStepsFactory;
            this.mru = mru;
            this.searchBoxPresenter = searchBoxPresenter;
            this.trace  = traceSourceFactory.CreateTraceSource("UI", "hist-dlg");
            this.alerts = alerts;

            items            = Selectors.Create(() => visible, () => acceptedFilter, MakeItems);
            actuallySelected = Selectors.Create(() => items().displayItems, () => selected,
                                                (items, selected) => items.SelectMany(i => i.Flatten()).Where(i => selected.Contains(i.key)).ToImmutableList());
            openButtonEnabled = Selectors.Create(actuallySelected, selected => selected.Any(IsOpenable));
            rootViewItem      = Selectors.Create(() => items().displayItems, () => selected, () => expanded, MakeRootItem);

            searchBoxPresenter.OnSearchNow += (s, e) =>
            {
                acceptedFilter = searchBoxPresenter.Text;
                FocusItemsListAndSelectFirstItem();
                changeNotification.Post();
            };
            searchBoxPresenter.OnRealtimeSearch += (s, e) =>
            {
                acceptedFilter = searchBoxPresenter.Text;
                changeNotification.Post();
            };
            searchBoxPresenter.OnCancelled += (s, e) =>
            {
                if (acceptedFilter != "")
                {
                    acceptedFilter = "";
                    searchBoxPresenter.Focus(null);
                }
                else
                {
                    visible = false;
                }
                changeNotification.Post();
            };

            view.SetViewModel(this);
        }
Exemplo n.º 3
0
        public override object CalculateResult()
        {
            var polygonalFuncs = new Func<int, int>[]
            {
                n => n * (n + 1) / 2,
                n => n * n,
                n => n * (3 * n - 1) / 2,
                n => n * (2 * n - 1),
                n => n * (5 * n - 3) / 2,
                n => n * (3 * n - 2),
            };

            var numbers = polygonalFuncs
                .SelectMany((f, i) => new Range(1, int.MaxValue)
                    .Select(n => f(n))
                    .SkipWhile(v => v < 1000)
                    .TakeWhile(v => v < 10000)
                    .Select(v => new
                    {
                        Part1 = v / 100,
                        Part2 = v % 100,
                        Value = v,
                        Type = i,
                    }))
                .ToArray();

            return new Range(5)
                .Aggregate(
                    numbers
                        .Where(n => n.Type == 0)
                        .Select(n => ImmutableSequence.Create(n)),
                    (acc, i) => acc
                        .SelectMany(x => numbers
                            .Where(n => n.Part1 == x.First().Part2)
                            .Where(n => x.All(v => n.Value != v.Value && n.Type != v.Type))
                            .Select(n => ImmutableSequence.Create(n, x))),
                    acc => acc
                        .Where(x => x.Last().Part1 == x.First().Part2))
                .Single()
                .Sum(x => x.Value);
        }
Exemplo n.º 4
0
        public void FuncTest()
        {
            bool            isExecuted1     = false;
            bool            isExecuted2     = false;
            bool            isExecuted3     = false;
            bool            isExecuted4     = false;
            Func <int>      f1              = () => { isExecuted1 = true; return(1); };
            Func <int>      f2              = () => { isExecuted2 = true; return(2); };
            Func <int, int> f3              = x => { isExecuted3 = true; return(x + 1); };
            Func <int, Func <int, int> > f4 = x => y => { isExecuted4 = true; return(x + y); };
            Func <int> query1 = from x in f1
                                from y in f2
                                from z in f3.Partial(y)
                                from _ in "abc".Func()
                                let f4x = f4(x)
                                          select f4x(z);

            Assert.IsFalse(isExecuted1);          // Deferred and lazy.
            Assert.IsFalse(isExecuted2);          // Deferred and lazy.
            Assert.IsFalse(isExecuted3);          // Deferred and lazy.
            Assert.IsFalse(isExecuted4);          // Deferred and lazy.
            Assert.AreEqual(1 + 2 + 1, query1()); // Execution.
            Assert.IsTrue(isExecuted1);
            Assert.IsTrue(isExecuted2);
            Assert.IsTrue(isExecuted3);
            Assert.IsTrue(isExecuted4);

            // Monad law 1: m.Monad().SelectMany(f) == f(m)
            Func <int, Func <int> > addOne = x => (x + 1).Func();
            Func <int> left  = 1.Func().SelectMany(addOne, False);
            Func <int> right = addOne(1);

            Assert.AreEqual(left(), right());
            // Monad law 2: M.SelectMany(Monad) == M
            Func <int> M = 1.Func();

            left  = M.SelectMany(FuncExtensions.Func, False);
            right = M;
            Assert.AreEqual(left(), right());
            // Monad law 3: M.SelectMany(f1).SelectMany(f2) == M.SelectMany(x => f1(x).SelectMany(f2))
            Func <int, Func <int> > addTwo = x => (x + 2).Func();

            left  = M.SelectMany(addOne, False).SelectMany(addTwo, False);
            right = M.SelectMany(x => addOne(x).SelectMany(addTwo, False), False);
            Assert.AreEqual(left(), right());

            bool               isExecuted5     = false;
            bool               isExecuted6     = false;
            bool               isExecuted7     = false;
            Func <int, int>    f5              = x => { isExecuted5 = true; return(x + 1); };
            Func <string, int> f6              = x => { isExecuted6 = true; return(x.Length); };
            Func <int, Func <int, string> > f7 = x => y =>
            { isExecuted7 = true; return(new string('a', x + y)); };
            Func <int, Func <string, string> > query2 = a => b =>
                                                        (from x in f5(a).Func()
                                                         from y in f6(b).Func()
                                                         from z in 0.Func()
                                                         select f7(x)(y))();

            Assert.IsFalse(isExecuted5);                                              // Deferred and lazy.
            Assert.IsFalse(isExecuted6);                                              // Deferred and lazy.
            Assert.IsFalse(isExecuted7);                                              // Deferred and lazy.
            Assert.AreEqual(new string('a', 1 + 1 + "abc".Length), query2(1)("abc")); // Execution.
            Assert.IsTrue(isExecuted5);
            Assert.IsTrue(isExecuted6);
            Assert.IsTrue(isExecuted7);
        }
Exemplo n.º 5
0
 public static Func <TState, UValue> SelectMany <TValue, UValue, TState>(
     this Func <TState, TValue> reader,
     Func <TValue, Func <TState, UValue> > binder)
 {
     return(reader.SelectMany(binder, IdValueSelector));
 }
Exemplo n.º 6
0
 public static Func <T, A> Join <T, A>(this Func <T, Func <T, A> > f)
 {
     return(f.SelectMany(x => x));
 }
Exemplo n.º 7
0
 // Not required, just for convenience.
 public static Func <TResult> SelectMany <TSource, TResult>
     (this Func <TSource> source, Func <TSource, Func <TResult> > selector) =>
 source.SelectMany(selector, Functions.False);