我有像这样的C#代码
// Class definition
Class Letter {
char c;
int id;
}
// A C# function to obtain an array of letters
Array[Letter] getLetters();
来自F#的函数调用
let L = getLetters()
我想将L
分成按id
分组的字符串列表,假设id
从0
开始到N
。我怎么能在F#中做到这一点?我是F#新手。
答案 0 :(得分:1)
let rslt letters =
letters
|> Seq.groupBy (fun l -> l.id) // Groups Letters into sequence of pair (id * seq<Letter>)
|> Seq.map (fun (_, str) -> str |> Seq.map (fun l -> l.c) ) // Maps the sequence to seq<seq<<char>>
|> Seq.map (fun ls -> System.String.Join("", ls)) // Maps the sequence to seq<string>
|> List.ofSeq // Creates list of strings from seq<string>