this post中的示例有一个示例
open System.IO
let lines =
File.ReadAllLines("tclscript.do")
|> Seq.map (fun line ->
let newLine = line.Replace("{", "{{").Replace("}", "}}")
newLine )
File.WriteAllLines("tclscript.txt", lines)
在编译时出错。
error FS0001: This expression was expected to have type
string []
but here has type
seq<string>
如何将seq转换为string []以删除此错误消息?
答案 0 :(得分:6)
您可以使用
File.WriteAllLines("tclscript.txt", Seq.toArray lines)
或者只是附上
|> Seq.toArray
在Seq.map调用之后。
(另请注意,在.NET 4中,WriteAllLines的重载确实占用了Seq)
答案 1 :(得分:6)
根据Jaime的回答,由于ReadAllLines()
返回一个数组,只需使用Array.map
代替Seq.map
open System.IO
let lines =
File.ReadAllLines("tclscript.do")
|> Array.map (fun line ->
let newLine = line.Replace("{", "{{").Replace("}", "}}")
newLine )
File.WriteAllLines("tclscript.txt", lines)
答案 2 :(得分:0)
就个人而言,我更喜欢序列表达式而不是高阶函数,除非你通过一系列函数来输出输出。它通常更干净,更易读。
let lines = [| for line in File.ReadAllLines("tclscript.do") -> line.Replace("{", "{{").Replace("}", "}}") |]
File.WriteAllLines("tclscript.txt", lines)
let lines =
let re = System.Text.RegularExpressions.Regex(@"#(\d+)")
[|for line in File.ReadAllLines("tclscript.do") ->
re.Replace(line.Replace("{", "{{").Replace("}", "}}"), "$1", 1)|]
File.WriteAllLines("tclscript.txt", lines)