Created
July 3, 2014 00:00
-
-
Save aaronpowell/d5ffaf78666f2b8fb033 to your computer and use it in GitHub Desktop.
Implementing the F# pipe forward (|>) operator using Sweet.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var add = function (x, y) { | |
return x + y; | |
}; | |
var log = console.log; | |
var add1 = add.length <= 1 ? add(1) : add.bind(null, 1); | |
var three = add1.length <= 1 ? add1(2) : add1.bind(null, 2); | |
var four = add.length <= [2].length + 1 ? add(2, 2) : add.bind(null, 2, 2); | |
log(three); | |
log(four); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macro (|>) { | |
case infix { $val | _ $fn($args (,) ...) } => { | |
return #{ | |
($fn.length <= [$args (,) ...].length + 1 ? $fn($args (,) ..., $val) : $fn.bind(null, $args (,) ..., $val)) | |
} | |
} | |
case infix { $val | _ $fn } => { | |
return #{ | |
($fn.length <= 1 ? $fn($val) : $fn.bind(null, $val)) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var add = function (x, y) { | |
return x + y; | |
}; | |
var log = console.log; | |
var add1 = 1 |> add | |
var three = 2 |> add1 | |
var four = 2 |> add(2) | |
log(three); | |
log(four); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment