6 people like it.

Trace-mode pipeline operators

This fake module can trace pipeline operators.

(an imitation) pipeline operator

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
module NinjaOperators =
    let private print x y = printfn "%A ==> %A" x y

    let ( |> ) x f = let result = f x in print x result ; result
    let ( <| ) f x = x |> f

    let ( ||> ) t f = t |> fun (x, y) -> f x y
    let ( <|| ) f t = t ||> f

    let ( |||> ) t f = t |> fun (x, y, z) -> f x y z
    let ( <||| ) f t = t |||> f

Usage

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
#if INTERACTIVE
// Please call the NinjaOperators module when you need to debug.
open NinjaOperators

// If you want to reset fsi, please open the Operators module again.
// > open Operators ;;
#endif

let f x = x * 10
let g x = x + 10

let sample1 () = 10 |> f |> g
// Output:
// 10 ==> 100
// 100 ==> 110
// val it : int = 110

let sample2 () =
    [1 .. 5]
    |> List.map f
    |> List.map g
    |> List.sum
// Output:
// [1; 2; 3; 4; 5] ==> [10; 20; 30; 40; 50]
// [10; 20; 30; 40; 50] ==> [20; 30; 40; 50; 60]
// [20; 30; 40; 50; 60] ==> 200
// val it : int = 200

let sample3 () = g <| (f <| 10)
// Output:
// 10 ==> 100
// 100 ==> 110
// val it : int = 110

let sample4 () = (1, 2) ||> ( + )
// Output:
// (1, 2) ==> 3
// val it : int = 3

let sample5 () = ( * ) <|| (10, 10)
// Output:
// (10, 10) ==> 100
// val it : int = 100

let sample6 () = (1, 2, 3) |||> fun x y z -> x + y + z
// Output:
// (1, 2, 3) ==> 6
// val it : int = 6

let sample7 () = (fun x y z -> x * y * z) <||| (1, 2, 3)
// Output:
// (1, 2, 3) ==> 6
// val it : int = 6
val private print : x:'a -> y:'b -> unit

Full name: Script.NinjaOperators.print
val x : 'a
val y : 'b
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val f : ('a -> 'b)
val result : 'b
val t : 'a * 'b
val f : ('a -> 'b -> 'c)
val t : 'a * 'b * 'c
val f : ('a -> 'b -> 'c -> 'd)
val z : 'c
val f : x:int -> int

Full name: Script.f
val x : int
val g : x:int -> int

Full name: Script.g
val sample1 : unit -> int

Full name: Script.sample1
val sample2 : unit -> int

Full name: Script.sample2
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val map : mapping:('T -> 'U) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.map
val sum : list:'T list -> 'T (requires member ( + ) and member get_Zero)

Full name: Microsoft.FSharp.Collections.List.sum
val sample3 : unit -> int

Full name: Script.sample3
val sample4 : unit -> int

Full name: Script.sample4
val sample5 : unit -> int

Full name: Script.sample5
val sample6 : unit -> int

Full name: Script.sample6
val y : int
val z : int
val sample7 : unit -> int

Full name: Script.sample7

More information

Link:http://fssnip.net/gK
Posted:11 years ago
Author:Nobuhisa
Tags: pipeline , debug