using arrays in a functional style in C #

Source: Internet
Author: User using arrays in a functional style in C #

With LINQ to Object, in C # 3.0, we have been able to work with arrays using a number of function structures. Most LINQ operators do not return an array: If Enumerable.select is called on the array, the result will return ienumerable<t>. In some cases, we prefer to save the result in an array, avoiding the overhead of calling Enumerable.toarray and copying the result sequence back to the array.

Some commonly used functional operations on arrays have become static methods in the System.Array class, but their naming conventions are different from F # and LINQ, for example, you will find mapping operations under ConvertAll name. We will use the standard name to implement our version of the operation, to demonstrate. A method similar to F # 's Array.int function is added in Listing 10.14.

Listing 10.14 methods for working with arrays in function (C #)

Static Class Arrayutils {

Publicstatic t[] int<t> (int length, func<int, t> init) {[1]

t[] arr = new T[length];

for (int i = 0; i < length; i++) arr[i] = init (i);

return arr;


Publicstatic r[] select<t, r> (this t[] arr, func<t, r> map) {[2]

r[] res = new R[arr. Length];

for (int i = 0; i < arr. Length; i++) Res[i] = map (arr[i]);

return res;



The int method is the usual static method [1], whose arguments are function init, which is used to initialize the elements in the array. The Select method is an extension method that applies the mapping function to each element in the original array, returning a new array, which hides the standard Select operation provided by LINQ. We'll use these methods in a way similar to the previous F # functions:

var rnd = new Random ();

var numbers = Arrayutils.int (5, N =>rnd. Next (20));

var squares = numbers. Select (n = = new {Number = N, Square = n*n});

foreach (var sq in squares)

Console.Write ("({0}, {1})", Sq. Number, Sq. Square);

As in the F # version, once an array is created, it cannot be modified. From a higher level, it is the pure function code that handles immutable data structures; Of course, we can actually make modifications, but only in the Arrayutils class, C, and only on collections that are not exposed to any other code. Modifications are not visible to the outside; in C #, writing code in this way is more valuable because it is more difficult to use a functional list here than in F #.

Our final topic in this chapter is about continuous (continuations). It may be a bit difficult to understand, but once you understand it, you might be surprised. The good news is that if you ever write any asynchronous code in. NET, in a sense, you've used continuous, and F # makes it easier. We will discuss in more detail in the 13th chapter, using continuous, for recursive functions is a very important optimization method, so we will focus here. using arrays in a functional style in C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.