gpt4 book ai didi

c# - 如何使用 LINQ 查询在 Select with F# 中包含索引

转载 作者:太空狗 更新时间:2023-10-30 00:07:10 25 4
gpt4 key购买 nike

我在 C# 中有这个代码示例,它从数组中输出索引和值:

static void Sample_Select_Lambda_Indexed()
{
string[] arr = { "my", "three", "words" };

var res = arr.Select((a, i) => new
{
Index = i,
Val = a
});

foreach(var element in res)
Console.WriteLine(String.Format("{0}: {1}", element.Index, element.Val));
}

输出是:

0: my
1: three
2: words

我想在 F# 中进行类似的查询,并且是这样开始的:

let arr = [|"my"; "three"; "words"|]

let res = query {
for a in arr do
// ???
}

我将如何完成此 LINQ 查询?

最佳答案

您可以使用Seq.mapi:

let res = arr |> Seq.mapi (fun i a -> ...)

或者直接使用Seq.iteri:

arr |> Seq.iteri (fun i v -> printfn "%i: %s" i v)

或者只是:

arr |> Seq.iteri (printfn "%i: %s")

关于c# - 如何使用 LINQ 查询在 Select with F# 中包含索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30445607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com