What is the difference between these two F # sequences?

> seq { for i in 0..3 do yield float i };;
val it : seq<float> = seq [0.0; 1.0; 2.0; 3.0]
> seq [ for i in 0..3 do yield float i ];;
val it : seq<float> = [0.0; 1.0; 2.0; 3.0]

PS why did F # initially expect a sequence without the seq prefix, but now they want a prefix?

+3
source share
2 answers

Detailed explanation of the two forms you posted:

The first uses a direct understanding of the sequence "seq {...}". The seq part was previously optional, but will not be supported in the future. I guess this does things like "async {...}", and the workflow syntax is more complicated or something like that.

Now the second is a clean list:

> let x = [ for i in 0..3 do yield float i ];;

val x : float list

"seq" is also a function:

> seq;;
val it : (seq<'a> -> seq<'a>) = <fun:clo@0_1>

, "seq [1; 2; 3;]" seq . , seq .

> x;;
val it : float list = [0.0; 1.0; 2.0; 3.0]

> seq x;;
val it : seq<float> = [0.0; 1.0; 2.0; 3.0]

: seq :

let seq (x : seq<_>) = (x :> seq<_>)

, "sorta like casting it" " ". , , .

+5

- seq (IEnumerable).

; "seq" ​​ (). ; seq {} [] [| |], seq .

, -by-seq, - , , .

+3

Source: https://habr.com/ru/post/1704307/


All Articles