全部!
这段代码有什么问题?我无法理解 Seq.Map 做错了什么。这是错误消息:类型“单元”与类型“seq<'a>”不兼容
let getPathToLibFile value =
let regex = new Regex("\"(?<data>[^<]*)\"")
let matches = regex.Match(value)
matches.Value
let importAllLibs (lines:string[]) =
lines
|> Seq.filter isImportLine
|> Seq.iter (printfn "Libs found: %s")
|> Seq.map getPathToLibFile // error in this line
|> Seq.iter (printfn "Path to libs: %s")
Run Code Online (Sandbox Code Playgroud)
Seq.Map 上有什么可以理解的例子吗?
PS 来自 wiki 的示例(它有效):
(* Fibonacci Number formula *)
let rec fib n =
match n with
| 0 | 1 -> n
| _ -> fib (n - 1) + fib (n - 2)
(* Print even fibs *)
[1 .. 10]
|> List.map fib
|> List.filter (fun n -> (n % 2) = 0)
|> printlist
Run Code Online (Sandbox Code Playgroud)
我怀疑问题实际上是您之前的电话。
Seq.iter不返回任何内容(或者更确切地说,返回unit),因此您不能在管道中间使用它。尝试这个:
let importAllLibs (lines:string[]) =
lines
|> Seq.filter isImportLine
|> Seq.map getPathToLibFile
|> Seq.iter (printfn "Path to libs: %s")
Run Code Online (Sandbox Code Playgroud)
...然后如果你真的需要打印出“找到的库”行,你可以添加另一个执行打印并只返回输入的映射:
let reportLib value =
printfn "Libs found: %s" value
value
let importAllLibs (lines:string[]) =
lines
|> Seq.filter isImportLine
|> Seq.map reportLib
|> Seq.map getPathToLibFile
|> Seq.iter (printfn "Path to libs: %s")
Run Code Online (Sandbox Code Playgroud)
这很可能是无效的 F#,但我认为目标是正确的 :)