In a code exchange earlier this week, a coworker saw this symbol in my code and rapidly he asked what it was. He seemed very surprised finding about the Pipe Forward Operator ( )that has. weird |> F# My friend is learning and he wants to use the on it, a construct lacks of. Let’s see how we can add this construct to every object. Scala |> Scala Scala Functional Composition Using the makes very easy to compose functions. The code becomes simpler to write and understand by others. |> Let’s see an example: Can we make use of so this composition is simpler to write? Well, we could write something like this: |> We just redefined so instead of nested function calls, we apply a function to an object and then another and so on, linearly. s I don’t know if it actually makes sense to have something like in , but we can implement it easily. Scala Pipe Operator in Scala Let’s take a look at how defines . F# |> We can mimic the same in let’s see how: Scala, We have defined a class that receives a value of type so we can apply the function to when calling the method . Pipe a A f a |> We also defined an object to do an to of any we want. Please, take a look at the article for more information about . PipeOps implicit conversion Pipe object Implicit conversions in Scala for C# developers implicit Once this has been defined, we can use in any object. Let’s see it in practice. |> The new way of defining PIPE is Added after original writing: Added after original writing ends here. In here, we transform into a and then we apply the passed function to it. helps a lot in the process. 5 Pipe(5) f: x => x + 1 Scala implicit Map Let’s see how we could define a function. map applies the function to each item in . The signature is a little disturbing since normally we would send as the second parameter and as the first one in order to take advantage of some of the syntax sugars. However, there is nothing wrong with it, it is just another way to define . map f items f items Scala map We can call by doing: map Let’s see another example with various operators. |> Note how we have defined . It is being composed by and using the operator. If we have to do this using , the code should look like follows: square filter map |> without |> Again, this is not how we normally define these kind of functions in , but it will be essentially the same. Scala Let’s compare our current definitions to the other way we would do it which is more natural to the language. Scala Conclusions We have seen how composes functions and how functions are composed in by using . We also saw how we can implement the operator in and comparing how we define these functions by using or not the Pipe Forward Operator. I am not sure if people are actually using since has another constructors and code styling that enable another set of possibilities, yet, we still can use and its benefits. Scala F# |> |> Scala |> Scala |> Hope this piece is useful to you and if you enjoyed it, please, recommend it ( ) so others can benefit from it. green heart is how hackers start their afternoons. We’re a part of the family. We are now and happy to opportunities. Hacker Noon @AMI accepting submissions discuss advertising &sponsorship To learn more, , , or simply, read our about page like/message us on Facebook tweet/DM @HackerNoon. If you enjoyed this story, we recommend reading our and . Until next time, don’t take the realities of the world for granted! latest tech stories trending tech stories
Share Your Thoughts