Geeks With Blogs
Mark Pearl

 

So I must admit, the first time I saw it I scratched my head and thought to myself, why will I ever use that? Several months since then and now I love the Forward Pipe (FP) in F#.

Why?

For me, the the FP makes me think of a problem in a different way. In C# when I attempt to solve I problem I typically break it down in steps, once I have reached the end result I have my smallest step, and then I work my methods backwards so that I can get back to the original input… in a very real way it makes me look and think of a problem in a bottom up approach… whereas I often think as humans we solve problems from a top down approach.

Now.. I am no psychologist, but how often when you ask directions from someone do they start by giving you the end destination and then give you directions from there back to where you are? It would be extremely hard to find the place… Yet in many ways that’s how I have solved problems in the past with C# and other languages and it almost seems like the languages write the solution in that way as well.

For instance, take my very contrived C# example for solving the following problem…

Take the value 10, get the square root of it and then round it off…

Math.Round(Math.Sqrt(10));

Now look at how the syntax was written – it seems to be out of sequence to how the problem was originally written in English… Take the same problem and using the |> operator in F# you see it is expressed in a totally different sequence of declaring it… almost identical to how the problem was phrased in English

10 |> float |> Math.Sqrt |> Math.Round

 

So what prompted this post? I have been hating type casting of numbers in F# up to now… it just seemed messy? Take for example the following code used to get the upper bound of a prime number…

let upperBound n:int =
    let f = float(n)
    let ln x = Math.Log x
    int(f*(ln f) + f*(ln (ln f)))

With the forward pipe this seems to read in my mind at least as more natural

let upperBound n:int =
    let f = n |> float
    let ln x = Math.Log x
    f*(ln f) + f*(ln (ln f)) |> int

 

So, I love the forward pipe! Absolutely love it!

Posted on Monday, June 28, 2010 6:34 PM F# | Back to top


Comments on this post: F# – Put that in your pipe and smoke it |>

# re: F# – Put that in your pipe and smoke it |>
Requesting Gravatar...
"that |> smoke"... awesome!

"let h x = g x |> f" looks better than "let h x = f (g x)". Even better is "let h x = x |> g |> f". But dude, best of all is "let h = g >> f".

What's the 'point' of 'x"? (e.g. "let ln x = Math.Log x" vs "let ln = Math.Log")
Left by Anon on Jun 29, 2010 7:07 PM

# re: F# – Put that in your pipe and smoke it |>
Requesting Gravatar...
Lol... absolutely true! I always forget about the >> - what is it called again?
Left by MarkPearl on Jun 29, 2010 8:04 PM

# re: F# – Put that in your pipe and smoke it |>
Requesting Gravatar...
Yep, forward pipes are nice, especially for transforms. "url |> getPage |> extractLinks |> filterBadSites" is more readable filterBadSites(extractLinks(getPage url)).

Backwards pipe <| is quite nice too, for controlling precedence. I often prefer it to paranthesis, e.g. "not <| foo x y" instead of "not (foo x y)".
Left by John Reynolds on Jun 29, 2010 8:58 PM

# re: F# – Put that in your pipe and smoke it |>
Requesting Gravatar...
> Now.. I am no psychologist, but how often when you ask directions from someone do they start by giving you the end destination and then give you directions from there back to where you are?

This is true, but consider how they work out those directions. Unless it is a route they know well it is likely they worked out at least part of the route in reverse. To get to X you need to be at W, to get to W you need to be at V ...
Left by Richard on Jul 06, 2010 12:35 PM

# re: F# – Put that in your pipe and smoke it |>
Requesting Gravatar...
True Richard...

I think what I like is the ease in which you can change directions based on the problem.
Left by Markpearl on Jul 09, 2010 7:03 PM

Your comment:
 (will show your gravatar)


Copyright © MarkPearl | Powered by: GeeksWithBlogs.net