How to declare pipelines?

Does it matter how I declare a pipeline? I know three ways:

let hello name = "Hello " + name + "!" let solution1 = hello <| "Homer" let solution2 = "Homer" |> hello 

What would you choose? solution1 or solution2 - and why?

+4
source share
2 answers

As mentioned, the |> tube operator helps with function compilation and type inferences. This allows you to change function parameters so that you can first place the last function parameter. This allows a chain of functions that are very readable (similar to LINQ in C #). Your example does not show the power of this - it really shines when you have a pipeline transformation configured for several functions in a line.

Using the |> chain, you can write:

 let createPerson n = if n = 1 then "Homer" else "Someone else" let hello name = "Hello " + name + "!" let solution2 = 1 |> createPerson |> hello |> printf "%s" 

Feedback Operator Advantage <| is that it changes the priority of the operator, so it can save a lot of brackets: function arguments are usually evaluated from left to right using <| you don't need parentheses if you want to pass the result of one function to another function - your example really doesn't use this.

They would be equivalent:

 let createPerson n = if n = 1 then "Homer" else "Someone else" let hello name = "Hello " + name + "!" let solution3 = hello <| createPerson 1 let solution4 = hello (createPerson 1) 
+7
source

F # is read from top to bottom, from left to right. For this reason, the |> operator is used much more than <| as it helps to infer type.

+3
source

Source: https://habr.com/ru/post/1343535/


All Articles