尝试在函数之间传递多个值时出现 F# 问题
F# Issue when trying to pipe multiple values between functions
我正在尝试重构这段代码:
for projPath in projectAssemblyInfoPaths do
let assemblyInfoLines = ReadWholeFileFn(projPath)
let updatedAssemblyInfoLines = UpdateFileFn(assemblyInfoLines, newVersion);
SaveFileFn(updatedAssemblyInfoLines, projPath);
看起来像这样:
for projPath in projectAssemblyInfoPaths do
((ReadWholeFileFn(projPath), newVersion) ||> UpdateFileFn, projPath) ||> SaveFileFn
但是,UpdateFileFn
和 SaveFileFn
的类型不匹配
UpdateFileFn
:
FS0001: Type mismatch. Expecting a
'string [] -> string -> 'a'
but given a
'string [] * string -> string []'
The type 'string []' does not match the type 'string [] * string'
SaveFileFn
:
FS0001: Type mismatch. Expecting a
'string [] * string -> string -> 'a'
but given a
'string [] * string -> unit'
The type 'string -> 'a' does not match the type 'unit'
这些是我的函数定义:
let ReadWholeFileFn (filePath: string): string[];
let UpdateFileFn (lines: string[], newVersion: string): string[];
let SaveFileFn (lines: string[], fileName: string): unit;
我试过一段简单的代码,例如:
(1, 2) ||> prinf "%d%d"
这很好用,不确定我在使用函数时遗漏了什么。
您并不真的需要 ||> 运算符。您可以使用我认为适合您的情况的部分应用函数来绑定参数。但是,您需要更改签名,以便函数中变化最大的部分是最后一个参数。然后就变成下面这样
let ReadWholeFileFn2 (filePath: string): string[]
let UpdateFileFn2 (newVersion: string) (lines: string[]): string[]
let SaveFileFn2 (fileName: string) (lines: string[]) : unit
for projPath in projectAssemblyInfoPaths do
ReadWholeFileFn2 projPath
|> UpdateFileFn2 newVersion
|> SaveFileFn2 projPath
干净的 F# 管道。
我正在尝试重构这段代码:
for projPath in projectAssemblyInfoPaths do
let assemblyInfoLines = ReadWholeFileFn(projPath)
let updatedAssemblyInfoLines = UpdateFileFn(assemblyInfoLines, newVersion);
SaveFileFn(updatedAssemblyInfoLines, projPath);
看起来像这样:
for projPath in projectAssemblyInfoPaths do
((ReadWholeFileFn(projPath), newVersion) ||> UpdateFileFn, projPath) ||> SaveFileFn
但是,UpdateFileFn
和 SaveFileFn
UpdateFileFn
:
FS0001: Type mismatch. Expecting a
'string [] -> string -> 'a'
but given a
'string [] * string -> string []'
The type 'string []' does not match the type 'string [] * string'
SaveFileFn
:
FS0001: Type mismatch. Expecting a
'string [] * string -> string -> 'a'
but given a
'string [] * string -> unit'
The type 'string -> 'a' does not match the type 'unit'
这些是我的函数定义:
let ReadWholeFileFn (filePath: string): string[];
let UpdateFileFn (lines: string[], newVersion: string): string[];
let SaveFileFn (lines: string[], fileName: string): unit;
我试过一段简单的代码,例如:
(1, 2) ||> prinf "%d%d"
这很好用,不确定我在使用函数时遗漏了什么。
您并不真的需要 ||> 运算符。您可以使用我认为适合您的情况的部分应用函数来绑定参数。但是,您需要更改签名,以便函数中变化最大的部分是最后一个参数。然后就变成下面这样
let ReadWholeFileFn2 (filePath: string): string[]
let UpdateFileFn2 (newVersion: string) (lines: string[]): string[]
let SaveFileFn2 (fileName: string) (lines: string[]) : unit
for projPath in projectAssemblyInfoPaths do
ReadWholeFileFn2 projPath
|> UpdateFileFn2 newVersion
|> SaveFileFn2 projPath
干净的 F# 管道。