background preloader

JS Functional Programming

Facebook Twitter

Functional Programming Patterns: A Cookbook - freeCodeCamp.org - Medium. This article targets an audience that’s graduating from functional libraries like ramda to using Algebraic Data Types. We’re using the excellent crocks library for our ADTs and helpers, although these concepts may apply to other ones as well. We’ll be focusing on demonstrating practical applications and patterns without delving into a lot of theory.

Safely Executing Dangerous Functions Let’s say we have a situation where we want to use a function called darken from a third-party library. darken takes a multiplier, a color and returns a darker shade of that color. Pretty handy for our CSS needs. But it turns out that the function is not as innocent as it seems. darken throws errors when it receives unexpected arguments! This is, of course, very helpful for debugging — but we wouldn’t want our application to blow up just because we couldn’t derive a color. TryCatch executes the provided function within a try-catch block and returns a Sum Type called Result.

Runtime errors! Pretty neat! Double Dabble Ramda | The Only Winning Move. Recently at work I’ve switched from using Lodash to Ramda for functional programming extensions to Javascript/Coffeescript and I’m happier than I’ve pretty much ever been. Ramda isn’t the Javascript Holy Grail – that honor, I’m guessing, goes to PureScript – very much next in line on the list of things to learn – but it does help me write tight, readable code in a record amount of time. I’ve spent the last day cleaning up our database interface, and will move on to refactoring the API next. For fun, here’s a walkthrough of a RamdaJS rewrite of the doubledabble code I posted here a month ago. Warning: by double-dabble I mean a method of converting between binary and decimal encoding for numbers. I realize that a lot of people use it to mean a (highly similar) method for switching between decimal and binary coded decimal instead.

OK, so from last time, here’s the code to convert a binary number into its decimal equivalent Run this on "11110011" and you get 243, as you should. So, I like it. Request: add list comprehensions · Issue #147 · elm-lang/elm-compiler. Functional Programming for OOP Developers: Part 1 – Software, Fitness, and Gaming – Jesse Warden. Yeah yeah yeah, tl;dr; and show me the code, yo! I have been learning Functional Programming over the past year with a friend of mine. We’ve both cut our teeth on finding who to learn from, what articles are useful, and what actually translates into your day to day programming job. I’ve also learned a lot of natural problems that arise as you start a new project from scratch with an OOP background, or if you’re refactoring some OOP code to be more functional. In this article, I wanted to share some of what I learned. Specifically what functional programming is, why it matters, how to use pure functions and list comprehensions in the real world.

While I use JavaScript in this article, I’ve learned that the concepts and libraries supporting it are available in many programming languages. Here’s my list derived AFTER actually doing it vs. rehashing marketing I’ve read elsewhere. Code works more than it doesn’t.Unit tests are smaller, and easier to write.

Job Market Time & Cognitive Load Every. Ramda-examples.md. Ramda Examples Lift const madd3 = R.lift(R.curry(function (a, b, c) { // [1, 1, 1] // [1, 2, 1] // [1, 3, 1] // [2, 1, 1] // [2, 2, 1] // [2, 3, 1] // [3, 1, 1] // [3, 2, 1] // [3, 3, 1] console.log(arguments); return a + b + c; })); madd3([1, 2, 3], [1, 2, 3], [1]); //=> [3, 4, 5, 4, 5, 6, 5, 6, 7] composeK R.composeK(h, g, f) == R.compose(R.chain(h), R.chain(g), R.chain(f)) List Comprehension /* * Example: * Find any integer x, y, z that match the following conditions * 1. 0 < x < y < z < 10 * 2. x * x + y * y = z * z * In Python: * [(x,y,z) for x in range(1,10) for y in range(1,10) for z in range(1,10) if x < y and y < z and x * x + y * y == z * z] * >>> [(3, 4, 5)] * In Javascript with Ramda: */ var R = require('ramda'); R.pipe( R.sequence(R.of), R.filter(R.apply(function(x, y, z) { return x < y && y < z && (x * x + y * y == z * z); })) )([R.range(1, 10), R.range(1, 10), R.range(1, 10)]) // [[3, 4, 5]] Chain.

Ramda functions working with external functions · Issue #457 · ramda/ramda.

Prelude.ls

Lazy.js. Tech.Pro. Underscore.js. LambdaJS. Reactive Programming in JavaScript - Silk's Engineering Blog. Seanhess/fjs. Functional programming with JavaScript | Minko Gechev's blog. This article is about the functional concepts of JavaScript. Some of them are built-in the languages, others extra implemented but all of them are very common for purely functional languages like Haskell. First I want to tell what I mean with the term purely functional language. These languages are “safe”, they will not make side effect i.e. evaluating an expression won’t change something in the internal state and lead to different result of the same expression when called next time. It seems very strange and useless to “imperative” guys like me, but actually there’s a list of benefits: Concurrency. I hope these arguments are enough for you to look at the next sections. Anonymous functions It seems that the functional programming becomes more and more popular.

$(document).ready(function () { //do some stuff }); The function passed to $(document).ready is exactly an anonymous function. More about passing functions in the next section… High-order functions Imagine we have this case: Closures. Azkaban | LinkedIn Data Team. A batch job scheduler can be seen as a combination of the cron and make Unix utilities combined with a friendly UI. Batch jobs need to be scheduled to run periodically. They also typically have intricate dependency chains—for example, dependencies on various data extraction processes or previous steps. Larger processes might have 50 or 60 steps, of which some might run in parallel and others must wait for the output of earlier steps.

Combining all these processes into a single program allows you to control the dependency management, but can lead to sprawling monolithic programs that are difficult to test or maintain. Simply scheduling the individual pieces to run at different times avoids the monolithic problem, but introduces many timing assumptions that are inevitably broken. A good batch workflow system allows a program to be built out of small reusable pieces that need not know about one another. Why was it made? State of the project. Functional Javascript. Var Functional; Functional is the namespace for higher-order functions. Functional.install = function(except) This function copies all the public functions in Functional except itself into the global namespace. If the optional argument is present, functions named by its property names are not copied.

Higher-order functions Functional.compose = function(fn...) Type: (a2 → a1) (a3 -> a2)… (a… -> an) -> a… -> a1 Returns a function that applies the last argument of this function to its input, and the penultimate argument to the result of the application, and so on. (1, 2, 3…, )() =def 1(2(3(…((…))))) compose('1+', '2*')(2)→ 5 Functional.sequence = function(fn...) Type: (a… → a1) (a1 -> a2) (a2 -> a3)… (an-1 -> an) -> a… -> an Same as compose, except applies the functions in argument-list order. (1, 2, 3…, )(…) =def (…(3(2(1(…))))) sequence('1+', '2*')(2)→ 6 Functional.map = function(fn, sequence, object) Type: (a ix → boolean) [a] -> [a] Applies fn to each element of sequence. map('1+', [1,2,3])→ [2, 3, 4]

DrBoolean (Brian)