Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The Elixir |> (pipeline / nose) operator. Makes functional programming much cleaner without brackets or intermediate variables. I wish more programming language has this. JavaScript comes close with chained calls of map.


Ocaml also has `@@` which gives `'a -> ('a -> 'b) -> 'b = <fun>` and `|>` for `('a -> 'b) -> 'a -> 'b = <fun>` so you can also do:

```ocaml

  type point = { x: float; y: float};;

  let euclidean a a' =
    let xd = (a'.x -. a.x) in
    let yd = (a'.y -. a.y) in
    sqrt ((xd*.xd) +. (yd*.yd))
  ;;

  let is_in_unit_circle x =
    x <= 1.

  let quadsolve iterations hits =
    ((4. *. hits) /. Float.of_int iterations)

  let estimate iters =
    let origin    = { x = 0.; y = 0. } in
     Array.init iters (fun _ -> { x = (Random.float 1.); y = (Random.float 1.) })
     |> Array.map (euclidean origin)
     |> Array.map is_in_unit_circle
     |> Array.fold_left (fun x y ->  if y then x +. 1. else x ) 0.
     |> quadsolve iters
;;

```

  utop # estimate 1000000;;
  - : float = 3.141432

Edit: formatting


Markdown fence blocks don't work. For monospace, indent 2 or more spaces.


This is common in the functional paradigm, although syntax and semantics varies in sometimes important ways among languages.

There's a long-standing TC39 proposal to add it to JS. It's interesting to read it to see examples of the aforementioned differences.

https://github.com/tc39/proposal-pipeline-operator


The Kickstarter iOS Swift code also used this, from what I remember. It was my first time ever seeing something like it, so I was also mindblown.


F# has this too (maybe had it first? I don't know) and it's wonderful. So does Elm.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: