Project

General

Profile

Actions

Feature #17353

open

Functional chaining operator

Added by fulcanelly (Maks Kompanienko) over 3 years ago. Updated over 3 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:101140]

Description

Since ruby already moving in that direction(functional), I would like to propose add to it OCaml-like chaining/pipe operator into ruby.
Which would allow such syntax

def handle(requests) = requests
  |> Array.filter { not _1.from.user.banned? }
  |> Array.map { _1 |> main_router.emit }
  |> Array.each &awaiter

What exactly happens here ?

Let's look at a bit easier example:

gets.to_i
|> make_stuff
|> format "the number is %d"
|> puts

Which is expands exactly to the code below

puts(format("the number is %d", make_stuff(gets.to_i)))

So what this operator does is nothing but just tricky form of AST building

Advantages:

  • Increase readability
  • It's more duck type-ish

Limitations:

  • cant be overloaded

Related issues 2 (1 open1 closed)

Related to Ruby master - Feature #15799: pipeline operatorClosedActions
Related to Ruby master - Feature #16794: Rightward operatorsOpenActions
Actions

Also available in: Atom PDF

Like1
Like0Like0Like0Like0