Functional Programming in JavaScript by Unknown

Functional Programming in JavaScript by Unknown

Author:Unknown
Language: eng
Format: epub, pdf
Publisher: Packt Publishing


First we need to make the curried and partial-applied functions, then we can compose them to our other composed functions.

var lighterColors = lighterColor .compose(nums2hex.curry()); var darkerRed = darkerColor .compose(nums2hex.partialApply(255)); Var lighterRgb2hex = lighterColor .compose(nums2hex.partialApply()); console.log( lighterColors(123, 0, 22) ); // Returns: 8cff11 console.log( darkerRed(123, 0) ); // Returns: ee6a00 console.log( lighterRgb2hex(123,200,100) ); // Returns: 8cd975

There we have it! The functions read really well and make a lot of sense. We were forced to begin with little functions that just did one thing. Then we were able to put together functions with more utility.

Let's look at one last example. Here's a function that lightens an RBG value by a variable amount. Then we can use composition to create new functions from it.

// lighterColorNumSteps :: string -> num -> string function lighterColorNumSteps(color, n) { for (var i = 0; i < n; i++) { color = lighterColor(color); } return color; } // now we can create functions like this: var lighterRedNumSteps = lighterColorNumSteps.curry().compose(reds)(0,0); // and use them like this: console.log( lighterRedNumSteps(5) ); // Return: 'ff5555' console.log( lighterRedNumSteps(2) ); // Return: 'ff2222'



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.