Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Implementing the F# pipe forward (|>) operator using Sweet.js
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);
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))
}
}
}
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