Created
July 25, 2015 19:08
-
-
Save kellysutton/c4a98dc1f1d9d9d43bce to your computer and use it in GitHub Desktop.
FP in 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
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