﻿ Function Topics: Pipelining Functions Function Topics: Pipelining Functions

 Description
 Consider the previous example where we were calculating the area of a sphere from the area of a circle:
```let PI = 3.14156;

let multiplyBy4 value = 4.00 * value; // Area of a Sphere```

Another way to chain functions is referred to as pipelining. It uses slightly different rules compared to composition. First, Instead of << or >>, pipelining uses the <| or the |> operator. Second, when performing the operation, pass the argument to the first function that will execute. As mentioned for pipelining:

• If you use the |> operator, the function on the left side will execute first, and that's the function to which you must pass the argument(s). Here is an example:
```let PI = 3.14156;

let multiplyBy4 value = 4.00 * value; // Area of a Sphere

// Passing the argument to the left side of the |> operator
sprintf "Area: %f" (calculateCircleArea 25.85 |> multiplyBy4)```
This would produce:
`Area of Sphere: 8397.044308`
• If you use the <| operator, the function on the right side of the operator will execute first. That's the function to which you must pass the argument(s)

You can pipeline as many functions as you want. You can perform the operation directly where needed. Here is an example that pipes two functions:

```let greetings msg = msg + " - F# is fun!"
let define    msg = "Announcement: " + msg

let g = "Welcome to the wonderful world of functional programming" |> define |> greetings

sprintf "%s" g```

Here is an example with many functions:

```let hourlySalary = 25.85

let calculateDailySalary salary = salary * 8.00
let calculateWeeklySalary salary = salary * 8.00 * 5.00
let weeklySalary daily = daily * 5.00
let calculateBiMonthlySalary salary = salary * 8.00 * 5.00 * 2.00
let biMonthlySalary weekly = weekly * 2.00
let calculateMonthlySalary salary = salary * 8.00 * 5.00 * 2.00 * 2.00
let monthlySalary biMonthly = biMonthly * 2.00
let calculateYearlySalary salary = salary * 8.00 * 5.00 * 2.00 * 2.00 * 12.00
let yearlySalary monthly = monthly * 12.00

let yearlyEvaluation = calculateDailySalary hourlySalary |> weeklySalary |> biMonthlySalary |> monthlySalary |> yearlySalary

sprintf "Yearly Salary: %0.0f" yearlyEvaluation```

As mentioned for the composition, if the pipelining involves many functions and makes the line of code too long, to make it easy to read, you can write each function call and its operator on its own line. Here is an example:

```let yearlyEvaluation =
calculateDailySalary hourlySalary
|> weeklySalary
|> biMonthlySalary
|> monthlySalary
|> yearlySalary```

Instead of first creating the functions, you can define them directly where needed. In this case, you should create them as lambda expressions (we will see an example in the next section).

 Chaining an Argument to Functions

On of the most valuable features of pipelining (and composing) is that you can pass (an) argument(s) once to a series of functions that can perform operations in chain, one after another. In this case, each function uses the return value of the previous function and applies that value to its own assignment. To do this, write the argument followed by a call to each function that is preceded by the necessary pipelining operator (<| or |>).

`argument(s) <| or |> function 1 <| or |> function 2 <| or |> function_n`

Here is an example:

```let hourlySalary = 25.85

let EvaluateDailySalary salary = salary * 8.00
let multiplBy5 value = value * 5.00
let multiplBy2 value = value * 2.00
let multiplBy12 value = value * 12.00

hourlySalary |> EvaluateDailySalary |> multiplBy5 |> multiplBy2 |> multiplBy2 |> multiplBy12 |> printfn "Yearly Salary: %0.02f"```

To. Here is an example:

```hourlySalary
|> EvaluateDailySalary
|> multiplBy5
|> multiplBy2
|> multiplBy2
|> multiplBy12
|> printfn "Yearly Salary: %0.02f"```

This. Here are examples:

```let hourly = 25.85
let salary = 34.50

hourly |> fun value -> value * 8.00 |> fun value -> value * 5.00 |> fun value -> value * 2.00 |> fun value -> value * 2.00 |> fun value -> value * 12.00 |> sprintf "Yearly Salary: %0.02f"

salary
|> fun value -> value * 8.00
|> fun value -> value * 5.00
|> fun value -> value * 2.00
|> fun value -> value * 2.00
|> fun value -> value * 12.00
|> sprintf "Yearly Salary: %0.0f"```