Skip to content

Pipe Operator #4144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/grammar.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,9 @@ grammar =
# [The existential operator](http://jashkenas.github.com/coffee-script/#existence).
o 'Expression ?', -> new Existence $1

o 'Expression PIPE Value', -> new Call $3, [$1]
o 'Expression PIPE Invocation', -> $3.pipe $1

o 'Expression + Expression', -> new Op '+' , $1, $3
o 'Expression - Expression', -> new Op '-' , $1, $3

Expand Down Expand Up @@ -631,6 +634,7 @@ operators = [
['left', 'RELATION']
['left', 'COMPARE']
['left', 'LOGIC']
['left', 'PIPE']
['nonassoc', 'INDENT', 'OUTDENT']
['right', 'YIELD']
['right', '=', ':', 'COMPOUND_ASSIGN', 'RETURN', 'THROW', 'EXTENDS']
Expand Down
6 changes: 5 additions & 1 deletion src/lexer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ exports.Lexer = class Lexer
tag = 'TERMINATOR'
else if value in MATH then tag = 'MATH'
else if value in COMPARE then tag = 'COMPARE'
else if value in PIPE then tag = 'PIPE'
else if value in COMPOUND_ASSIGN then tag = 'COMPOUND_ASSIGN'
else if value in UNARY then tag = 'UNARY'
else if value in UNARY_MATH then tag = 'UNARY_MATH'
Expand Down Expand Up @@ -806,6 +807,7 @@ NUMBER = ///

OPERATOR = /// ^ (
?: [-=]> # function
| \|> # pipe
| [-+*/%<>&|^!?=]= # compound assign / compare
| >>>=? # zero-fill right shift
| ([-+:])\1 # doubles
Expand Down Expand Up @@ -868,7 +870,7 @@ POSSIBLY_DIVISION = /// ^ /=?\s ///
# Other regexes.
HERECOMMENT_ILLEGAL = /\*\//

LINE_CONTINUER = /// ^ \s* (?: , | \??\.(?![.\d]) | :: ) ///
LINE_CONTINUER = /// ^ \s* (?: , | \??\.(?![.\d]) | :: | \|> ) ///

INVALID_ESCAPE = ///
( (?:^|[^\\]) (?:\\\\)* ) # make sure the escape isn’t escaped
Expand Down Expand Up @@ -901,6 +903,8 @@ LOGIC = ['&&', '||', '&', '|', '^']
# Bit-shifting tokens.
SHIFT = ['<<', '>>', '>>>']

PIPE = ['|>']

# Comparison tokens.
COMPARE = ['==', '!=', '<', '>', '<=', '>=']

Expand Down
4 changes: 4 additions & 0 deletions src/nodes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,10 @@ exports.Call = class Call extends Base

children: ['variable', 'args']

pipe: (arg) ->
@args.unshift arg
this

# Tag this invocation as creating a new instance.
newInstance: ->
base = @variable?.base or @variable
Expand Down
39 changes: 39 additions & 0 deletions test/pipe.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

test "Pipe to function", ->

fn = (x) -> x * 100

eq 100, 1 |> fn
eq 300, 3 |> fn

test "Pipe to partialy filled function", ->

fn = (x,y) -> "#{x},#{y}"

eq '1,2', 1 |> fn(2)
eq '1,2', 1 |> fn 2

test "Pipe chain", ->

f = (x) -> x * 10
g = (x) -> x + 5

eq 15, 1 |> f |> g
eq 60, 1 |> g |> f

add = (x,y) -> x + y
mul = (x,y) -> x * y

eq 45, 4 |> mul(10) |> add 5
eq 70, 4 |> add(10) |> mul 5

test "Pipe on the next line", ->

fn = (x,y) -> "#{x},#{y}"

eq '1,2', 1
|> fn(2)

eq '1,2', 1
|> fn 2