映射以下内容的最佳方法是什么:
[|"A"; "B"; "C"; "D"|]
Run Code Online (Sandbox Code Playgroud)
至
[|("","A","B"); ("A","B","C"); ("B","C","D"); ("C","D","")|]
Run Code Online (Sandbox Code Playgroud)
?
我更喜欢基于惯用组合的方法:将空字符串附加到数组的两端,用于Seq.windowed 3生成三个项目的滑动数组序列,最后将每个序列元素映射到元组并将序列转换回数组:
let conv source =
Array.concat [[|String.Empty|]; source; [|String.Empty|]] |> Seq.windowed 3
|> Seq.map (fun x -> x.[0],x.[1],x.[2]) |> Seq.toArray
Run Code Online (Sandbox Code Playgroud)
conv [|"A";"B";"C";"D"|]在FSI下测试产量:
val it : (string * string * string) [] =
[|("", "A", "B"); ("A", "B", "C"); ("B", "C", "D"); ("C", "D", "")|]
Run Code Online (Sandbox Code Playgroud)