How could I write this C# code in F# or Haskel, or a similar functional language?
var lines = File.ReadAllLines(@"\\ad1\\Users\aanodide\Desktop\APIUserGuide.txt");
// XSDs are lines 375-471
var slice = lines.Skip(374).Take(471-375+1);
var kvp = new List<KeyValuePair<string, List<string>>>();
slice.Aggregate(kvp, (seed, line) =>
{
if(line.StartsWith("https"))
kvp.Last().Value.Add(line);
else
kvp.Add(
new KeyValuePair<string,List<string>>(
line, new List<string>()
)
);
}
return kvp;
});
So, if I read your code correctly, your input looks something like this:
From this, you want the headers grouped with the URLs below them. Here’s a Haskell program that does this:
Output:
The key function of interest here is
span, which is used here to take the consecutive lines starting with"https"and return them together with the remaining lines, which are then dealt with recursively.