Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why was "->" deprecated in F# comprehensions?

Tags:

f#

Sometimes in books I see this syntax for list and sequence comprehensions in F#:

seq { for i = 0 to System.Int32.MaxValue -> i }

This is from Programming F# by Chris Smith, page 80. In the F# which comes with VS2010, this doesn't compile. I believe -> has been deprecated. (See Alternative List Comprehension Syntax). However, -> can still be used in comprehensions which involve ranges:

seq { for c in 'A' .. 'Z' -> c }

According to Expert F# 2.0, page 58, this is because -> is shorthand for Seq.map over a range.

  1. Why was the first usage of -> above deprecated?
  2. The current use of -> seems inconsistent. Can anyone reconcile this for me?
like image 912
royco Avatar asked Nov 06 '10 05:11

royco


People also ask

Why is Strftime deprecated?

PHP 8.1: strftime() is deprecated Though, the solutions may be related/shared. The issue with the output modifiers is it needs to have a solution for locale-aware output. Most of the other uses in the core did not need this and will be converted to locale-independent formatting via date() or DateTime*::format().

What does deprecated mean in PHP?

As a constantly developing language, newer PHP versions include functions that become deprecated. These functions cease to exist or change the expected result of the function as further versions of PHP are released.


1 Answers

The -> construct is supported only in the "simple" sequence expression syntax where you're doing a projection (Seq.map) over some data source using the following structure:

seq { for <binding> in <input> -> <projection> }

The first example you mentioned is using for .. to .. which is a different syntactical construct than for .. in, but you can rewrite it using the second one (In fact, I almost always use for .. in when writing sequence expressions):

seq { for i in 0 .. System.Int32.MaxValue -> i }

In all other forms of sequence expressions you'll have to use yield. In earlier versions of F#, the -> syntax was equivalent to yield (and there was also ->> which was equivalent to yield!). So for example, you was able to write:

seq { -> 10              // You need 'yield' here
      ->> [ 1; 2; 3 ] }  // You need 'yield!' here

This syntax looks quite odd, so I think that the main reason for making these two deprecated is to keep the language consistent. The same computation expression syntax is used in sequence expressions (where -> makes some sense), but also for other computation types (and you can define your own), where yield feels more appropriate (and it also corresponds to return in asynchronous workflows or other computation expressions).

The "simple" sequence-specific syntax is still useful, because it saves you some typing (you replace do yield with just ->), but in more complicated cases, you don't save that many characters and I think that the syntax using -> & ->> can look a bit cryptic.

like image 116
Tomas Petricek Avatar answered Nov 13 '22 16:11

Tomas Petricek