F# – yield vs yield!

C# developers should be familiar with the yield keyword that was introduced in C# 2.0, and you’ll be pleased to know that F# also has the yield keyword which works in conjunction with F#’s equivalent of IEnumerablesequences.

You can create sequences in F# using sequence expressions:

image

In addition, you can also use the yield! (pronounced yield bang) keyword to return a subsequence which is merged into the final sequence. Using a rather contrived example (sorry…) let’s compare the output of yield and yield!:

image

Look at how the signatures differ for these two sequences:

image

and how the values in the two sequences differ too:

image

The yield! keyword is similar to IEnumerable.SelectMany in its ability to flatten a collection of collections but it’s far more powerful as you can mix and match it with other yield/yield! statements and give you a level of expressiveness that IEnumerable.SelectMany does not.

Subscribe to my newsletter and get new contents delivered straight to your inbox :-)