Etheraex
Etheraex

Reputation: 506

F# Issue when trying to pipe multiple values between functions

I am trying to refactor this code:

for projPath in projectAssemblyInfoPaths do
    let assemblyInfoLines = ReadWholeFileFn(projPath)
    let updatedAssemblyInfoLines = UpdateFileFn(assemblyInfoLines, newVersion);
    SaveFileFn(updatedAssemblyInfoLines, projPath);

To look something like this:

for projPath in projectAssemblyInfoPaths do
    ((ReadWholeFileFn(projPath), newVersion) ||> UpdateFileFn, projPath) ||> SaveFileFn

But, I'm getting type mismatch for both UpdateFileFn and 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'

And these are my function definitions:

let ReadWholeFileFn (filePath: string): string[];

let UpdateFileFn (lines: string[], newVersion: string): string[];

let SaveFileFn (lines: string[], fileName: string): unit;

I have tried a simple piece of code like:

(1, 2) ||> prinf "%d%d"

And that works fine, not sure what I'm missing when working with functions.

Upvotes: 1

Views: 154

Answers (1)

user1981
user1981

Reputation: 586

You do not really need the ||> operator for this. You can bind the arguments using partially applied functions which I think is appropriate for your case. However then you need to change the signatures so the most chaning part of your function is the last argument. Then it becomes like below

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

A clean F# pipeline.

Upvotes: 4

Related Questions