这个 C# 代码是如何用函数式语言(F#?Haskel?)完成的?

发布于 2024-11-18 08:08:00 字数 600 浏览 0 评论 0原文

我如何用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑,眼淚并存 2024-11-25 08:08:00

因此,如果我正确地阅读了您的代码,您的输入看起来像这样:

[...]
Foo
https://example1.com
https://example2.com
Bar
https://example3.com
Baz
Xyzzy
https://example4.com
[...]

由此,您希望标头与其下方的 URL 分组。下面是执行此操作的 Haskell 程序:

import Data.List (isPrefixOf)

groupUrls :: [String] -> [(String, [String])]
groupUrls [] = []
groupUrls (header:others) = (header, urls) : groupUrls remaining
  where (urls, remaining) = span (isPrefixOf "https") others

main = do
    input <- readFile "\\\\ad1\\\\Users\\aanodide\\Desktop\\APIUserGuide.txt"
    let slice = take (471 - 375 + 1) $ drop 374 $ lines input
    let kvp = groupUrls slice
    print kvp

输出:

[("Foo",["https://example1.com","https://example2.com"]),("Bar", ["https://example3.com"]),("Baz",[]),("Xyzzy",["https://example4.com"])]

这里感兴趣的关键函数是 span,此处用于获取以 "https" 开头的连续行并将它们一起返回与其余的行,然后是递归处理。

So, if I read your code correctly, your input looks something like this:

[...]
Foo
https://example1.com
https://example2.com
Bar
https://example3.com
Baz
Xyzzy
https://example4.com
[...]

From this, you want the headers grouped with the URLs below them. Here's a Haskell program that does this:

import Data.List (isPrefixOf)

groupUrls :: [String] -> [(String, [String])]
groupUrls [] = []
groupUrls (header:others) = (header, urls) : groupUrls remaining
  where (urls, remaining) = span (isPrefixOf "https") others

main = do
    input <- readFile "\\\\ad1\\\\Users\\aanodide\\Desktop\\APIUserGuide.txt"
    let slice = take (471 - 375 + 1) $ drop 374 $ lines input
    let kvp = groupUrls slice
    print kvp

Output:

[("Foo",["https://example1.com","https://example2.com"]),("Bar", ["https://example3.com"]),("Baz",[]),("Xyzzy",["https://example4.com"])]

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文