USING THE LAMBDA EXPRESSION IN TRANSACTIONS VECTORS IN C#

 

USING THE LAMBDA EXPRESSION IN TRANSACTIONS VECTORS IN C SHARP

By Mohamed Ali Ettougourti

 

Operations on vectors or arrays  occupy the largest share of time spent on the execution of computer programs.

In sharp c # and other languages the term « lambda » is used to « standardize » some vector operations.

We give in these article basic examples:

We start by creating a vector or an array of double values

double [] x = new double [100];

To create random values we initiated a generator of random values « t » that we will use to fill the array « x » of random values.

System.Random t = new Random ();

To do this we will use the lambda expression illustrated by « => »

x = x.Select(b => b = t.NextDouble()).ToArray();

This simple line of code allows us to avoid for next loop, or do while loop or any other loops.

former code example:

for (int i = 0; i <x.length; i ++)

{

x [i] = t.NextDouble ();

}

The lambda expression also can perform some basic operations on the vector.

Thus we are able to calculate the sum of the values contained in the vector:

double sum = x.Sum();

Or to calculate the arithmetic mean:

double average = x.Average();

The same lambda expression allows us to find the maximum value in the vector:

double max = x.Max();

Or the minimum value of vector:

double min = x.Min();

we may even turning the vector on « List » to search and find the position in the vector  of the both values « min » and « max » ;

int position = x.ToList () .IndexOf (min.);

position = x.ToList () .IndexOf (max.);

The term « lambda » also helps manage elegantly and mass conversion of the values contained in the vector from one type to another.

We can easily convert double values in the vector « x »  to values of type « float » or « int » or « short » ..

We can calculate the sum of the values contained in the vector as if they were of type int32 , operating conversion and performing the addition with the lambda expression

int  int_sum = x.Select (b => System.Convert.ToInt32 (b)) Sum ();

A line of code using the command « Cast » will not work generating aerror  of type « system invalid cast exception. »

int  int_sum = x.Cast () Sum ();

with the lambda expression more advanced arithmetic  operations can be done on the values of the vector.

One can for example raise the values of the vector to the second power, determining the amount, and then divide the number by  the vector length.

All these operations take place in a single line of code

double y= x.Select(b => b * b).Sum() / x.Length;

Other uses of the lambda expression make use of words such as « next » and « index ».

This is the case if one wants to operate an accumulation on a sequence of values in the vector by using the « aggregate » function.

double yy1 = x.Where(b=> b> 0.5).Aggregate((b, next) => next + b);

When you start the program you would notice that the variable « yy » is equal to the variable « sum » and we have done nothing more than to calculate the sum of values in a different way.

But the aggregate function can regain its usefulness when using it with another statement such as « where » for example.

It will be possible soon when we calculate the sum of certain values chosen according to a specific criterion.

Code Sample:

double yy1 = x.Where(b=> b> 0.5).Aggregate((b, next) => next + b);

Checking result you would notice that only values greater than 0.5 were taken into consideration in calculating the sum.

The word « next » is not a keyword. It can be replaced in the previous line of code with the word « index » without   any results changing.

double yy2 = x.Where(b => b > 0.5).Aggregate((b, index) => index + b);

Truly the word « next » or « index » can be just as easily replaced by any other word such as « suivant » or even « cat » 🙂  without any kind of compiler protestation .

And  here is the source code of the examples given

private void sample ()

{
double []x = new double [100];

System.Random t = new Random();
x = x.Select(b => b = t.NextDouble()).ToArray();
double sum = x.Sum();
double average = x.Average();
double max = x.Max();
double min = x.Min();
int position = x.ToList().IndexOf(min);
position = x.ToList().IndexOf(max);

//generating error invalid cast exception
//int int_sum= x.Cast<int>().Sum();

double y= x.Select(b => b * b).Sum() / x.Length;

double yy1 = x.Where(b=> b> 0.5).Aggregate((b, next) => next + b);
double yy2 = x.Where(b => b > 0.5).Aggregate((b, suivant ) => suivant + b);

double yy2 = x.Where(b => b > 0.5).Aggregate((b, cat) => cat + b);
}

(25)