12.3.2.1 using higher-order functions
In F #, using the Seq module is the same as list, and we already know how to use the LINQ extension method in C #. There is a significant difference in the use of lists and sequences: the sequence is deferred. The processing code is not executed until the element is fetched from the return sequence, although it does so only to return the result. Now, let's use a simple code snippet to illustrate:
var nums1 = Nums. Where (n = n%3 = = 0) . Select (n = n * N) |
Let nums1 = Nums |> Seq.filter (fun N-n%3=0) |> Seq.map (fun n, n * N) |
When we run this code, it does not process any elements; it only creates a sequence object that can be used to access the element; This also shows that the Nums value can be an infinite number sequence. If we only access the first 10 elements of the sequence, the code will work properly because both filtering and mapping are deferred processing data.
Using higher-order processing functions, you may already be familiar with the whole book, after we have discussed it in depth in the sixth chapter, we have provided many examples. In this chapter, we will turn to the methods that express the optional workflow.
12.3.2.1 using higher-order functions