![]() ![]() You can create an empty sequence by using Seq.empty, or you can create a sequence of just one specified element by using Seq.singleton. You can create sequences by using sequence expressions, as described previously, or by using certain functions. These functions work with lists, arrays, maps, and sets as well, because all of those types are enumerable, and therefore can be treated as sequences. The Seq module in the FSharp.Collections namespace contains functions for working with sequences. NET collection types, is compatible with the seq type and can be used wherever a sequence is expected. This means that any type that implements the generic, which includes arrays, lists, sets, and maps in F#, and also most. The type seq is a type abbreviation for IEnumerable. Contrast this to a function that takes a list as an argument, which can only take lists. A function that takes a sequence as an argument works with any of the common F# data types, in addition to any. Many data types, such as lists, arrays, sets, and maps are implicitly sequences because they are enumerable collections. ![]() Sequences also support more diverse functions for extracting subsequences. Sequences also support operations such as grouping and counting by using key-generating functions. Sequences support many of the same functions as lists. Yield the values of a binary tree in a sequence. In this case, the sequences for each subtree in a binary tree are concatenated in a recursive function to produce the final sequence. The following example demonstrates the use of yield! to combine individual sequences into a single final sequence. The following example creates a multiplication table that consists of tuples of three elements, each consisting of two factors and the product: let multiplicationTable = This code prints a sequence of prime numbers between 1 and 100 to the console. The first example uses a sequence expression that contains an iteration, a filter, and a yield to generate an array. The previous example will produce the value of x in addition to all values from 1 to x for each x. ![]() When yield! is used in an expression, all other single values must use the yield keyword: // Combine repeated values with their values To include a sequence within another sequence, you'll need to use the yield! keyword: // Repeats '1 2 3 4 5' ten timesĪnother way of thinking of yield! is that it flattens an inner sequence and then includes that in the containing sequence. ![]() Sometimes, you may wish to include a sequence of elements into another sequence. If you try to use ->, you'll get an error saying that not all branches return a value. For example, seq Īs mentioned previously, do is required here because there is no else branch that goes with the if. Sequence expressions can take a number of forms. Sequence ExpressionsĪ sequence expression is an expression that evaluates to a sequence. The Seq module provides support for manipulations involving sequences. NET type that implements IEnumerable interface can be used as a sequence. Sequences are represented by the seq type, which is an alias for IEnumerable. Individual sequence elements are computed only as required, so a sequence can provide better performance than a list in situations in which not all the elements are used. Sequences are particularly useful when you have a large, ordered collection of data but do not necessarily expect to use all of the elements. A sequence is a logical series of elements all of one type. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |