Last active
January 27, 2021 14:13
-
-
Save toni783/22c083886e23f43bfed2f7174647a35c to your computer and use it in GitHub Desktop.
JS Bin - JavaScript: From Fundamentals to Functional JS, v2
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
| /*const suspects = ["Miss Scarlet", "Colonel Mustard", "Mr. White"];*/ | |
| // suspects.forEach(el => console.log(el)); | |
| /* | |
| _.each = function(list, callback) { | |
| if(Array.isArray(list)){ | |
| for(let i = 0; i < list.length; i++){ | |
| callback(list[i], i, list); | |
| } | |
| }else { | |
| for( key in list) { | |
| callback(list[key], key, list) | |
| } | |
| } | |
| } | |
| _.each(suspects, (el) => console.log(el)) | |
| */ | |
| /*const weapons = ['knife', 'gun', 'spear'];*/ | |
| /* | |
| const brokenWeapons = _.map(weapons, (val) => ` broken ${val} ` ) | |
| console.log(brokenWeapons) | |
| */ | |
| /*_.map = function(list, callback) { | |
| const callbackArray = [] | |
| _.each(list, (el, index, list) => { | |
| callbackArray.push(callback(el, index, list)) | |
| }) | |
| return callbackArray | |
| } | |
| const brokenWeapons = _.map(weapons, (val) => ` broken ${val} `) | |
| console.log(brokenWeapons)*/ | |
| 'use strict'; | |
| function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } } | |
| var videoData = [{ | |
| name: 'Miss Scarlet', | |
| present: true, | |
| rooms: [{ | |
| kitchen: false | |
| }, { | |
| ballroom: false | |
| }, { | |
| conservatory: false | |
| }, { | |
| 'dining room': false | |
| }, { | |
| 'billiard room': false | |
| }, { | |
| library: false | |
| }] | |
| }, { | |
| name: 'Mrs. White', | |
| present: false, | |
| rooms: [{ | |
| kitchen: false | |
| }, { | |
| ballroom: false | |
| }, { | |
| conservatory: false | |
| }, { | |
| 'dining room': false | |
| }, { | |
| 'billiard room': false | |
| }, { | |
| library: false | |
| }] | |
| }, { | |
| name: 'Reverend Green', | |
| present: true, | |
| rooms: [{ | |
| kitchen: false | |
| }, { | |
| ballroom: false | |
| }, { | |
| conservatory: false | |
| }, { | |
| 'dining room': false | |
| }, { | |
| 'billiard room': false | |
| }, { | |
| library: false | |
| }] | |
| }, { | |
| name: 'Rusty', | |
| present: false, | |
| rooms: [{ | |
| kitchen: false | |
| }, { | |
| ballroom: false | |
| }, { | |
| conservatory: false | |
| }, { | |
| 'dining room': false | |
| }, { | |
| 'billiard room': false | |
| }, { | |
| library: false | |
| }] | |
| }, { | |
| name: 'Colonel Mustard', | |
| present: true, | |
| rooms: [{ | |
| kitchen: false | |
| }, { | |
| ballroom: false | |
| }, { | |
| conservatory: false | |
| }, { | |
| 'dining room': false | |
| }, { | |
| 'billiard room': false | |
| }, { | |
| library: false | |
| }] | |
| }, { | |
| name: 'Professor Plum', | |
| present: true, | |
| rooms: [{ | |
| kitchen: false | |
| }, { | |
| ballroom: false | |
| }, { | |
| conservatory: false | |
| }, { | |
| 'dining room': false | |
| }, { | |
| 'billiard room': false | |
| }, { | |
| library: false | |
| }] | |
| }]; | |
| /*_.filter = function(arr, callback) { | |
| const elements = []; | |
| _.forEach(arr, (el, index, list) => { | |
| if (callback(el, index, list)) { | |
| elements.push(el) | |
| } | |
| }) | |
| return elements | |
| } | |
| const newSuspects = _.filter(videoData, (el) => el.present ) | |
| console.log(newSuspects) | |
| */ | |
| /* | |
| const mySuspects = _.filter(videoData, el => el.present ) | |
| const suspectsNames = _.map(mySuspects, el => el.name) | |
| console.log(suspectsNames) | |
| */ | |
| /* | |
| _.from = function (obj) { | |
| const toArray = [] | |
| for(el in obj) { | |
| toArray.push(el) | |
| } | |
| return toArray | |
| } | |
| _.from = function (obj) { | |
| return Array.prototype.slice.call(obj) | |
| } | |
| const myNewArray = _.from('foo') | |
| console.log(myNewArray) | |
| */ | |
| /* | |
| _.reduce = (iterable, func, accumulator) => { | |
| if (Array.isArray(iterable)) { | |
| iterable.forEach((value, index, collection) => { | |
| if (accumulator === undefined && index === 0) { | |
| accumulator = iterable[0] | |
| } else { | |
| accumulator = func(accumulator, value, index, collection) | |
| } | |
| }) | |
| return accumulator | |
| } else { | |
| for (const value in iterable) { | |
| if (accumulator === undefined) { | |
| accumulator = iterable[value] | |
| } else { | |
| accumulator = func(accumulator, iterable[value], value, iterable) | |
| } | |
| } | |
| return accumulator | |
| } | |
| } | |
| const reduceNumber = _.reduce([1, 2, 3], function(sum, n) { | |
| return sum + n; | |
| }, 3); | |
| const reduceObject = _.reduce({ | |
| 'a': 1, | |
| 'b': 2, | |
| 'c': 1 | |
| }, function(result, value, key) { | |
| (result[value] || (result[value] = [])).push(key); | |
| return result; | |
| }, {}); | |
| console.log('[REDUCE NUMBER] ' + reduceNumber) | |
| console.log('[REDUCE OBJECT]') | |
| console.log(reduceObject) | |
| */ | |
| var newDevelopment = [{ | |
| name: 'Miss Scarlet', | |
| present: true, | |
| rooms: [{ | |
| kitchen: false | |
| }, { | |
| ballroom: false | |
| }, { | |
| conservatory: true | |
| }, { | |
| 'dining room': true | |
| }, { | |
| 'billiard room': false | |
| }, { | |
| library: true | |
| }] | |
| }, { | |
| name: 'Reverend Green', | |
| present: true, | |
| rooms: [{ | |
| kitchen: true | |
| }, { | |
| ballroom: false | |
| }, { | |
| conservatory: false | |
| }, { | |
| 'dining room': false | |
| }, { | |
| 'billiard room': true | |
| }, { | |
| library: false | |
| }] | |
| }, { | |
| name: 'Colonel Mustard', | |
| present: true, | |
| rooms: [{ | |
| kitchen: false | |
| }, { | |
| ballroom: false | |
| }, { | |
| conservatory: true | |
| }, { | |
| 'dining room': false | |
| }, { | |
| 'billiard room': true | |
| }, { | |
| library: false | |
| }] | |
| }, { | |
| name: 'Professor Plum', | |
| present: true, | |
| rooms: [{ | |
| kitchen: true | |
| }, { | |
| ballroom: false | |
| }, { | |
| conservatory: false | |
| }, { | |
| 'dining room': true | |
| }, { | |
| 'billiard room': false | |
| }, { | |
| library: false | |
| }] | |
| }]; | |
| var getNotPresentRooms = function getNotPresentRooms() { | |
| var test = {}; | |
| var test2 = {}; | |
| _.each(newDevelopment, function (el) { | |
| var rooms = el.rooms; | |
| _.each(rooms, function (room) { | |
| for (myRoom in room) { | |
| test2[myRoom] = false; | |
| if (room[myRoom]) { | |
| test[myRoom] = true; | |
| } | |
| } | |
| }); | |
| }); | |
| presentRooms = Object.keys(test); | |
| allRooms = Object.keys(test2); | |
| notPresentRoom = _.xor(presentRooms, allRooms); | |
| console.log(notPresentRoom); | |
| }; | |
| //getNotPresentRooms() | |
| var reduceNotPresentRooms = function reduceNotPresentRooms(accumulator, value) { | |
| var emptyRooms = _.reduce(value.rooms, function (accum, room) { | |
| for (innerKey in room) { | |
| if (!room[innerKey]) { | |
| accum.push(innerKey); | |
| } | |
| } | |
| return accum; | |
| }, []); | |
| accumulator.push(emptyRooms); | |
| return accumulator; | |
| }; | |
| var optimizedGetRooms = function optimizedGetRooms() { | |
| var _ref; | |
| var notInRoms = _.reduce(newDevelopment, reduceNotPresentRooms, []); | |
| var result = (_ref = _).intersection.apply(_ref, _toConsumableArray(notInRoms)); | |
| console.log(result); | |
| }; | |
| // optimizedGetRooms() | |
| /* | |
| const roomReducer = (result, value, key,collection) => { | |
| console.log(value) | |
| } | |
| const reducerFunc = (result, value, key,collection) => { | |
| const testReducerRoom = _.reduce(value.rooms, roomReducer, {} ) | |
| console.log(testReducerRoom) | |
| } | |
| const test = _.reduce(newDevelopment, reducerFunc, {}) | |
| console.log(test)*/ | |
| /* | |
| const notInRoom = (suspect, memo) => { | |
| const emptyRooms = _.reduce(suspect.rooms, (room, memo) => { | |
| if (room === false) { | |
| memo.push(room) | |
| } | |
| return memo | |
| }, []) | |
| console.log(emptyRooms) | |
| return emptyRooms | |
| } | |
| notInRooms = _.map(newDevelopment, notInRoom) | |
| const test = _.intersection(...notInRooms) | |
| console.log(test) | |
| */ | |
| /* | |
| // simple currying for a function with known parameters | |
| const abc = (a,b)=> { | |
| return [a,b] | |
| } | |
| curry = (fn) => { | |
| return (arg1) => { | |
| return (arg2) => { | |
| return fn(arg1,arg2) | |
| } | |
| } | |
| } | |
| const curried = curry(abc) | |
| const firstRun = curried(1) | |
| const secondRun = firstRun(5) | |
| console.log(secondRun) | |
| console.log(curried(1)(2)) | |
| */ | |
| var compose = function compose(fn1, fn2) { | |
| return function (arg) { | |
| return fn1(fn2(arg)); | |
| }; | |
| }; | |
| var consider = function consider(name) { | |
| return 'I think it could be... ' + name; | |
| }; | |
| var exclaim = function exclaim(statement) { | |
| return statement.toUpperCase() + '!'; | |
| }; | |
| var blame = compose(consider, exclaim); | |
| console.log(blame('my variable')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment