Skip to content

Instantly share code, notes, and snippets.

@kellysutton
Created July 25, 2015 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellysutton/c4a98dc1f1d9d9d43bce to your computer and use it in GitHub Desktop.
Save kellysutton/c4a98dc1f1d9d9d43bce to your computer and use it in GitHub Desktop.
FP in JS
requirejs.config({
paths: {
ramda: 'https://cdnjs.cloudflare.com/ajax/libs/ramda/0.13.0/ramda.min'
}
});
require(['ramda'], function (_) {
const trace = _.curry(function(tag, x) {
console.log(tag, x);
return x;
});
const multiply = _.curry(function(a, b) {
return b * a;
});
const multiplyBy255 = multiply(255);
const floatToColorInt = _.compose(Math.floor, multiplyBy255);
const intToHexString = _.curry(function(rad, i) {
return i.toString(rad);
})(16);
const padWithZero = function(h) {
return h = h.length === 1 ? "0" + h : h;
};
const intToPaddedHexString = _.compose(padWithZero, intToHexString);
const floatToHex = _.compose(intToPaddedHexString, floatToColorInt);
const join = _.curry(function(s, a) {
return a.join(s);
});
const joinWithSpace = join("");
const addHash = function(s) {
return "#" + s;
};
const hexArrayToString = _.compose(addHash, joinWithSpace);
const floatArrayToHexString = _.compose(hexArrayToString, _.map(floatToHex));
console.log(floatArrayToHexString([0.984, 0.352, 0.619]));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment