Why is "->" deprecated in the understanding of F #?
Sometimes in books I see this syntax for understanding lists and sequences in F #:
seq { for i = 0 to System.Int32.MaxValue -> i } This is from F # Programming from Chris Smith , page 80. In F #, which comes with VS2010, this does not compile. I believe that -> out of date. (See Alternative Listing Syntax ). However, -> can still be used in an understanding that includes ranges:
seq { for c in 'A' .. 'Z' -> c } According to Expert F # 2.0 , page 58, this is because -> is an abbreviation for Seq.map in the range.
- Why is the first use
->above outdated? - Current usage
->seems inconsistent. Can anyone reconcile this for me?
The construction -> supported only in the syntax of the expression "simple" sequence in which you perform a projection ( Seq.map ) on top of some data source using the following structure:
seq { for <binding> in <input> -> <projection> } The first example you mentioned uses for .. to .. , which is a different syntax than for .. in , but you can rewrite it with the second (in fact, I almost always use for .. in when writing expressions sequence):
seq { for i in 0 .. System.Int32.MaxValue -> i } In all other sequence expressions, you must use yield . In earlier versions of F #, the syntax -> was equivalent to yield (and also was ->> , which was equivalent to yield! ). For example, you could write:
seq { -> 10 // You need 'yield' here ->> [ 1; 2; 3 ] } // You need 'yield!' here This syntax looks rather strange, so I think the main reason these two are deprecated is to keep the language consistent. The same calculation expression syntax is used in sequence expressions (where -> makes sense), but also for other types of calculations (and you can define your own), where yield feels more appropriate (and also matches return in asynchronous workflows or other calculation expressions).
The "simple" sequence-specific syntax is still useful because it saves you when typing (you replace do yield only -> ), but in more complex cases you don't save a lot of characters and me that syntax using -> and ->> may look a little cryptic.