Created
October 15, 2018 06:22
-
-
Save prameshbhattarai/16a2ee9a7b82fd64328c79340ed3e30e to your computer and use it in GitHub Desktop.
high order function in javascript
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 characters
| /* | |
| * Chaining of map() and reduce() function | |
| */ | |
| (function() { | |
| "use strict" | |
| const object = [{id: 1, name: "normal object1"}, {id: 2, name: "normal object2"}, {id: 3, name: "normal object3"}] | |
| const providers = []; | |
| object.map(mapToProviderObject) | |
| .reduce(pushFilteredProviders, providers); | |
| // .reduce((collect, each, index, array) => providers.push(each), providers); // can be use either way | |
| function mapToProviderObject(provider) { | |
| const providerObj = { | |
| providerId: provider.id, | |
| provider: "mapped => provider Object", | |
| name: " not - " +provider.name | |
| }; | |
| return providerObj; | |
| } | |
| function pushFilteredProviders(thisProviderObject, loopProviderObject) { | |
| // arguments => collectionObject, iteratingObject, index, array | |
| arguments[0].push(loopProviderObject); | |
| return arguments[0]; | |
| } | |
| console.log(providers); | |
| })(); | |
| /* | |
| * Get sum of double even numbers in the list | |
| */ | |
| (function() { | |
| "use strict" | |
| const list = [1, 2, 3, 4, 5, 6, 7, 8, 9]; | |
| const sumOfEves = list | |
| .filter(v => v % 2 === 0) // filter for even number | |
| .map(v => v * 2) // map to double of itself | |
| .reduce((sum, element, i, array) => sum + element, 0); // collect and make sum | |
| console.log("Sum of double of evens are " + sumOfEves); | |
| })(); | |
| /* | |
| * removing Object with duplicate properties in List | |
| */ | |
| (function() { | |
| "use strict" | |
| const list = [{id: 1}, {id: 2}, {id: 3}, {id: 3}, {id: 4}, {id: 5}, {id: 6}, {id: 7}, {id: 8}]; | |
| console.log("before"); | |
| console.log(list); | |
| function unlike(collection, key) { | |
| var items = {}; // temp array, for inserting flags for each item | |
| return collection.filter(each => { | |
| var found = items[each[key]]; // fetch from temp array | |
| items[each[key]] = true; // set flag for item inserted | |
| return !found; // if not found then insert into our collection | |
| }); | |
| } | |
| console.log("after unliking..."); | |
| console.log(unlike(list, 'id')); | |
| })(); | |
| /* | |
| * Find greatest number in provided param | |
| */ | |
| (function() { | |
| "use strict" | |
| var smallest, biggest; | |
| function whichIsGreatest() { | |
| var great = arguments[0]; | |
| [].forEach.call(arguments, function (el) { | |
| el > great ? great = el : ''; | |
| }); | |
| return great; | |
| }; | |
| function otherWay(...args) { | |
| return args.reduce((greatest, element, i, array) => element > greatest ? greatest = element : greatest, 0); | |
| } | |
| console.log(whichIsGreatest(100,2,3,4,5,6,7,8,9)); | |
| console.log(otherWay(100,2,3,4,5,6,7,8,9)); | |
| })(); | |
| /* | |
| * Non-Mutable javaScript objects | |
| */ | |
| (function() { | |
| "use strict" | |
| var mutableObject = { | |
| id: 1, | |
| identity: "mutable" | |
| }; | |
| console.log(mutableObject); | |
| console.log("try to change the object"); | |
| mutableObject = { | |
| id: 22, | |
| identity: "changed" | |
| }; | |
| console.log(mutableObject); | |
| const partiallyImmutable = { | |
| id: 2, | |
| identity: "partially Immutable" | |
| }; | |
| console.log(partiallyImmutable); | |
| console.log("try to change the object"); | |
| try{ | |
| partiallyImmutable = { | |
| id: 22, | |
| identity: "partially Immutable infected" | |
| }; | |
| }catch(error) { | |
| console.log("cannot be changed, its constant"); | |
| } | |
| console.log("lets try to change it from properties"); | |
| partiallyImmutable.id = 22; | |
| partiallyImmutable.identity = "partially Immutable infected"; | |
| console.log("it had been changed"); | |
| console.log(partiallyImmutable); | |
| var mutable = { | |
| id: 1, | |
| identity: "fully Immutable" | |
| }; | |
| const fullyImmutable = Object.freeze(mutable); | |
| try{ | |
| fullyImmutable.id = 22; | |
| fullyImmutable.identity = "fully Immutable infected"; | |
| }catch(error) { | |
| console.log("cannot be changed, fully Immutable object"); | |
| } | |
| })(); | |
| /* | |
| * Passing function as param | |
| */ | |
| (function() { | |
| "use strict" | |
| function double(el) { | |
| return el + el; | |
| }; | |
| function multiplyByItself(el) { | |
| return el * el; | |
| }; | |
| console.log("double it, then return the multiple of double"); | |
| var fn = double(5); // double it | |
| console.log(multiplyByItself(fn)); // then return the multiple of double | |
| console.log("multiple it, then return the double of multiple"); | |
| fn = multiplyByItself(5); // multiple it | |
| console.log(double(fn)); // then return the double of multiple | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment