3
votes

I am having problems calling a function from within a sequence construct. Apparently, the function is being called so lazily that the overall code does not produce the intended result. Here is the code:

 mylist
 |> fun myseq -> 
    seq { for b in mylist do yield { b with status = (getStatus b)}}

Here, mylist is a list of records. I intent to build a sequence out of it with the field status updated from the function getStatus. It simply does not work, the function does not appear to run for each iteration of the for loop as expected.

Appreciate any help.

1

1 Answers

5
votes

Sequences are lazy. Your function will not run until the sequence is enumerated.

You need to turn the sequence into a concrete collection type like list or array (which will enumerate the sequence and force your function to run):

seq { for b in mylist do yield { b with status = getStatus b } }
|> Seq.toList

or, if you only care about side effects (which is not your case), use Seq.iter.