F# Seq<Seq<char>> 转换为字符串

A19*_*919 1 f#

使用 F# 并尝试从 string 中获取abcdefghijklmnop另一个 string aeimbfjncgkodhlp

let input ="abcdefghijklmnop"

let convertList (input:string)=
    input
    |> Seq.mapi(fun i ei->  i % 4, ei)
    |> Seq.groupBy (fst)
    |> Seq.map(fun (g,s)-> s |> Seq.map snd)

let result = convertList input

result
Run Code Online (Sandbox Code Playgroud)

在我的函数中,最终结果是seq<seq<char>>,如何转换seq<seq<char>>为字符串?

Tom*_*cek 5

在 F# 中,string类型实现了seq<_>接口,这意味着您可以将字符串视为字符序列,但不幸的是,反过来有点难看。

一种选择是seq<seq<char>>seq<char>使用一个using Seq.concat,然后将其转换seq<string>为您可以使用的连接String.concat

result |> Seq.concat |> Seq.map string |> String.concat ""
Run Code Online (Sandbox Code Playgroud)

可能更有效但不太优雅的选择是转换seq<char>array<char>,然后您可以将其传递给System.String构造函数:

result |> Seq.concat |> Seq.toArray |> System.String
Run Code Online (Sandbox Code Playgroud)