如何定义一个函数来将一个字母数组分成一组字符串

时间:2015-03-27 17:53:09

标签: f#

我有像这样的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分组的字符串列表,假设id0开始到N。我怎么能在F#中做到这一点?我是F#新手。

1 个答案:

答案 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>