Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is no simple way to do this. You could workaround this by defining a C# type that can store either one value or a sequence of values - using the F# notation it would be:</p> <pre><code>type EnumerationResult&lt;'a&gt; = | One of 'a | Seq of seq&lt;'a&gt; </code></pre> <p>(translate this to C# in any way you like :-))</p> <p>Now, you could write something like:</p> <pre><code>static IEnumerable&lt;EnumerationResult&lt;string&gt;&gt; Visitor (string root, string filter) { foreach (var file in Directory.GetFiles(root, filter)) yield return EnumerationResult.One(file); foreach (var subdir in Directory.GetDirectories(root)) yield return EnumerationResult.Seq(Visitor(subdir, filter)) } } </code></pre> <p>To use it, you'd have to write a function that flattens EnumerationResult, which could be an extension method in C# with the following signature:</p> <pre><code>IEnumerable&lt;T&gt; Flatten(this IEnumerable&lt;EnumerationResult&lt;T&gt;&gt; res); </code></pre> <p>Now, this is a part where it gets tricky - if you implemented this in a straighforward way, it would still contain "forach" to iterate over the nested "Seq" results. However, I believe that you could write an optimized version that wouldn't have quadratic complexity.</p> <p>Ok.. I guess this is a topic for a blog post rather than something that could be fully described here :-), but hopefully, it shows an idea that you can try following!</p> <p><em>[EDIT: But of course, you can also use naive implementation of "Flatten" that would use "SelectMany" just to make the syntax of your C# iterator code nicer]</em></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload