sábado, 5 de marzo de 2016

LINQ Recipe No. 1-3: How to Use the Various Types of Functions

Contents

1. Introduction
2. Keywords
3. Problem
4. Solution
5. Discussion
5.1 Generator functions
5.2 Statistical functions
5.3 Projector functions
5.4 Filter functions
6. Practice: Types of Functions - Examples
7. Conclusions
8. Literature & Links

1. Introduction

In this new LINQ recipe many new concepts about the different types of functions will be presented. We will study and practice how to use the kinds functions like: generator, statistical, projector, and filter. As we will see, these kinds of functions will allow us to manipulate data in more broadly way than the basic ones (those presented in recipes 1-1 and 1-2). In the practice section a series of examples will be explained to reinforce the comprehension for further recipes of this interesting topic: functional programming with LINQ in Microsoft .NET applications.

2. Keywords

  • .NET
  • Data
  • Generator
  • Filter
  • Function
  • LINQ
  • Microsoft
  • Projector

3. Problem

The programmer needs to learn which kinds of functions exist in functional programming and how they work to manipulate data in a more sophisticated way.

4. Solution

In functional programming there exist up to four categories for functions:
  • Generators
  • Statistical
  • Projectors
  • Filters

5. Discussion

The .NET Framework offers to the programmer, in the functional programming context, four types of functions.
Types of Functions
illustration 1. Types of Functions (Mukherjee, 2014).

5.1 Generator functions

This kind of function lets the programmer to generate values out of nothing (Mukherjee, 2014). They can be represented using this equation:

() => T[]

Where T represents the output data type for the function. In the general sense, this type of function could be viewed like a method without parameters. The .NET Framework comes with many built-in generator functions; among them we have:
  • Enumerable.Range(): generates a sequence of integral numbers between an specified range. ("Enumerable.Range Method", 2016).

5.2 Statistical functions

It's possible to generate statistics from a collection of elements. This is done by using the built-in statistical functions
  • Any(): this function determines whether one or more elements satisfy some condition. Its return data type is bool. ("Enumerable.Any(TSource) Method", 2016)
  • Count():  this function function returns the number of elements in a collection. ("Enumerable.Count(TSource)", 2016)
  • Single(): this returns a single (one) element from a collection. ("Enumerable.Single (Method)", 2016)
  • Sum(): calculates the addition of a collection of numerical elements. ("Enumerable.Sum (Method)", 2016)
Many of the above methods has multiple overloaded versions suited for different requirements.

On the other hand, the general syntax is

T[] => Boolean
T[] => Number

T[] corresponds to a collection of elements (more commonly integer values) and the return type is an primitive type like Boolean or Number.

5.3 Projector functions

The projector functions is suitable to process -project- a collection elements and return a collection of elements; i.e.:

T[] => U[]

For example, if we have a collection with city names and if we want to create -project- a collection with number of letters for each city name, then we must use a projector function to perform this process. The general equation for this example is 

String[] => Integer[]

Some of the available functions are: 
  • Select(): projects each collection element from one representation to a new one. ("Enumerable.Select(TSource, TResult)", 2016)
  • SelectMany(): project each collection element to an IEnumerable<T> data type. 

5.4 Filter functions

With these kind of functions the programmer can filter out the elements of a specified collection. Examples of these functions can be: 
  • First()
  • Last()
  • Where()
According to Mukjerhee (2014) a filter function can be represented using either of these two equations:
  • T[] => U[]: the filter function returns a list of elements (a collection).
  • T[] => T: this function returns a single element according to condition or predicate.

6. Practice: Types of functions - Examples

Now we can proceed to elaborate some code example to reinforce our comprehension about the different kinds of functions.


In line 14 we generate five integers -from 1 to 5-. With the line 24 we enlist 5 Microsoft products, then with the functional method Count we count the number of elements in the sequence: 5.


Now, we proceed with the use of projector functions. In particular, in the line 31, numbers from 1 to 5 are cubed. Finally with the code in the line 41 we select the first element from the string array msProducts.

Code compilation:

csc /t:exe TypeFunctionsExamples.cs

Assembly execution:

.\TypeFunctionsExamples.exe

Online assembly execution (ideone.com): http://ideone.com/RJpfw5

Local assembly execution:
Assembly TypeFunctionsExamples.exe execution
Illustration 2. Assembly TypeFunctionsExamples.exe execution.

7. Conclusions

We have learned to distinguish the different kinds of functions: generators, statistical, projector, and filters. As we have seen all of these functions can be glued together to manipulate or transform data from different sources. In the example code presented, we have got a first sight to the functional programming methods available in the .NET Framework; in future recipes or articles we will go deeper.

In the next recipe we are going to understand the benefits of functional programming.

8. Literature & Links

Mukherjee, S (2014). Thinking in LINQ Harnessing the Power of Functional Programming in .NET Applications. United States: Apress.
Enumerable.Range Method (System.Linq) (2016, March 4). Retrieved from: https://msdn.microsoft.com/en-us/library/system.linq.enumerable.range(v=vs.100).aspx
Enumerable.Any(TSource) (Method) (IEnumerable(TSource), Func(TSource, Boolean)) (System.Linq) (2016, March 5). Retrieved from: https://msdn.microsoft.com/library/bb534972(v=vs.100).aspx
Enumerable.Count(TSource) (Method) (IEnumerable(TSource)) (System.Linq) (2016, March 5). Retrieved from: https://msdn.microsoft.com/library/bb338038(v=vs.100).aspx
Enumerable.Single (Method) (System.Linq) (2016, March 5). Retrieved from: https://msdn.microsoft.com/es-es/library/system.linq.enumerable.single(v=vs.110).aspx
Enumerable.Sum Method (System.Linq) (2016, March 5). Retrieved from: https://msdn.microsoft.com/en-us/library/system.linq.enumerable.sum(v=vs.110).aspx
Enumerable.Select(TSource, TResult) Method (IEnumerable(TSource), Func(TSource, TResult)) (System.Linq) (2016, March 5). Retrieved from: https://msdn.microsoft.com/library/bb548891(v=vs.100).aspx
Enumerable.SelectMany(TSource, TResult) Method (IEnumerable(TSource), Func(TSource, IEnumerable(TResult))) (System.Linq) (2016, March 5). Retrieved from: https://msdn.microsoft.com/en-us/library/bb534336(v=vs.110).aspx


V

No hay comentarios:

Publicar un comentario

Envíe sus comentarios, dudas, sugerencias, críticas. Gracias.