Last active
March 19, 2024 16:11
-
-
Save rrag/7a97c7dda1e986980293 to your computer and use it in GitHub Desktop.
Revisions
-
rrag revised this gist
Jan 12, 2016 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -86,8 +86,6 @@ Some more examples of side effects - logging - read/update state to some centralized cache #### Pure functions A function is pure if it returns the same output given the same input without any side effects. -
rrag revised this gist
Dec 4, 2015 . 1 changed file with 124 additions and 19 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ There are many tutorials and articles available online which explain functional programming. Examples show small functions, which are composed into others which again get composed. It is hard to imagine how it would all work, then come the analogies and then the math. While the math is necessary to understand it can be difficult to grasp initially. The analogies on the other hand, (at least for me) are not relatable. Some articles assume the reader knows the different terminologies of FP. Over all I felt it is not inviting to learn. This introduction is for those who have had a tough time understanding those analogies, taken the plunge to functional programming but still have not been able to swim. This is yet another tutorial on functional programming @@ -219,24 +219,6 @@ A function which either accepts other function(s) as argument(s) or returns a fu We have seen these in the examples above, and having a function as a first class citizen is key to making this possible. This is key to functional programming, it starts to shine when you have many granular functions and then composing them together to make higher order function. ## Dont think imperative, think functionnal Once you have choosen a language which provides these functional constructs, you got to write code @@ -367,10 +349,133 @@ averageHeight2(students); see it in the repl [here](http://bit.ly/1KTIzLh) #### Functor Before we go further on this topic, let us take a small detour and talk about error handling as we know it. In many languages, a function can return only a single value, most of these languages deal with error conditions by throwing an error / exception for undesired input or external factors. It is hard to accept it, but throwing error is not functional. It makes the program hard to reason about. Let us look at an example ```js var capitalizeFirst = function (string) { return string[0].toUpperCase() + string.slice(1); }; console.log(capitalizeFirst("hello")); // "Hello" console.log(capitalizeFirst(null)); // Error ``` every time something like this happens we take the hand to the forehead and slap ourselves for not thinking about it and then add a null check. ```js var capitalizeFirst = function (string) { return string !== null ? string[0].toUpperCase() + string.slice(1) : null; }; ``` and then you suddenly realize oh no, there is `undefined`, ```js var capitalizeFirst = function (string) { return string !== null && string !== undefined ? string[0].toUpperCase() + string.slice(1) : null; }; ``` and then you realize later when reading some blog that `undefined` can be defined like `window.undefined = "abc";` so the `string !== undefined` is checking against `"abc"` another facepalm of self, curse yourself and the stupid moron who defined `undefined`, then you do ```js var capitalizeFirst = function (string) { return string !== null && typeof string != "undefined" ? string[0].toUpperCase() + string.slice(1) : null; }; ``` now satisfied that you have tackled it all, you write another function ```js var append = R.curry(function (a, b) { return b.concat(a); }); var appendSpace = append(" "); var appendDoe = append("Doe"); var someDoe = R.compose(appendDoe, appendSpace); console.log(someDoe("John")); // John Doe console.log(someDoe(null)); // Error ``` and then realize about the `null` and `undefined` checks for both its input arguments. (another facepalm) Before you know it, your code has these checks all across, with one of the 3 different version above. An alternative solution is to use a container, ```js var Maybe = function(x) { this.__value = x; } Maybe.of = function(x) { return new Maybe(x); } Maybe.prototype.isNothing = function() { return (this.__value === null || typeof this.__value == "undefined"); } Maybe.prototype.map = function(f) { return this.isNothing() ? Maybe.of(null) : Maybe.of(f(this.__value)); } ``` and use it as ```js Maybe.of("John") .map(appendSpace) .map(appendDoe); // Maybe("John Doe") Maybe.of("John") .map(appendSpace) .map(appendDoe); // Maybe(null) no error this time ``` Once the `Maybe` evaluates to `null`, it stops further execution and does not throw an error. Array as a functor ```js var add3 = function(a) { return a + 3; }; add3(10); // 13 // add3 is a function add3([1, 2, 3]); // "1,2,33" function add3 is not capable of handling both arrays and single items [1, 2, 3].map(add3); // [4, 5, 6] ``` simply speaking it is a type you can map a function over. #### Monad #### Monoid ## Conclusion A program written following the above rules is considered functional, it is hard to not mutate data in-place and even harder to move away from `for` loop. Just hang in there. The results of that transition are simply beautiful, Pure functions are just a breeze to test. #### References & other reading material - http://www.smashingmagazine.com/2014/07/dont-be-scared-of-functional-programming/ - http://drboolean.gitbooks.io/mostly-adequate-guide/content/index.html - https://www.youtube.com/watch?v=AvgwKjTPMmM -
Ragu Ramaswamy revised this gist
Nov 19, 2015 . 1 changed file with 12 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,6 @@ There are many tutorials and articles available online which explain functional programming, with analogies and math. While the math is necessary to understand it can be difficult to grasp initially. The analogies on the other hand, (at least for me) are not relatable. Some articles assume the reader knows the different terminologies of FP. Over all I felt it is not inviting to learn. This introduction is for those who have had a tough time understanding those analogies, taken the plunge to functional programming but still have not been able to swim. This is yet another tutorial on functional programming ## Terminology @@ -21,12 +19,12 @@ This has some really cool implications var head = function(array) { return array[0]; } var first = head; console.log(first([10, 20, 30, 40])); // 10 ``` `first` and `head` are referencing the same function 2. A function can be passed as a method argument - Just like a primitive or string or object can be passed as a method argument. Some of the best examples of this are @@ -44,13 +42,13 @@ This has some really cool implications // do stuff with value console.log(value); }; $("input").onchange(doStuff); ``` ##### nodejs ```js var fs = require('fs'); fs.readFile("/path/to/file", function(err, data) { if (err) throw err; console.log(data); @@ -64,9 +62,9 @@ This has some really cool implications return Math.floor(Math.random() * faces); } } var roll = dice(6); roll(); // returns a random number between 0 and 5 (both inclusive) roll(); // returns a different random number between 0 and 5 (both inclusive) ``` @@ -217,7 +215,7 @@ storeUser(user); #### Higher order functions A function which either accepts other function(s) as argument(s) or returns a function is a higher order function. We have seen these in the examples above, and having a function as a first class citizen is key to making this possible. This is key to functional programming, it starts to shine when you have many granular functions and then composing them together to make higher order function. @@ -241,7 +239,7 @@ add3([1, 2, 3]); // "1,2,33" function add3 is not capable of handling both array ``` ## Dont think imperative, think functionnal Once you have choosen a language which provides these functional constructs, you got to write code Let us see a few cases of how you can tell apart the two different styles #### for ... loop - bleahhhh @@ -293,7 +291,7 @@ var student = ...; student.grade = 3; student.GPA = 4; ``` use ES6 Object assign or a polyfill ```js var student = ...; @@ -376,4 +374,3 @@ A program written following the above rules is considered functional, it is hard - http://www.smashingmagazine.com/2014/07/dont-be-scared-of-functional-programming/ - http://drboolean.gitbooks.io/mostly-adequate-guide/content/index.html -
rrag revised this gist
Aug 6, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ There are many tutorials and articles available which explain functional programming, with analogies and very simple examples. It is quite hard to understand some of the terminologies, even harder to imagine how to write large apps. This introduction is for those who have had a tough time understanding those analogies, taken the plunge to functional programming but still have not been able to swim. This is yet another tutorial on functional programming -
rrag revised this gist
Aug 6, 2015 . 1 changed file with 4 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -76,8 +76,7 @@ This has some really cool implications In functional programming, a function should not have side effects. Let me get this out of the way, writing code with side effects is part of how I (and probably most programmers) have written code all along. We learn that *Side effects are bad*, in functional programming, Wikipedia states Side effects as > In computer science, a function or expression is said to have a side effect if, in addition to returning a value, it also modifies some state or has an observable interaction with calling functions or the outside world. For example, a particular function might modify a global variable or static variable, modify one of its arguments, raise an exception, write data to a display or file, read data, or call other side-effecting functions. In the presence of side effects, a program's behavior may depend on history; that is, the order of evaluation matters. Understanding and debugging a function with side effects requires knowledge about the context and its possible histories. @@ -137,7 +136,9 @@ given the same `DB` and `user` as input the output is always the same. It is a f Pure functions are easy to test, using different mock `DB` to test different cases is quite easy. Now one might argue this means a long number of arguments, and we are going away from encapsulation, why would a program calling `insert` need to know about `DB`? Enter Currying #### Currying @@ -376,5 +377,3 @@ A program written following the above rules is considered functional, it is hard - http://www.smashingmagazine.com/2014/07/dont-be-scared-of-functional-programming/ - http://drboolean.gitbooks.io/mostly-adequate-guide/content/index.html -
rrag revised this gist
Aug 6, 2015 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ There are many tutorials and articles available which explain functional programming, with analogies and very simple examples. It is quite hard to understand some of the terminologies, even harder to imagine how to write large apps. This introduction is for those who have have a tough time understanding those analogies, taken the plunge to functional programming but still have not been able to swim. @@ -375,3 +375,6 @@ A program written following the above rules is considered functional, it is hard - http://www.smashingmagazine.com/2014/07/dont-be-scared-of-functional-programming/ - http://drboolean.gitbooks.io/mostly-adequate-guide/content/index.html -
rrag revised this gist
Aug 6, 2015 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,8 @@ There are many tutorials and articles available which explain functional programming, with analogies and very simple examples. It is quite hard to understand some of the terminologies, even harder imagine how to write large apps. This introduction is for those who have have a tough time understanding those analogies, taken the plunge to functional programming but still have not been able to swim. This is yet another tutorial on functional programming ## Terminology -
rrag revised this gist
Aug 6, 2015 . 1 changed file with 9 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -359,11 +359,19 @@ Notice how the `averageHeight` function is composed out of smaller functions, it See the above example in [Ramda repl](http://bit.ly/1KTGLBO) a better version of the above would be ```js var averageHeight2 = R.compose(R.mean, R.map(R.prop('height')), R.filter(R.propEq('grade', 3))); averageHeight2(students); ``` see it in the repl [here](http://bit.ly/1KTIzLh) ## Conclusion A program written following the above rules is considered functional, it is hard to not mutate data in-place and even harder to move away from `for` loop. Just hang in there. The results of that transition are simply beautiful, Pure functions are just a breeze to test. #### References & other reading material - http://www.smashingmagazine.com/2014/07/dont-be-scared-of-functional-programming/ - http://drboolean.gitbooks.io/mostly-adequate-guide/content/index.html -
rrag revised this gist
Aug 6, 2015 . 1 changed file with 47 additions and 13 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,6 @@ I started learning javascript with jQuery, then node.js, d3, underscore, ES5 array methods like `map`, `reduce`, `filter`. I never got around to understanding the terminology used and what they mean. how to apply them effectively. I have read many of the tutorials online and still left wondering how do I write large apps This introduction is for those who like me have taken the plunge to functional programming but still have not been able to swim @@ -222,10 +220,6 @@ A function which either accepts other function(s) as argument(s) or returns a fu We have seen these in the examples above, and having a function as a first class citizen is key to making this possible. This is key to functional programming, it starts to shine when you have many granular functions and then composing them together to make higher order function. #### Functor A Functor is a function which unwraps the input and calls a function with that, then wraps the output of the function and returns it @@ -244,8 +238,6 @@ add3([1, 2, 3]); // "1,2,33" function add3 is not capable of handling both array // map is a functor ``` ## Dont think imperative, think functionnal Once you have choosen a language which provides these functional constructs, you got to write code @@ -323,13 +315,55 @@ All functions accept one or more input arguments and return a value, either a fu When working with api outside your control there is not much you can do, a good example I came across recently is the `canvas` api, for cases like that you will have to take exception to the functional style and go back to imperative ## Terminology Contd... #### Pointfree I intentionally did not talk about *Pointfree* earlier since it takes a little getting used to functional style. Look at the example from above ```js var students = [ { name: "A", grade: 3, height: 50 }, { name: "B", grade: 2, height: 40 }, { name: "C", grade: 3, height: 60 }, { name: "D", grade: 4, height: 55 }, { name: "E", grade: 5, height: 55 }, { name: "F", grade: 6, height: 45 }, { name: "G", grade: 2, height: 66 }, ]; ``` ```js // functional var heightOfStudentsInGrade3 = students .filter(function(each) {return each.grade === 3;}); .map(function(each) {return each.height;}); // returns an array of height var averageHeight = heightOfStudentsInGrade3 .reduce(function(h1, h2) {return h1 + h2}) / heightOfStudentsInGrade3.length; ``` You see it all starts with the array `students` everything is operated on `data`. Let us convert this function above to pointfree style ```js var isThirdGrade = function(s) { return s.grade === 3 }; var getHeight = function(s) { return s.height }; var averageHeight = R.compose(R.mean, R.map(getHeight), R.filter(isThirdGrade)); averageHeight(students); // 55 ``` Let me introduce a beautiful library [Ramda](http://ramdajs.com/0.17/index.html) the `R` above is from Ramda Notice how the `averageHeight` function is composed out of smaller functions, it is all evaluated right to left. Functions where the `data` is provided at the end and not used in its definition are called Pointfree See the above example in [Ramda repl](http://bit.ly/1KTGLBO) ## Conclusion A program written following the above rules is considered functional, it is hard to not mutate data in-place and even harder to move away from `for` loop. Just hang in there. The results of that transition are simply beautiful, Pure functions are just a breeze to test. #### References & other reading material - http://www.smashingmagazine.com/2014/07/dont-be-scared-of-functional-programming/ - http://drboolean.gitbooks.io/mostly-adequate-guide/content/index.html -
rrag revised this gist
Aug 5, 2015 . 1 changed file with 48 additions and 49 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -19,60 +19,59 @@ Wikipedia states This has some really cool implications 1. A function can be assigned to a variable - Just like a primitive or string or object can be assigned to a variable ```js var head = function(array) { return array[0]; } var first = head; console.log(first([10, 20, 30, 40])); // 10 ``` `first` and `head` are referencing the same function 2. A function can be passed as a method argument - Just like a primitive or string or object can be passed as a method argument. Some of the best examples of this are ##### jQuery ```js $("input").onchange(function(value) { // do stuff with value console.log(value); }); ``` that can also be written as ```js var doStuff = function(value) { // do stuff with value console.log(value); }; $("input").onchange(doStuff); ``` ##### nodejs ```js var fs = require('fs'); fs.readFile("/path/to/file", function(err, data) { if (err) throw err; console.log(data); }) ``` 3. A function can return another function - Just like a primitive or string or object can be returned from a function ```js var dice = function (faces) { return function() { return Math.floor(Math.random() * faces); } } var roll = dice(6); roll(); // returns a random number between 0 and 5 (both inclusive) roll(); // returns a different random number between 0 and 5 (both inclusive) ``` #### Side effects -
rrag revised this gist
Aug 5, 2015 . 1 changed file with 40 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -204,9 +204,49 @@ You can partially apply some arguments to a curried function and use the returne lodash has a [curry](https://lodash.com/docs#curry) implementation A means of achieving partial application is using the `bind` operator in javascript ```js function store(DB, user) { var newUser = DB.store(user); return newUser; } var storeUser = store.bind(null, DB); storeUser(user); ``` #### Higher order functions A function which either accepts other function(s) as argument(s) or returns a function is a higher order function. We have seen these in the examples above, and having a function as a first class citizen is key to making this possible. This is key to functional programming, it starts to shine when you have many granular functions and then composing them together to make higher order function. #### Pointfree #### Functor A Functor is a function which unwraps the input and calls a function with that, then wraps the output of the function and returns it ```js var add3 = function(a) { return a + 3; } add3(10); // 13 // add3 is a function add3([1, 2, 3]); // "1,2,33" function add3 is not capable of handling both arrays and single items [1, 2, 3].map(add3); // [4, 5, 6] // map is a functor ``` ## Dont think imperative, think functionnal Once you have choosen a language which provides these functional constructs, you got to write code -
rrag revised this gist
Aug 5, 2015 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -285,10 +285,12 @@ All functions accept one or more input arguments and return a value, either a fu When working with api outside your control there is not much you can do, a good example I came across recently is the `canvas` api, for cases like that you will have to take exception to the functional style and go back to imperative ### Conclusion A program written following the above rules is considered functional, it is hard to not mutate data in-place and even harder to move away from `for` loop. Just hang in there. The results of that transition are simply beautiful, Pure functions are just a breeze to test. #### References & other reading material - http://www.smashingmagazine.com/2014/07/dont-be-scared-of-functional-programming/ - http://drboolean.gitbooks.io/mostly-adequate-guide/content/index.html -
rrag revised this gist
Aug 5, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -291,4 +291,4 @@ A program written following the above rules is considered functional, it is hard - http://www.smashingmagazine.com/2014/07/dont-be-scared-of-functional-programming/ - http://drboolean.gitbooks.io/mostly-adequate-guide/content/index.html -
rrag revised this gist
Aug 5, 2015 . 1 changed file with 88 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -180,6 +180,10 @@ curriedAdd(1, 2, 3); // 6 var add3 = curriedAdd(1, 2); // returns a function add3(3); // 6 add3(1); // 4 var add2MoreNumbersWith10 = curriedAdd(10); // returns a function add2MoreNumbersWith10(1, 2); // 13 add2MoreNumbersWith10(1)(2); // 13 ``` Currying a function with `n` arguments will return a new function which can take less than `n` arguments and return a function which will be another curried function that can take the remaining arguments. @@ -196,13 +200,95 @@ var storeUser = curriedStore(DB); storeUser(user); ``` You can partially apply some arguments to a curried function and use the returned function like it accepts only the remaining arguments. lodash has a [curry](https://lodash.com/docs#curry) implementation #### Pointfree #### Functor ## Dont think imperative, think functionnal Once you have choosen a language which provides these functional constructs, you got to write code Let us see a few cases of how you can tell apart the two different styles #### for ... loop - bleahhhh Say you want to find, given an array of student objects, find the average height of all the students who are in grade 3 ```js // imperative var students = [...]; // array of student objects var sum = 0, numberOfGrade3Students = 0; for (var i = 0; i < students.length; i++) { var student = students[i]; if (student.grade === 3) { sum += student.height; numberOfGrade3Students++; } } var averageHeight = sum / numberOfGrade3Students; ``` There are different ways to achieve this functional style, I will explain my favourite here ```js // functional var students = [...]; // array of student objects var heightOfStudentsInGrade3 = students .filter(function(each) {return each.grade === 3;}); .map(function(each) {return each.height;}); // returns an array of height var averageHeight = heightOfStudentsInGrade3 .reduce(function(h1, h2) {return h1 + h2}) / heightOfStudentsInGrade3.length; ``` recursion is another way to handle this without using for loop, but I am not going to there So for starters DO NOT USE FOR LOOP, that is not functional. #### Immutable data Remember functional programs do not have side effects, mutating an input or any data element is a side effect. treat all variables as constants and do not mutate them. objects too, do not mutate them. so instead of ```js var student = ...; student.grade = 3; student.GPA = 4; ``` use ES6 Object assign or a polyfill ```js var student = ...; var modifiedStudent = Object.assign({}, student, {grade: 3, GPA, 4}); ``` or use ES7 spread operator ```js var student = ...; var modifiedStudent = { ...student, {grade: 3, GPA, 4} }; ``` #### All functions take and return All functions accept one or more input arguments and return a value, either a function or a value #### when to not use functional style When working with api outside your control there is not much you can do, a good example I came across recently is the `canvas` api, for cases like that you will have to take exception to the functional style and go back to imperative ### Conclusion A program written following the above rules is considered functional, it is hard to not mutate data in-place and even harder to move away from `for` loop. The results of that transition are simply beautiful, Pure functions are just a breeze to test. #### References & other reading material - http://www.smashingmagazine.com/2014/07/dont-be-scared-of-functional-programming/ - http://drboolean.gitbooks.io/mostly-adequate-guide/content/index.html - -
rrag revised this gist
Aug 5, 2015 . 1 changed file with 37 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ I started my carrer as a Rookie Java dev, I liked java. Object Oriented code was awesome, I thought it was solution to all the problems. Used to hate javascript. As time would have it I had to learn javascript out of necessity. I started with jQuery, then node.js, d3, underscore, ES5 array methods like `map`, `reduce`, `filter` etc. @@ -166,8 +166,43 @@ add10To(add5To(5)); // 20 the function returned by `addWith` takes an argument `b` but also has access to `a` which is an argument of its enclosing function. This function which has access to variables and arguments of its enclosing function is a closure Let us start currying with an example ```js function add(a, b, c) { return a + b + c; } add(1, 2, 3); // 6 add(1, 2); // NaN - this is because c is undefined and when it participates in an arrithmetic operation it results in Not a Number var curriedAdd = _.curry(add); curriedAdd(1, 2, 3); // 6 var add3 = curriedAdd(1, 2); // returns a function add3(3); // 6 add3(1); // 4 ``` Currying a function with `n` arguments will return a new function which can take less than `n` arguments and return a function which will be another curried function that can take the remaining arguments. Another example ```js function store(DB, user) { var newUser = DB.store(user); return newUser; } var curriedStore = _.curry(store); var storeUser = curriedStore(DB); storeUser(user); ``` You can partially apply some arguments to a curried function and use the returned function like it accepts only the necessary #### Pointfree #### Functor ## How to write code functional style - Dos and Donts -
rrag revised this gist
Aug 5, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -92,7 +92,7 @@ Some more examples of side effects - logging - read/update state to some centralized cache It is not forbidden to have side effects, they should be minimal. #### Pure functions A function is pure if it returns the same output given the same input without any side effects. @@ -103,7 +103,7 @@ function add(a, b) { return a + b; } ``` This ^ is a pure function, given the same input `a, b` the same output is returned without any side effects ```js function insert(user) { if (DB.exists(user.id)) { -
rrag revised this gist
Aug 5, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ I started my carrer as a Rookie Java dev, I liked java. Object Oriented code was awesome, thought it was solution to the many problems. Used to hate javascript. As time would have it I had to learn javascript out of necessity. I started with jQuery, then node.js, d3, underscore, ES5 array methods like `map`, `reduce`, `filter` etc. -
rrag revised this gist
Aug 4, 2015 . 1 changed file with 101 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,17 +1,21 @@ I grew up learning Java, I liked it. Object Oriented code was awesome. Used to hate javascript. As time would have it I had to learn javascript out of necessity. I started with jQuery, then node.js, d3, underscore, ES5 array methods like `map`, `reduce`, `filter` etc. This new style of writing functional code started to grow on me and writing code with these concepts became the new normal. But I never got around to understanding the terminology used and what they mean, until I spent the time to learn these. I realize I had not been following most of the principles and been writing bad code whilst thinking I was writing functional style. This introduction is for those who like me have taken the plunge to functional programming but still have not been able to swim ## Terminology #### Functions as first class citizens Functions are first class means they are just like anyone else, or rather they are not special, they behave the same as say primitives or strings or objects. Wikipedia states > "first-class" is a computer science term that describes programming language entities that have no restriction on their use (thus first-class functions can appear anywhere in the program that other first-class entities like numbers can, including as arguments to other functions and as their return values). This has some really cool implications 1. A function can be assigned to a variable - Just like a primitive or string or object can be assigned to a variable @@ -55,28 +59,113 @@ fs.readFile("/path/to/file", function(err, data) { }) ``` 3. A function can return another function - Just like a primitive or string or object can be returned from a function ```js var dice = function (faces) { return function() { return Math.floor(Math.random() * faces); } } var roll = dice(6); roll(); // returns a random number between 0 and 5 (both inclusive) roll(); // returns a different random number between 0 and 5 (both inclusive) ``` #### Side effects In functional programming, a function should not have side effects. Let me get this out of the way, writing code with side effects is part of how I (and probably most programmers) have written code all along. We learn that *Side effects are bad*, and functional programming, Wikipedia states > In computer science, a function or expression is said to have a side effect if, in addition to returning a value, it also modifies some state or has an observable interaction with calling functions or the outside world. For example, a particular function might modify a global variable or static variable, modify one of its arguments, raise an exception, write data to a display or file, read data, or call other side-effecting functions. In the presence of side effects, a program's behavior may depend on history; that is, the order of evaluation matters. Understanding and debugging a function with side effects requires knowledge about the context and its possible histories. Some more examples of side effects - Insert/update/read from a database - read/write a file - mutations (read as `i++` or `i = i + j` or `str = str.concat(" world")` - logging - read/update state to some centralized cache One might wonder then how am I supposed to store to a file or read/write from a database. Those will be side effects. It is not forbidden to use them but rather be limited in their use #### Pure functions A function is pure if it returns the same output given the same input without any side effects. some examples ```js function add(a, b) { return a + b; } ``` This ^ is a pure function, given the same input `a, b` the same output is retrieved without any side effects ```js function insert(user) { if (DB.exists(user.id)) { throw Error("users exists"); } var id = DB.insert(user); user.id = id; return user; } ``` There are multiple side effects going on ^ here. 1. `user` is mutated with `user.id = id` so the input is changed 2. Depending on the state of the DB the error `"user exists"` might be thrown 3. in addition to returning the user the DB is also updated. Testing this function is quite complex as the state of `DB` has to be set up and verified after the execution. In cases of multiple instances of this code running, it is impossible to assert the after state of `DB` so this ^ function is certainly not pure. Converting that to pure would be ```js function insert(DB, user) { return function() { throwIfUserExists(DB, user); var savedUser = saveUser(DB, user); return savedUser; } } ``` given the same `DB` and `user` as input the output is always the same. It is a function. Pure functions are easy to test, using different mock `DB` to test different cases is quite easy. Now one might argue this means a long number of arguments, and we are going away from encapsulation, why would a program calling `insert` need to know about `DB`. Enter Currying #### Currying Before we talk about Currying you need to understand what a closure is ```js var addWith = function(a) { return function(b) { return a + b; } } var add5To = addWith(5); add5To(5); // 10 add5To(10); // 15 add5To(0); // 5 var add10To = addWith(10); add10To(add5To(5)); // 20 ``` the function returned by `addWith` takes an argument `b` but also has access to `a` which is an argument of its enclosing function. This function which has access to variables and arguments of its enclosing function is a closure #### Pointfree -
rrag revised this gist
Aug 4, 2015 . 2 changed files with 84 additions and 22 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,84 @@ I grew up learning Java, I liked it. Object Oriented code was awesome. Used to hate javascript. As time would have it I had to learn javascript out of necessity. I started with jQuery, then node.js, d3, underscore, ES5 methods like `map`, `reduce`, `filter` etc. This new style of writing functional code started to grow on me and writing code in javascript became the new normal. But I never got around to understanding the terminology used and what they mean until I spent the time to learn these. I realize I had not been following most of the principles and writing bad code whilst thinking I was writing functional style. This introduction is for those who like me have taken the plunge to functional programming but still have not been able to swim ## Terminology #### First class functions Functions are first class means they are just like anyone else, or rather they are not special, they behave the same as say primitives or strings or objects. This has some really cool implications 1. A function can be assigned to a variable - Just like a primitive or string or object can be assigned to a variable ```js var head = function(array) { return array[0]; } var first = head; console.log(first([10, 20, 30, 40])); // 10 ``` `first` and `head` are referencing the same function 2. A function can be passed as a method argument - Just like a primitive or string or object can be passed as a method argument. Some of the best examples of this are ##### jQuery ```js $("input").onchange(function(value) { // do stuff with value console.log(value); }); ``` that can also be written as ```js var doStuff = function(value) { // do stuff with value console.log(value); }; $("input").onchange(doStuff); ``` ##### nodejs ```js var fs = require('fs'); fs.readFile("/path/to/file", function(err, data) { if (err) throw err; console.log(data); }) ``` #### Side effects In functinoal programming, a function should not have side effects. Let me get this out of the way, writing code with side effects is part of how I (and probably most programmers) have written code all along. Some examples of side effects - Insert/update a database - writing a file - mutations (read as `i++` or `i = i + j` or `str = str.concat(" world")` - logging - read from file - update state to cache In short any progr #### Pure functions A function is pure if it returns the same output given the same input without any side effects #### Currying #### Pointfree ## How to write code functional style - Dos and Donts This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,22 +0,0 @@ -
rrag created this gist
Aug 4, 2015 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ I grew up learning Java, I liked it. Object Oriented code was awesome. Used to hate javascript. But as time would have it I had to learn javascript out of necessity. I started with jQuery, then node.js, d3, underscore, this new style of writing functional code started to grow on me and writing code in javascript became the new normal. But I never got around to understanding the terminology used and what they mean, Finally getting it up now First class functions Functions are first class means they are just like anyone else, or rather they are not special, they behave the same as say primitives or strings or objects. A function can be assigned to a variable - Just like a primitive or string or object can be assigned to a variable e.g. // Javascript var head = function(array) { return array[0]; } A function can be passed as a method argument - Just like a primitive or string or object can be passed as a method argument A function can be the return of another function - Just like a primitive or string or object can be returned Higher order functions - Pure functions - Currying Pointfree