Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F#: how to evaluate a "seq" to get all its values eagerly?

We know that in F#, seq is lazy evaluated. My question is, if I have a seq with limited number of values, how to convert it into some data type that contains all its value evaluated?

> seq { for i in 1 .. 10 do yield i * i };;
val it : seq<int> = seq [1; 4; 9; 16; ...]

Thanks a lot.

like image 350
vik santata Avatar asked Feb 03 '16 05:02

vik santata


People also ask

What does ⟨F⟩ mean?

This sound is usually considered to be an allophone of /h/, which is pronounced in different ways depending upon its context; Japanese /h/ is pronounced as [ɸ] before /u/. In Welsh orthography, ⟨f⟩ represents /v/ while ⟨ff⟩ represents /f/. In Slavic languages, ⟨f⟩ is used primarily in words of foreign (Greek, Latin, or Germanic) origin.

What does the letter F mean in math?

In countries such as the United States, the letter "F" is defined as a failure in terms of academic evaluation. Other countries that use this system include Saudi Arabia, Venezuela, and the Netherlands. In the hexadecimal number system, the letter "F" or "f" is used to represent the hexadecimal digit fifteen (equivalent to 15 10 ).

What does F stand for in the Etruscan alphabet?

In the Etruscan alphabet, 'F' probably represented /w/, as in Greek, and the Etruscans formed the digraph 'FH' to represent /f/.

Is the letter F doubled at the end of words?

It is often doubled at the end of words. Exceptionally, it represents the voiced labiodental fricative / v / in the common word "of". F is the twelfth least frequently used letter in the English language (after C, G, Y, P, B, V, K, J, X, Q, and Z ), with a frequency of about 2.23% in words.


2 Answers

The answer from @Carsten is correct: you can use Seq.toArray or Seq.toList if you wish to convert lazily evaluated sequences to lists or arrays. Don't use these function to force evaluation, though.

The most common reason people tend to ask about this is because they have a projection that involves side effects, and they want to force evaluation. Take this example, where one wishes to print the values to the console:

let lazySeq = seq { for i in 1 .. 10 do yield i * i }
let nothingHappens = lazySeq |> Seq.map (printfn "%i")

The problem is that when you evaluate these two expressions, nothing happens:

> 

val lazySeq : seq<int>
val nothingHappens : seq<unit>

Because nothingHappens is a lazily evaluated sequence, no side-effects occur from the map.

People often resort to Seq.toList or Seq.toArray in order to force evaluation:

> nothingHappens |> Seq.toList;;
1
4
9
16
25
36
49
64
81
100
val it : unit list =
  [null; null; null; null; null; null; null; null; null; null]

While this works, it's not particularly idiomatic; it produces a weird return type: unit list.

A more idiomatic solution is to use Seq.iter:

> lazySeq |> Seq.iter (printfn "%i");;
1
4
9
16
25
36
49
64
81
100
val it : unit = ()

As you can see, this forces evaluation, but has the more sane return type unit.

like image 76
Mark Seemann Avatar answered Oct 22 '22 14:10

Mark Seemann


use Seq.toArray (for an array) or Seq.toList (for an list) ;)

there are plenty more - just choose ;)


example:

> seq { for i in 1 .. 10 do yield i * i } |> Seq.toArray;;
val it : int [] = [|1; 4; 9; 16; 25; 36; 49; 64; 81; 100|]
like image 45
Random Dev Avatar answered Oct 22 '22 15:10

Random Dev