typestar

The pipeline in OCaml

|> feeds the left value forward; data reads in flow order.

(* |> feeds the left value forward: data reads in flow order. *)
let counts = [12; 5; 31; 8; 19]

let busy_pools =
  counts
  |> List.filter (fun c -> c > 10)
  |> List.length

let () =
  Printf.printf "Pools above ten animals: %d\n" busy_pools;
  (* The pipeless version reads inside out. *)
  Printf.printf "Total: %d\n" (List.fold_left ( + ) 0 counts);
  counts |> List.map string_of_int |> String.concat ", "
         |> print_endline

How it works

  1. counts |> List.filter ... |> List.length reads as a sentence.
  2. The pipeless version nests inside out.
  3. Pipelines end anywhere a function fits, even print_endline.

Keywords and builtins used here

The run, in numbers

Lines
14
Characters to type
415
Tokens
90
Three-star pace
65 tpm

At the three-star pace of 65 tokens a minute, this run takes about 83 seconds.

Type this snippet

Step 2 of 4 in Functions, step 5 of 29 in Language basics.

← Previous Next →

The pipeline in other languages