Last active
October 21, 2023 00:23
-
-
Save hamzaerbay/fc62864cd40cfcf4eb14d447abceb05a to your computer and use it in GitHub Desktop.
Reusable Functions
This file contains 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 foo = x => x + 2; | |
const bar = x => x * 3; | |
console.log(foo(bar(5))) | |
const scream = str => str.toUpperCase() | |
const exclaim = str => `${str}!` | |
const repeat = str => `${str} ${str}`; | |
console.log(repeat(exclaim(scream('hololo')))) | |
// lets do HTMLBaseFontElement | |
const compose = (...fns) => x => | |
fns.reduceRight((acc,fn) => fn(acc), x) | |
const withExuberance = compose(repeat, exclaim, scream) | |
console.log(withExuberance('hololo hololo')) | |
const pipe = (...fns) => x => | |
fns.reduce((acc,fn)=>fn(acc),x) | |
const withExuberance2 = pipe( | |
scream, exclaim, repeat | |
) | |
console.log(withExuberance2('lolo')) |
This file contains 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
function sum(x) { | |
return function(y) { | |
return x + y; | |
} | |
} | |
console.log(sum(3,2)) // second variable is ignored | |
const addThree = sum(1) | |
console.log(addThree(3)) | |
const sum2 = x => y => x+y; |
This file contains 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
// Higher order Functions | |
// 1. Accept a function as an argument | |
// 2. Returns a new function | |
const withCount = fn => { | |
let count = 0; | |
return (...args) => { | |
console.log(`calls count ${++count}`); | |
return fn(...args); | |
} | |
} | |
const add = (x,y) => x + y; | |
const countedAdd = withCount(add) | |
console.log(countedAdd(1,2)) | |
console.log(countedAdd(2,3)) | |
console.log(countedAdd(2,5)) | |
console.log(countedAdd(3,6)) | |
console.log(countedAdd(4,2)) |
This file contains 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 mutableArray = [1,2,3] | |
const rAA = mutableArray; | |
rAA.push(4); | |
console.log(mutableArray); | |
const mutableObject = {foo: 'bar'} | |
const rAO = mutableObject; | |
rAO.foo = 'baz'; | |
console.log(mutableObject) | |
const push = value => array => { | |
const clone = [...array]; | |
clone.push(value) | |
return clone; | |
} | |
// for Immutable array | |
const immutableArray = [1,2,3]; | |
const rAAI = push(4)(immutableArray) | |
console.log(immutableArray) | |
console.log(rAAI) | |
class MutableGlass { | |
constructor(content, amount) { | |
this.content = content; | |
this.amount = amount; | |
} | |
takeDrink(value) { | |
this.amount = Math.max(this.amount - value, 0); | |
return this | |
} | |
} | |
const mg1 = new MutableGlass('water', 100); | |
const mg2 = mg1.takeDrink(20) | |
console.log(mg1, mg2) | |
console.log(mg1 === mg2) | |
console.log(mg1.amount == mg2.amount) | |
class ImmutableGlass { | |
constructor(content, amount) { | |
this.content = content; | |
this.amount = amount; | |
} | |
takeDrink(value) { | |
return new ImmutableGlass(this.content, Math.max(this.amount - value, 0)) | |
} | |
} | |
const ig1 = new ImmutableGlass('water', 100); | |
const ig2 = ig1.takeDrink(20) | |
console.log(ig1, ig2) | |
console.log(ig1 === ig2) | |
console.log(ig1.amount == ig2.amount) |
This file contains 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
// Partial Application | |
const fetch = require('node-fetch') | |
const getFromAPI = baseUrl => endpoint => cb => | |
fetch(`${baseUrl}${endpoint}`) | |
.then(res=> res.json()) | |
.then(data=> cb(data)) | |
.catch(err=>{ | |
console.error(err.message); | |
}) | |
const getGithub = getFromAPI('https://api.github.com') | |
const getGithubUsers = getGithub('/users') | |
const getGithubRepos = getGithubUsers('/repositories') | |
getGithubUsers(data => { | |
console.log(data.map(user => user.login)) | |
}) | |
getGithubUsers(data => { | |
console.log(data.map(user => user.avatar_url)) | |
}) |
This file contains 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 array = [1,2,3] | |
// array.map( x => x * 2) | |
const double = x => x*2; | |
array.map(double) | |
// Legibility | |
// Reduce surface area | |
// Unit test our named functions |
This file contains 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
// f(x) = x + 1 | |
const f = x => x+1; | |
// EX 1 global state | |
const COST_OF_ITEM = 19 | |
const cartTotal = quantity => COST_OF_ITEM * quantity; | |
console.log(cartTotal(2)) | |
console.log(cartTotal(2)) | |
// EX 2 same input different output | |
const generateID = () => Math.floor(Math.random() * 10000) | |
console.log(generateID()) | |
console.log(generateID()) | |
console.log(generateID()) | |
const createUser = (name, age) => ({ | |
id: generateID(), | |
name, | |
age | |
}) | |
console.log(createUser('hamza', 32)) | |
console.log(createUser('hamza', 32)) | |
console.log(createUser('hamza', 32)) | |
// for fixing the different id generation | |
// EX 3 side effects#1 | |
let id = 0 | |
const createFoodItem = name => ({ | |
id: ++id, | |
name | |
}) | |
console.log(createFoodItem('CheeseBurger')) | |
console.log(createFoodItem('Fries')) | |
console.log(createFoodItem('Coke')) | |
console.log(createFoodItem(id)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment