Skip to content

Instantly share code, notes, and snippets.

@iDoMeteor
Forked from burakcan/forward_backward.js
Created July 2, 2016 04:22
Show Gist options
  • Save iDoMeteor/971c63aac456cd2e3bb3cfbabd20ec7a to your computer and use it in GitHub Desktop.
Save iDoMeteor/971c63aac456cd2e3bb3cfbabd20ec7a to your computer and use it in GitHub Desktop.
Basic F# forward / backward operators in JS
/* jshint esnext: true */
const FORWARD = '|>';
const BACKWARD = '<|';
function _(...args) {
return function(value) {
return args.reduce((val, curr, index) => {
switch(curr) {
case FORWARD:
return args[index + 1](val);
case BACKWARD:
return val(args[index + 1]);
default:
return val;
}
}, value);
};
}
const pipeline = _(
'|>', map(_(
'|>', add, '<|', 1
)),
'|>', concat, '<|', ['a', 'b', 'c'],
'|>', join,
'|>', remove, '<|', (/,/g)
);
const otherPipeline = _(
'|>', split, '<|', ''
);
const result = pipeline([1, 2, 3]);
const otherResult = otherPipeline(result);
console.log(result);
console.log(otherResult);
function add(num) {
return function(val) {
return val + num;
};
}
function map(fn) {
return function(arr) {
return arr.map(fn);
};
}
function join(arr) {
return arr.join();
}
function split(str) {
return function(val) {
return str.split(val);
};
}
function concat(one) {
return function(two) {
return one.concat(two);
};
}
function remove(str) {
return function(reg) {
return str.replace(reg, '');
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment