├── ArroFuntionsexamples.js ├── ESLintexample.js ├── README.md ├── class04_01.js ├── closureexamples.js ├── combinationexamples.js ├── curryingAndPartial.js ├── examples0.js ├── functionsAsArguementsexamples.js ├── functionsAsData2examples.js ├── functionsAsDataexamples.js ├── highierOrderFunctions.js ├── index.js ├── mapexamples.js ├── package-lock.json ├── package.json ├── privateVariable.js ├── recursion.js ├── reduceexamples.js ├── sliceexamples.js ├── someexamples.js ├── sortexamples.js └── trial.js /ArroFuntionsexamples.js: -------------------------------------------------------------------------------- 1 | const add = (x,y) => x + y; 2 | 3 | const double = number => number*2; 4 | 5 | const double = function (number) { 6 | return number*2; 7 | } 8 | 9 | const sayHello = () => console.log('Hello'); 10 | 11 | const myArrowFunction = (arg1, arg2) => ({ 12 | name: 'Ronald', 13 | }); -------------------------------------------------------------------------------- /ESLintexample.js: -------------------------------------------------------------------------------- 1 | const x = 5; 2 | x = 6; 3 | 4 | const numbers = [1,2,3,4,5]; 5 | numbers[0] = 100; 6 | numbers.reverse(); 7 | 8 | const person = { 9 | name: 'John Doe', 10 | age: 34, 11 | }; 12 | person.name = 'Bob'; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Functional-programming-ES6 2 | Functional programming with ES6 3 | -------------------------------------------------------------------------------- /class04_01.js: -------------------------------------------------------------------------------- 1 | // words :: String -> [String] 2 | /*const words = str => split(' ', str); 3 | 4 | const split = (sep, str) => str.split(sep); 5 | 6 | const w = 'A boy has a dog' 7 | 8 | const r = words(w) 9 | 10 | console.log(r);*/ 11 | 12 | 13 | /*const words = str => split(' ', str); 14 | 15 | const split = (sep, str) => str.split(sep); 16 | 17 | const w = 'Jingle bells Batman smells' 18 | 19 | console.log(words,(w));*/ 20 | 21 | 22 | const s = 'Jingle bells Batman smells' 23 | const split = (sep, str) => str.split(sep); 24 | const words = split('') 25 | console.log(words,(s)); 26 | 27 | -------------------------------------------------------------------------------- /closureexamples.js: -------------------------------------------------------------------------------- 1 | const createPrinter = () =>{ 2 | const myFavoriteNumber = 28; 3 | 4 | return () => 5 | console.log ('My favorite number is ${myFavoriteNumber}'); 6 | } 7 | 8 | const printer = createPrinter (); 9 | printer(); 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /combinationexamples.js: -------------------------------------------------------------------------------- 1 | const employees = [{ 2 | name: 'John Olsen', 3 | age: 54, 4 | jobTitle: 'developer', 5 | salary: 70000, 6 | }, { 7 | name: 'Karen Norris', 8 | age: 34, 9 | jobTitle: 'engineer', 10 | salary: 75000, 11 | }, { 12 | name: 'Daryl Cline', 13 | age: 25, 14 | jobTitle: 'secretary', 15 | salary: 54000, 16 | }, { 17 | name: 'Abbey Garcia', 18 | age: 40, 19 | jobTitle: 'developer', 20 | salary: 100000, 21 | }, { 22 | name: 'Finn Smith', 23 | age: 29, 24 | jobTitle: 'engineer', 25 | salary: 40000, 26 | }, { 27 | name: 'Eve Wordsworth', 28 | age: 20, 29 | jobTitle: 'developer', 30 | salary: 65000, 31 | }, { 32 | name: 'Ronald Jacobs', 33 | age: 60, 34 | jobTitle: 'developer', 35 | salary: 90000, 36 | }]; 37 | 38 | const developers = employees.filter(employees => 39 | employees.jobTitle==='developer'); 40 | 41 | const developerSalaries = developers.map(developers=> 42 | developers.salary) 43 | const totalDeveloperSalaries = developerSalaries.reduce((acc, x) => 44 | acc + x, 0); 45 | const averageDeveloperSalary = totalDeveloperSalaries/developerSalaries.length; 46 | 47 | const nonDevelopers = employees.filter(employee => 48 | employee.jobTitle !== 'developer'); 49 | const nonDeveloperSalaries = nonDevelopers.map(nonDev => nonDev.salary); 50 | const totalNonDeveloperSalaries = nonDeveloperSalaries.reduce((acc, x) => acc + x, 0); 51 | const averageNonDeveloperSalary = totalNonDeveloperSalaries / nonDeveloperSalaries.length; 52 | console.log(averageNonDeveloperSalary); 53 | 54 | -------------------------------------------------------------------------------- /curryingAndPartial.js: -------------------------------------------------------------------------------- 1 | const add = (x,y,z) => x + y + z; 2 | 3 | /*const addPartial = x => 4 | (y,z) => add (x, y, z); 5 | 6 | const add5 = addPartial (5); 7 | const sum = add(5, 6, 7); 8 | 9 | console.log(sum);*/ 10 | 11 | /*const addPartial = (x, y) => 12 | z => add(x, y, z); 13 | 14 | const add5and6 = addPartial (5, 6); 15 | const sum = add5and6(7) 16 | console.log(sum);*/ 17 | 18 | //currying 19 | const addPartial = x => 20 | y => 21 | z=> add(x, y, z); 22 | const sum = addPartial(5)(6)(7); 23 | console.log(sum); -------------------------------------------------------------------------------- /examples0.js: -------------------------------------------------------------------------------- 1 | const createPrinter = () => { 2 | const myFavoriteNumber = 42; 3 | 4 | return () => 5 | console.log('My favorite number is ${myFavoriteNumber'); 6 | } 7 | 8 | const printer = createPrinter(); 9 | printer(); -------------------------------------------------------------------------------- /functionsAsArguementsexamples.js: -------------------------------------------------------------------------------- 1 | const add = (x, y) => x + y; 2 | const subtract = (x, y) => x - y; 3 | 4 | const combine2and3 = func => 5 | func(2,3); 6 | 7 | combine2and3(add); // ->2+3 8 | combine2and3((x,y) => x+y); // -> passing anynomous function 9 | combine2and3(subtract); // ->2-3 10 | 11 | combine2and3(math.max); // ->3 12 | 13 | -------------------------------------------------------------------------------- /functionsAsData2examples.js: -------------------------------------------------------------------------------- 1 | const double = x => x * 2; 2 | const subtractOne = x => x - 1; 3 | const triple = x => x * 3; 4 | const add5 = x => x + 5; 5 | 6 | const myNumber = 42; 7 | 8 | const doubled = double(myNumber); 9 | const minusOne = subtractOne(doubled); 10 | // ... 11 | 12 | const functionsArray = [ 13 | double, 14 | subtractOne, 15 | triple, 16 | add5, 17 | Math.sqrt, 18 | ]; 19 | 20 | var number = 42; 21 | 22 | functionsArray.forEach(func => number = func(number)); 23 | 24 | console.log(number); -------------------------------------------------------------------------------- /functionsAsDataexamples.js: -------------------------------------------------------------------------------- 1 | const sayHello = name => console.log('Hello' + name); 2 | const sayHello2 = sayHello; 3 | sayHello2('Ronald'); 4 | 5 | const myFuntion = true 6 | ? () => console.log ('First option') 7 | : () => console.log('Second option'); 8 | const fetchDataReal = () => { 9 | //time-intesive operations here! 10 | } 11 | 12 | const fetchDataFake = () => ({ 13 | name: 'kyobe Rooney', 14 | age: 20, 15 | }); 16 | 17 | const fetchData = Development 18 | ? fetchDataFake 19 | :fetchDataReal; 20 | 21 | -------------------------------------------------------------------------------- /highierOrderFunctions.js: -------------------------------------------------------------------------------- 1 | const divide = (x,y) => x/y; 2 | 3 | const secondArguementIsntZero =func => 4 | (...args) => { 5 | if (args[1]===0) { 6 | console.log ('Error: dividing by zero'); 7 | return null; 8 | } 9 | 10 | return func (...args); 11 | } 12 | 13 | const divideSafe = secondArguementIsntZero(divide); 14 | console.log(divideSafe(7, 0)); 15 | console.log(divideSafe(5, 3)); -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const person = { 2 | name: 'Jimmy Smith', 3 | age: '10', 4 | hairColor: 'brown', 5 | eyeColor: 'blue', 6 | }; 7 | 8 | const carrerData = { 9 | title: 'developer', 10 | company: 'Innovative computer systems', 11 | }; 12 | 13 | const updates = { 14 | name: 'Ronald Kyobe' 15 | }; 16 | 17 | const numbers = [1,2,3,4,5] 18 | 19 | const newNumbers = [ 20 | ...numbers, 21 | ...6, 22 | ]; 23 | 24 | 25 | const personwithData = { 26 | name: person.name, 27 | ...carrerData, 28 | }; 29 | 30 | const updatedPerson = { 31 | ...updates, 32 | ...carrerData, 33 | } 34 | 35 | const numbers = [1, 2, 3, 4, 5]; 36 | const newNumbers = [ 37 | 0, 38 | ...numbers, 39 | 6, 40 | ]; 41 | 42 | console.log(newNumbers); -------------------------------------------------------------------------------- /mapexamples.js: -------------------------------------------------------------------------------- 1 | const numbers = [1, 2, 3, 4, 5]; 2 | 3 | const double = x=>x*2; 4 | 5 | const doubledNumbers = numbers.map(double); 6 | 7 | const isEven = x => x % 2 === 0; 8 | 9 | const evenNumbers = numbers.filter(isEven); 10 | 11 | const words = [ 12 | 'hello', 13 | 'goodbye', 14 | 'the', 15 | 'Antarctica', 16 | ]; 17 | 18 | const createLengthTest = minLength => 19 | word => word.length > minLength; 20 | 21 | const longWords = words.filter(createLengthTest(5)); 22 | 23 | console.log(longWords); -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "working", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1 5 | } 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "working", 3 | "version": "1.0.0", 4 | "description": "tying my hands on node", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start": "index.js" 9 | }, 10 | "author": "", 11 | "license": "ISC" 12 | } 13 | -------------------------------------------------------------------------------- /privateVariable.js: -------------------------------------------------------------------------------- 1 | const Person = ({ name, age, job}) => { 2 | var _name = name; 3 | var _age = age; 4 | var _job = job; 5 | 6 | return { 7 | getName: () => _name, 8 | getAge: () => _age, 9 | getJob: () => _job, 10 | 11 | setJob: newJob => _job = newJob, 12 | setAge: newAge => _age = newAge, 13 | }; 14 | } 15 | 16 | const me = Person ({ name: 'Ronald', age: 25, job: 'developer'}) 17 | /*console.log(me.getJob()); 18 | me.setJob ('Senior developer'); 19 | console.log(me.getJob());*/ 20 | console.log(me.getAge()); 21 | me.setAge (28); 22 | console.log(me.getAge()); -------------------------------------------------------------------------------- /recursion.js: -------------------------------------------------------------------------------- 1 | const countDown = (x, max) => { 2 | if (x<0) return; 3 | console.log (x); 4 | countDown(x-1); 5 | }; 6 | 7 | countDown(10); 8 | 9 | /*const countUp = (x, max) => { 10 | if (x > max) return; 11 | console.log(x); 12 | countUp(x + 1, max); 13 | }; 14 | 15 | countUp(0, 10);*/ 16 | -------------------------------------------------------------------------------- /reduceexamples.js: -------------------------------------------------------------------------------- 1 | const numbers = [5, 7, 2, 40, 23, 14, 8, 4, 11]; 2 | 3 | const product = numbers.reduce((acc, x) => { 4 | console.log(`acc is ${acc}`); 5 | console.log(`x is ${x}`); 6 | 7 | return acc * x; 8 | }, 1); 9 | 10 | console.log(product); -------------------------------------------------------------------------------- /sliceexamples.js: -------------------------------------------------------------------------------- 1 | const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 2 | 3 | console.log(numbers.slice().reverse()); 4 | console.log(numbers); -------------------------------------------------------------------------------- /someexamples.js: -------------------------------------------------------------------------------- 1 | const employees = [{ 2 | name: 'Jane Doe', 3 | salary: 90000, 4 | }, { 5 | name: 'Donald Jones', 6 | salary: 65000, 7 | }, { 8 | name: 'Donna Appleseed', 9 | salary: 1500000, 10 | }, { 11 | name: 'John Smith', 12 | salary: 250000, 13 | }]; 14 | 15 | const makesMoreThanOneMillion = employee => 16 | employee.salary > 1000000; 17 | 18 | const result = employees.some(makesMoreThanOneMillion); 19 | 20 | const formValues = [ 21 | 'Ronald', 22 | 'Kyobe', 23 | 'Mukalazi', 24 | 'Developer', 25 | ]; 26 | 27 | const isNotEmpty = string => !!string; 28 | const allFieldsFilled = formValues.every(isNotEmpty); 29 | 30 | console.log(allFieldsFilled); -------------------------------------------------------------------------------- /sortexamples.js: -------------------------------------------------------------------------------- 1 | const mixedUpNumbers = [10, 2, 4, 3, 7, 5, 8, 9, 1, 6]; 2 | 3 | const ascending = (a,b) => { 4 | if (ab) return 1; 6 | return 0; 7 | } 8 | 9 | const sortedNumbers = mixedUpNumbers.slice().sort(ascending); 10 | console.log(sortedNumbers); -------------------------------------------------------------------------------- /trial.js: -------------------------------------------------------------------------------- 1 | const person = { 2 | name: 'Jimmy Smith', 3 | age: '10', 4 | hairColor: 'brown', 5 | eyeColor: 'blue', 6 | }; 7 | 8 | const carrerData = { 9 | title: 'developer', 10 | company: 'Innovative computer systems', 11 | }; 12 | 13 | const personwithData = { 14 | ...person, 15 | ...carrerData, 16 | }; --------------------------------------------------------------------------------