Interactive Session #8: Pipeline Operator

The pipeline operator |> is a quite unique feature of F#, it allows to connect functions which take one argument together by passing the result of one function to the next.

> let add1 x = x+1.0;;

val add1 : float -> float

> 1.0 |> add1;;
val it : float = 2.0
> 1.0 |> add1 |> add1;;
val it : float = 3.0

This may also be applied to a complete lists of values.

> let l = [1.0; 2.0; 3.0; 4.0];;

val l : float list = [1.0; 2.0; 3.0; 4.0]

> l |> (List.map add1);;
val it : float list = [2.0; 3.0; 4.0; 5.0]

The type of the operator may be explored as follows by putting the operator into parentheses.

> (|>);;
val it : ('a -> ('a -> 'b) -> 'b) = <fun:it@82-1>

The type signature has the meaning that the first operand (left side) is of generic type ‘a and the right side is a function of generic type ‘a -> ‘b. The resulting type is ‘b, which means that the function on the right side will be applied to the value on the left side. Therefore the pipeline operator is equivalent to the following generic function:

> let pipeline a b = b(a);;

val pipeline : 'a -> ('a -> 'b) -> 'b

Alternatively the pipeline operator may also be applied to an array or a sequence of values.

> let x_n = [|1..100|] |> Array.map (fun x -> float(x)/10.0);;
val x_n : float [] =
 [|0.1; 0.2; 0.3; 0.4; 0.5; 0.6; 0.7; 0.8; 0.9; 1.0; 1.1; 1.2; 1.3; 1.4; 1.5;
 1.6; 1.7; 1.8; 1.9; 2.0; 2.1; 2.2; 2.3; 2.4; 2.5; 2.6; 2.7; 2.8; 2.9; 3.0;
...

> let y_n = x_n |> Array.map (fun x -> sin(x));;

val y_n : float [] =
 [|0.09983341665; 0.1986693308; 0.2955202067; 0.3894183423; 0.4794255386;
 0.5646424734; 0.6442176872; 0.7173560909; 0.7833269096; 0.8414709848;

To draw a list of values we need an array of tuples to use the function DrawLines. Two arrays of values with same length may be converted into an array of tuples with Array.zip. Additionally, the points need to be translated to be in the middle of the form.

> open System.Drawing;;
> let points = Array.zip x_n y_n |> Array.map (fun (x, y) ->
-   PointF(float32(10.0*x), float32(100.0-10.0*y)));;

val points : PointF [] =
 [|{X=1, Y=99.00166}; {X=2, Y=98.01331}; {X=3, Y=97.0448}; {X=4, Y=96.10582};
 {X=5, Y=95.20574}; {X=6, Y=94.35358}; {X=7, Y=93.55782}; {X=8, Y=92.82644};
...

Now we may draw this list of points into a form.

> open System.Windows.Forms;;
> let f = new Form(Visible=true, Width=300, Height=300)
- let gfx = f.CreateGraphics()
- let pen = new Pen(Color.Black, 2.0f);;

val f : Form = System.Windows.Forms.Form, Text:
val gfx : Graphics
val pen : Pen
> gfx.DrawLines(pen, points);;
val it : unit = ()

Further reading:

Advertisements
This entry was posted in Algorithms, General, Language. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s