这个 C# 代码是如何用函数式语言(F#?Haskel?)完成的?
我如何用 F# 或 Haskel 或类似的函数式语言编写此 C# 代码?
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;
});
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;
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,如果我正确地阅读了您的代码,您的输入看起来像这样:
由此,您希望标头与其下方的 URL 分组。下面是执行此操作的 Haskell 程序:
输出:
这里感兴趣的关键函数是
span
,此处用于获取以"https"
开头的连续行并将它们一起返回与其余的行,然后是递归处理。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.