Exemplo n.º 1
0
        }                             //to the delegation method, it essentially takes the '1', passes to method 'IntOps',

        //then passes it to 'Add10' to be added by 10, ie 'x + 10', and finally send back as
        //the new variable for arr[0]. Effectively replacing 1 with a new value of 11.


        static void Main(string[] args)   //Main method
        {
            //Create an array
            int[] A = new int[] { 1, 2, 3 };

            //MUST UNDS HOW THEY create a variable and ASSIGNING IT TO THE DELEGATION METHOD!!!!!!!!!
            IntOps myOp = Add10;    //Just use it as tho it is a data type.

            //'Add10' here is another method. So they create a variable called..
            //'myOp' and call method 'Add10' to assign a math function, but it still has no value.

            //Console.WriteLine(myOp);      //try this, you will get nothing cos its still empty.
            PrintArray(A);
            ApplyOperation(A, myOp);    //Only here then you add a value to the math operation that
            PrintArray(A);              //'Add10' has assigned to 'myOp'. This is also where the
            //method-ceptions occurs. Cos here Main() method will call upon 'ApplyOperation()'
            //method to perform its calculation. But in addition to that, 'ApplyOperation()' calls on
            //to the IntOps method which then calls on to the 'Add10' method. Totally sweet amirite??? :D



            //Same thing, just with different mathematical functions
            myOp = Minus1;
            PrintArray(A);
            ApplyOperation(A, myOp);
            PrintArray(A);


            myOp = Multiply2;
            PrintArray(A);
            ApplyOperation(A, myOp);
            PrintArray(A);
        }
Exemplo n.º 2
0
 static void ApplyOperation(int[] arr, IntOps ops)
 {
     for (int i = 0; i < arr.Length; i++)
     {
         arr[i] = ops(arr[i]);
     }
 }
Exemplo n.º 3
0
        delegate int IntOps(int n);            //Creation of delegation

        static void ApplyOperation(int[] arr, IntOps ops)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = ops(arr[i]); //This line here takes a value from array 'arr', eg: 1,
            }  //and input it into ops. But because ops is a variable that has been defined/linked
        }                             //to the delegation method, it essentially takes the '1', passes to method 'IntOps',
        static void Main()
        {
            int[] x = new int[] { 1, 2, 3, 4, 5 };
            PrintArray(x);
            IntOps delegateOps = Add10;

            x[0] = delegateOps(x[0]);
            PrintArray(x);
            ApplyOperation(x, delegateOps);
            PrintArray(x);
        }
Exemplo n.º 5
0
        delegate int IntOps(int b); // Operation that takes an integer and outputs an integer

        private static int[] ApplyOperation(int[] A, IntOps ops)
        {
            int[] newArray = new int[A.Length];

            for (int i = 0; i < A.Length; i++)
            {
                newArray[i] = ops(A[i]); // Applying ops to each element
            }

            return(newArray);
        }
Exemplo n.º 6
0
        static void Main()
        {
            int[]    A          = new int[] { 1, 2, 3 };
            IntOps[] operations = new IntOps[] { Add10, Minus1, Multiply2 };

            for (int i = 0; i < operations.Length; i++)
            {
                IntOps myOp = operations[i];
                PrintArray(A);
                ApplyOperation(A, myOp);
                PrintArray(A);
            }
        }
Exemplo n.º 7
0
 public static object TrueDivide(object a, object b)
 {
     if (a is int)
     {
         return(IntOps.TrueDivide((int)a, b));
     }
     else if (a is long)
     {
         return(Int64Ops.TrueDivide((long)a, b));
     }
     else if (a is IronMath.BigInteger)
     {
         return(LongOps.TrueDivide((IronMath.BigInteger)a, b));
     }
     return(Ops.Divide(a, b));
 }
Exemplo n.º 8
0
        public static object Repeat(object a, object b)
        {
            System.Collections.IEnumerator enumerator;
            object count;

            try {
                enumerator = Ops.GetEnumerator(a);
            } catch {
                throw Ops.TypeError("object can't be repeated");
            }
            try {
                count = IntOps.Make(b);
            } catch {
                throw Ops.TypeError("integer required");
            }
            return(Ops.Multiply(a, b));
        }
Exemplo n.º 9
0
        public static object Abs(object o)
        {
            if (o is int)
            {
                return(IntOps.Abs((int)o));
            }
            if (o is long)
            {
                return(Int64Ops.Abs((long)o));
            }
            if (o is double)
            {
                return(FloatOps.Abs((double)o));
            }
            if (o is bool)
            {
                return(((bool)o) ? 1 : 0);
            }
            if (o is string)
            {
                throw Ops.TypeError("bad operand type for abs()");
            }
            BigInteger bi = o as BigInteger;

            if (!Object.Equals(bi, null))
            {
                return(LongOps.Abs(bi));
            }
            if (o is Complex64)
            {
                return(ComplexOps.Abs((Complex64)o));
            }

            object ret;

            if (Ops.TryToInvoke(o, SymbolTable.AbsoluteValue, out ret))
            {
                return(ret);
            }
            else
            {
                throw Ops.TypeError("bad operand type for abs()");
            }
        }
Exemplo n.º 10
0
        public static void Program1()
        {
            int[] A = new[] { 1, 2, 3 };

            int[] B = SquareArray(A);
            PrintArray(B); // { 1, 4, 9 }

            int[] C = IncrementArray(A);
            PrintArray(C);                                          // { 2, 3, 4 }

            int[] D = ApplyOperation(A, Square);                    // To apply Square operations to elements in array
            PrintArray(D);                                          // { 1, 4, 9 }
            int[] E = ApplyOperation(A, TimesTen);                  // To apply TimesTen operation to elements in array
            PrintArray(E);                                          // { 10, 20, 30 }

            IntOps times100 = delegate(int x) { return(x * 100); }; // Anonymous Methods = do not belong to a class

            int[] F = ApplyOperation(A, times100);
            PrintArray(F); // { 100, 200, 300 }

            // You can define anonymous methods long as it complies with delegate input and output
            IntOps times100b = delegate(int x) { return(TimesN(x, 100)); };

            int[] G = ApplyOperation(A, times100b);
            PrintArray(G); // { 100, 200, 300 }

            // Pass method directly into method that takes in a delegate;
            int[] H = ApplyOperation(A, delegate(int x) { return(x * 100); });
            PrintArray(H); // { 100, 200, 300 }

            // Lambda expressions - writing anonymous functions
            int[] I = ApplyOperation(A, (int x) => { return(x * 100); });
            PrintArray(I); // { 100, 200, 300 }

            int[]      J = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
            List <int> K = new List <int>(
                from x in J
                where x % 2 == 0
                select x);

            PrintArray(K); // { 2, 4, 6, 8 }
        }
Exemplo n.º 11
0
        static void Main()
        {
            int[] A = new int[] { 1, 2, 3 };

            IntOps myOp = Add10;

            PrintArray(A);
            ApplyOperation(A, myOp);
            PrintArray(A);

            myOp = Minus1;
            PrintArray(A);
            ApplyOperation(A, myOp);
            PrintArray(A);

            myOp = Multiply2;
            PrintArray(A);
            ApplyOperation(A, myOp);
            PrintArray(A);
        }
Exemplo n.º 12
0
        //This is where the difference lies compared to the previous one
        static void Main()
        {
            int[] A = new int[] { 1, 2, 3 };

            //Below lies the code to assign the delegation method!!!
            IntOps[] operations = new IntOps[] { Add10, Minus1, Multiply2 };
            //Here, we essentially link three different methods within the 'operations' array.
            //Basically instead of actual values, we put methods.
            //So if the particulat position is called, the respective method will be activated.


            for (int i = 0; i < operations.Length; i++)
            {
                //Here we create another variable using the delegation method
                IntOps myOp = operations[i];
                PrintArray(A);
                ApplyOperation(A, myOp);
                PrintArray(A);
            }
        }//Here's the logic. So when i=0, it refers to the 0 position within the operations