Thanks guys.

By on 3/20/2009 4:18 AM ()

Hi everybody. I have a question. What is the difference between |> and >> ?

For me, both operators made sense when I saw their definitions, and realized they are pure sugar:

1
2
let inline (>>) f g x = g(f x)
let inline (|>) x f = f x

The function composition operator (>>) has 2 of 3 arguments applied.
The pipe-forward operator (|>) simply inverts the arguments.

You could define (>>) in terms of (|>), if you wanted to:

1
let inline (>>) f g x = x |> f |> g

This blog has a pretty good explanation:
[link:blogs.msdn.com]

By on 3/19/2009 8:56 PM ()

Hi everybody. I have a question. What is the difference between |> and >> ?

This blog has a pretty good explanation:
[link:blogs.msdn.com]</quote> Pretty good explanation. Thanks.

By on 3/20/2009 4:21 AM ()

Hi, I've been wondering the same thing, as they look so similar.

v.labreque's answer sums it up... ">>" is function composition, and "|>" is function application.

Imagine that in your example you are trying to define a functino "foo" which combines the three operations.

My first approach would be with the "|>" operator....

1
let foo_applic <b>x </b>= <b>x </b>|> multiply_add 512 |> toStr |> rev

Here we start with an instance x, and "apply" a function to it, then "apply" another function to the result.

But, the better F# way is with ">>"

1
let foo_composit = multiply_add 512 >> toStr >> rev

Both are used the same way....

1
2
3
4
> foo_composit 2;;
val it : String = "641262"
> foo_applic 2;;
val it : String = "641262"
By on 3/19/2009 1:56 PM ()

Another way to think of these:

|> "do it right now."
>> "build me another function that combines these functions"

1
2
3
4
5
6
7
8
9
10
module Examples =
  let Add1 x =
    x + 1
    
  let Add2 =
    Add1 >> Add1

open Examples
printfn "result: %d" (Add2 3)
printfn "result: %d" (3 |> Add1 |> Add1)

Results:

1
2
3
4
5
6
7
8
module Examples = begin
  val Add1 : int -> int
  val Add2 : (int -> int)
end

result: 5
result: 5
> 
By on 3/19/2009 9:55 AM ()
1
2
3
4
5
6
7
8
9
>> is function composition, |> is function application.

> (>>);;

val it : (('a -> 'b) -> ('b -> 'c) -> 'a -> 'c) = <fun:clo@6>

> (|>);;

val it : ('a -> ('a -> 'b) -> 'b) = <fun:clo@7>

In your example: at no point in your definition of "result3" do you create fun x -> rev (toStr (multiply_add 512 x)), while in "result4" you first create this function, and then evaluate it with x=2.

By on 3/19/2009 9:02 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper