Esempio n. 1
0
        public static IList <T> BubbleSort <T>(this IList <T> list, PredicateDelegate <T> predicate)
        {
            int i = list.Count - 1;

            while (i > 0)
            {
                int swap = 0;
                for (int j = 0; j < i; j++)
                {
                    if (predicate(list[j], list[j + 1]))
                    {
                        var temp = list[j];
                        list[j]     = list[j + 1];
                        list[j + 1] = temp;
                        swap        = j;
                    }
                }
                i = swap;
            }

            var newList = new List <T>();

            for (int index = 0; index < list.Count; index++)
            {
                newList.Add(list[index]);
            }
            return(newList);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            VoidDelegate <int>    var1 = Void2;
            VoidDelegate <double> var2 = Void3;
            VoidDelegate <string> var3 = Void4;

            VoidDelegate <int, double, string> var6 = Void6;
            VoidDelegate <string, double, int> var7 = Void7;
            VoidDelegate <int, int, int>       var8 = Void8;

            ReturnDelegate <int, int>       var9  = Return2;
            ReturnDelegate <double, string> var10 = Return3;
            ReturnDelegate <string, double> var11 = Return4;


            ReturnDelegate <int, double, string, char> var12 = Return6;
            ReturnDelegate <string, double, int, long> var13 = Return7;
            ReturnDelegate <int, int, int, byte>       var14 = Return8;

            PredicateDelegate <int> var15 = BoolMethod1;

            PredicateDelegate <string> var16 = BoolMethod2;
            PredicateDelegate <double> var17 = BoolMethod3;
            PredicateDelegate <byte>   var18 = BoolMethod4;
        }
Esempio n. 3
0
        public RelayCommand(Action execute, PredicateDelegate canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");

            _execute = execute;
            _canExecute = (object obj) => { return canExecute(); };
        }
Esempio n. 4
0
        public static void Start()
        {
            var nums = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            //Now that we're using a generic delegate type we'll have to specify that we're passing it an int...
            PredicateDelegate <int> predicateDelegate = Predicate;

            var result = Filter(nums, Predicate);
        }
Esempio n. 5
0
 public static List<int> FilterRecords(this List<int> OldList, PredicateDelegate FilterFunction)
 {
     var result = new List<int>();
     foreach (var item in OldList)
     {
         if (FilterFunction(item) == true) result.Add(item);
     }
     return result;
 }
Esempio n. 6
0
 public static void Example1()
 {
     var nums = new List<int> { 12, 10, 13, 09, 11, 19, 11, 08, 16, 10, 12, 10, 05, 09, 11, 14 };
     var delTen = new PredicateDelegate(PredicateMethods.IsGreaterThan10);
     foreach (var element in nums.FilterRecords(delTen))
     {
         Console.WriteLine(element.ToString());
     }
 }
Esempio n. 7
0
        static void Main(string[] args)
        {
            Console.WriteLine("*LastDigitComparer + BubbleSort: ");
            Console.WriteLine();

            var array = new List <int>()
            {
                2, 4, 1, 6, 10
            };
            var sortedArray = array.BubleSort(new LastDigitComparer());

            foreach (var item in sortedArray)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine();

            Console.WriteLine("BubbleSort with Delegate - Predicate for Compare Integers : ");
            Console.WriteLine();

            var secondArrey = new List <int>()
            {
                2, 4, 1, 6, 10
            };
            PredicateDelegate <int> myDelegate = CompareIntegers;
            var sortedArreyPredicate           = secondArrey.BubleSort(myDelegate);

            foreach (var item in sortedArreyPredicate)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine();

            Console.WriteLine("*ReverseComparer<T> + StringLengthComparer + SelectionSort: ");
            Console.WriteLine();

            var strings = new List <string>()
            {
                "grr", "brrr", null, "aaaaa", "yeaaaaa", "A"
            };
            var stringsArray = strings.SelectionSort(new ReverseComparer <string>(new StringLengthComparer()));

            strings.Add("o");
            foreach (var item in stringsArray)
            {
                if (item == null)
                {
                    Console.WriteLine("\"null\"");
                }
                else
                {
                    Console.WriteLine(item);
                }
            }
            Console.WriteLine();
        }
Esempio n. 8
0
        /// <summary>
        /// Using annonymous methods and Lambda expressions
        /// We are assigning Delegate instance delTen with a Lambda method
        /// </summary>
        public static void Example2()
        {
            var nums = new List<int> { 12, 10, 13, 09, 11, 19, 11, 08, 16, 10, 12, 10, 05, 09, 11, 14 };
            var delTen = new PredicateDelegate(x=>x>15? true:false);

            foreach (var element in nums.FilterRecords(delTen))
            {
                Console.WriteLine(element.ToString());
            }
        }
Esempio n. 9
0
 static IEnumerable Filter(IEnumerable src, PredicateDelegate pred)
 {
     foreach (object item in src)
     {
         if (pred(item))
         {
             yield return(item);
         }
     }
 }
Esempio n. 10
0
        internal Predicate(LibraryMethodList container, Functor functor, PredicateDelegate predicateDelegate, bool canEvaluate)
            : base(container, functor, canEvaluate)
        {
            if (predicateDelegate == null)
            {
                throw new ArgumentNullException("predicateDelegate");
            }

            m_predicateDelegate = predicateDelegate;
        }
Esempio n. 11
0
        internal Predicate(LibraryMethodList container, Functor functor, PredicateDelegate predicateDelegate, bool canEvaluate)
            : base(container, functor, canEvaluate)
        {
            if (predicateDelegate == null)
            {
                throw new ArgumentNullException("predicateDelegate");
            }

            m_predicateDelegate = predicateDelegate;
        }
Esempio n. 12
0
 public void Print(PredicateDelegate predicate)
 {
     foreach (var t in _table)
     {
         Console.ForegroundColor = predicate(t) ? ConsoleColor.Green : ConsoleColor.White;
         Console.Write(t + " ");
     }
     Console.WriteLine();
     Console.WriteLine();
 }
 static IEnumerable Filter(IEnumerable stds, PredicateDelegate pred)
 {
     foreach (object o in stds)
     {
         if (pred(o))
         {
             yield return(o);
         }
     }
 }
Esempio n. 14
0
 public IEnumerable <T> FindDelegate(PredicateDelegate <T> predicado)
 {
     foreach (var item in _items)
     {
         if (item != null && predicado(item))
         {
             yield return(item);
         }
     }
 }
Esempio n. 15
0
        public RelayCommand(Action execute, PredicateDelegate canExecute)
        {
            if (execute == null)
            {
                throw new ArgumentNullException("execute");
            }

            _execute    = execute;
            _canExecute = (object obj) => { return(canExecute()); };
        }
 // PredicateDelegate is passed IItemDataTyped objects
 public IAsyncOperation <IList <ItemKey> > SelectKeysAsync(PredicateDelegate predicate)
 {
     if (predicate == null)
     {
         throw new ArgumentNullException("predicate");
     }
     return(AsyncInfo.Run(async cancelToken =>
     {
         return await this.SelectKeysAsync(predicate, cancelToken);
     }));
 }
Esempio n. 17
0
        private void BuscarPares(Lista <int> lista)
        {
            //Delegado anónimo
            PredicateDelegate <int> paresDelegate = delegate(int i) { return(i % 2 == 0); };
            var listaPares = lista.FindDelegate(paresDelegate);

            Console.WriteLine("Pares:");
            foreach (var item in listaPares)
            {
                Console.WriteLine("- " + item.ToString());
            }
        }
Esempio n. 18
0
        //We're not really doing anything integer specific - just creating a list, looping through it, passing each int into our
        //generic predicate delegate, and adding and returning the results. We can remove the references to int and make this method
        //generic as well. Can't really call it filter integers any more, since we can filter way more than ints...
        public static IEnumerable <T1> Filter <T1>(IEnumerable <T1> list, PredicateDelegate <T1> predicateDelegate)
        {
            var result = new List <T1>();

            foreach (T1 i in list)
            {
                if (predicateDelegate(i))
                {
                    result.Add(i);
                }
            }
            return(result);
        }
Esempio n. 19
0
 // PredicateDelegate is passed IItemDataTyped objects
 public IAsyncOperation <IList <IItemDataTyped> > SelectAsync(PredicateDelegate predicate)
 {
     if (predicate == null)
     {
         throw new ArgumentNullException("predicate");
     }
     return(AsyncInfo.Run(async cancelToken => {
         using (await CrossThreadLockScope.Enter(m_lock))
         {
             return await m_items.SelectAsync(predicate, cancelToken);
         }
     }));
 }
Esempio n. 20
0
        public static IEnumerable <int> FilterIntegers(IEnumerable <int> list, PredicateDelegate predicateDelegate)
        {
            var result = new List <int>();

            foreach (int i in list)
            {
                if (predicateDelegate(i))
                {
                    result.Add(i);
                }
            }
            return(result);
        }
    static IEnumerable Filter(IEnumerable stds, PredicateDelegate pred)
    {
        IList res = new ArrayList();

        foreach (object o in stds)
        {
            if (pred(o))
            {
                res.Add(o);
            }
        }
        return(res);
    }
Esempio n. 22
0
        internal Predicate Add(Functor functor, PredicateDelegate predicateDelegate, bool canEvaluate)
        {
            if (functor == null)
            {
                throw new ArgumentNullException("functor");
            }
            if (predicateDelegate == null)
            {
                throw new ArgumentNullException("predicateDelegate");
            }
            var predicate = Methods.Add(functor, predicateDelegate, canEvaluate);

            return(predicate);
        }
Esempio n. 23
0
        static IEnumerable <int> FilterIntegers(IEnumerable <int> items
                                                , PredicateDelegate predicate)
        {
            var ret = new List <int>();

            foreach (var item in items)
            {
                if (predicate(item))
                {
                    ret.Add(item);
                }
            }
            return(ret);
        }
Esempio n. 24
0
    static IEnumerable Filter(IEnumerable src, PredicateDelegate pred)
    {
        IList res = new ArrayList();

        foreach (object item in src)
        {
            // if(pred.Invoke(item))
            if (pred(item))
            {
                res.Add(item);
            }
        }
        return(res);
    }
Esempio n. 25
0
        /// <summary>
        /// 让外界指定数据筛选的方法
        /// </summary>
        /// <param name="list"></param>
        /// <param name="predicate"></param>
        /// <returns></returns>
        static IEnumerable <int> FilterIntegers(IEnumerable <int> list,
                                                PredicateDelegate predicate)
        {
            //找出所有的偶数,放到List<int>集合中
            var result = new List <int>();

            foreach (var num in list)
            {
                if (predicate(num))
                {
                    result.Add(num);
                }
            }
            return(result);
        }
        //public static List<TOut> Select<TIn, TOut>(this TIn[] input, SelectDelegate<TIn, TOut> select) =>
        //    input.ToList().Select(select);

        public static List <T> Where <T>(this IEnumerable <T> enumerable, PredicateDelegate <T> predicate)
        {
            var list = new List <T>();

            foreach (var thing in enumerable)
            {
                var wrap = new ReferenceWrapper <bool>(false);
                predicate(thing, wrap);
                if (wrap)
                {
                    list.Add(thing);
                }
            }

            return(list);
        }
        internal async Task <IList <ItemKey> > SelectKeysAsync(PredicateDelegate predicate, CancellationToken cancelToken)
        {
            if (predicate == null)
            {
                throw new ArgumentNullException("predicate");
            }
            List <ItemKey> matches = new List <ItemKey>();

            for (int i = 0, max = this.KeyCount; i < max; ++i)
            {
                IItemDataTyped item = await this.GetItemAsync(i, true, cancelToken);

                if (item != null && predicate(item))
                {
                    matches.Add(item.Key);
                }
            }

            return(matches);
        }
Esempio n. 28
0
        static void Main(string[] args)
        {
            PredicateDelegate.IfValid();

            ActionDelegate.Save();

            FuncDelegate.MainFunc();

            DelegatesFunc();

            FuncActionPred.PredicateDemo();
            FuncActionPred.ActionDemo();
            FuncActionPred.FuncDemo();

            //multi cast delegates
            MultiCastDelegates.DelFunc();
            //using lambda
            DelegateEx1.UsingLambda();
            //without lambda
            DelegateEx1.AddData();
        }
        public static bool Any <T>(this List <T> enumerable, PredicateDelegate <T> predicate = null)
        {
            if (predicate == null)
            {
                return(enumerable.Count > 0);
            }
            Kernel.PrintDebug("Hi from any");
            var wrap = new ReferenceWrapper <bool>(false);

            Kernel.PrintDebug("ref yes");
            foreach (var thing in enumerable)
            {
                Kernel.PrintDebug("foreach ok");
                predicate(thing, wrap);
                Kernel.PrintDebug("predicate ok");
                if (wrap)
                {
                    return(true);
                }
            }
            return(false);
        }
Esempio n. 30
0
        internal Predicate Add(Functor functor, PredicateDelegate predicateDelegate, bool canEvaluate)
        {
            if (functor == null)
            {
                throw new ArgumentNullException("functor");
            }
            if (predicateDelegate == null)
            {
                throw new ArgumentNullException("predicateDelegate");
            }
            if (Contains(functor))
            {
                throw new ArgumentException("Item already exists.", "functor");
            }

            var predicate = new Predicate(this, functor, predicateDelegate, canEvaluate);

            Items.Add(predicate);
            Library.Touch();

            return(predicate);
        }
 // PredicateDelegate is passed IItemDataTyped objects
 public IAsyncOperation<IList<ItemKey>> SelectKeysAsync(PredicateDelegate predicate)
 {
     if (predicate == null)
     {
         throw new ArgumentNullException("predicate");
     }
     return AsyncInfo.Run<IList<ItemKey>>(async cancelToken =>
     {
         using (await CrossThreadLockScope.Enter(m_lock))
         {
             return await m_items.SelectKeysAsync(predicate, cancelToken);
         }
     });
 }
        internal async Task<IList<ItemKey>> SelectKeysAsync(PredicateDelegate predicate, CancellationToken cancelToken)
        {
            if (predicate == null)
            {
                throw new ArgumentNullException("predicate");
            }
            List<ItemKey> matches = new List<ItemKey>();
            for (int i = 0, max = this.KeyCount; i < max; ++i)
            {
                IItemDataTyped item = await this.GetItemAsync(i, true, cancelToken);
                if (item != null && predicate(item))
                {
                    matches.Add(item.Key);
                }
            }

            return matches;
        }
Esempio n. 33
0
 public MarkableInvocationGraph(PredicateDelegate predicate)
 {
     this.predicate = predicate;
 }
Esempio n. 34
0
        internal Predicate Add(Functor functor, PredicateDelegate predicateDelegate, bool canEvaluate)
        {
            if (functor == null)
            {
                throw new ArgumentNullException("functor");
            }
            if (predicateDelegate == null)
            {
                throw new ArgumentNullException("predicateDelegate");
            }

            Predicate predicate = Methods.Add(functor, predicateDelegate, canEvaluate);

            return predicate;
        }
Esempio n. 35
0
 internal void Add(string name, string op, int arity, PredicateDelegate predicateDelegate, bool canEvaluate)
 {
     Add(new Functor(name, arity), predicateDelegate, canEvaluate);
     Add(new Functor(op, arity), predicateDelegate, canEvaluate);
 }
Esempio n. 36
0
 public FilterEnumerator(IEnumerator enumerator, PredicateDelegate pred)
 {
     this.src  = enumerator;
     this.pred = pred;
 }
Esempio n. 37
0
 public FilterEnumerable(IEnumerable src, PredicateDelegate pred)
 {
     this.src  = src;
     this.pred = pred;
 }
Esempio n. 38
0
 public DelegateCondition(PredicateDelegate predicate)
 {
     Predicate = predicate;
 }
 // PredicateDelegate is passed IItemDataTyped objects
 public IAsyncOperation<IList<ItemKey>> SelectKeysAsync(PredicateDelegate predicate)
 {
     if (predicate == null)
     {
         throw new ArgumentNullException("predicate");
     }
     return AsyncInfo.Run(async cancelToken =>
     {
         return await this.SelectKeysAsync(predicate, cancelToken);
     });
 }
Esempio n. 40
0
 internal void Add(string name, string op, int arity, PredicateDelegate predicateDelegate, bool canEvaluate)
 {
     Add(new Functor(name, arity), predicateDelegate, canEvaluate);
     Add(new Functor(op, arity), predicateDelegate, canEvaluate);
 }
Esempio n. 41
0
 public DelegateCondition(PredicateDelegate predicate)
 {
     Predicate = predicate;
 }