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 add = (x, y) => x + y | |
| const sub = (x, y) => x - y | |
| console.log('10 + 3 - 4 =', sub( add(10, 3), 4 ) ) |
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 add = x => y => x + y | |
| const sub = x => y => x - y | |
| console.log('10 + 3 - 4 =', sub( add(10)(3) )(4) ) |
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
| // if the other argument is lower than 0, return 0 | |
| const atLeastZero = Math.max.bind(null, 0) | |
| console.log(atLeastZero(10)) // 10 | |
| console.log(atLeastZero(-10)) // 0 | |
| console.log(atLeastZero(0)) // 0 |
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 curry = fn => | |
| // if this function takes one argument | |
| fn.length === 1 | |
| // it's already curry | |
| ? fn | |
| // otherwise let's create a curry version | |
| // return a function that takes one argument | |
| // when we receive it we bind it to fn to create a new | |
| // function then we curry the rest recursively | |
| : x => curry(fn.bind(null, x)) |
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 maxCurry = x => y => | |
| Math.max(x, y) | |
| // if the other argument is lower than 0, return 0 | |
| const atLeastZero = maxCurry(0) | |
| console.log(atLeastZero(10)) // 10 | |
| console.log(atLeastZero(-10)) // 0 | |
| console.log(atLeastZero(0)) // 0 |
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 letterToMessage = (letter, index) => | |
| `${letter} is at position ${index}` | |
| const letterIsAlwaysZ = letterToMessage.bind(null, 'z') | |
| const letters = ['a', 'b', 'c', 'd', 'e'] | |
| const messages = letters.map(letterToMessage) | |
| // Using the full function | |
| console.log(letterToMessage('a', 0)) // a is at position 0 |
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 partial = (fn, ...args) => | |
| fn.bind(null, ...args) | |
| const propEq = (propName, expectedValue, object) => | |
| object[propName] === expectedValue | |
| const users = | |
| [ {username: 'lubien', online: false} | |
| , {username: 'rian-vergara', online: true } | |
| , {username: 'leochrisis', online: true } |
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 partial = (fn, ...args) => | |
| fn.bind(null, ...args) | |
| const prop = (propName, object) => | |
| object[propName] | |
| const users = | |
| [ {username: 'lubien', name: 'João' } | |
| , {username: 'rian-vergara', name: 'Rian' } | |
| , {username: 'leochrisis', name: 'Leonardo'} |
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 getAll = userUID => | |
| database | |
| .ref('user_data') | |
| .child(userUID) | |
| .child('customers') | |
| // exemplo de uso: | |
| getAll(myUserId).on('value', snap => { | |
| const value = snap.val() |
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 partial = (fn, ...args) => | |
| fn.bind(null, ...args) | |
| const add3 = (x, y, z) => x + y + z | |
| const addOnePlusTwo = partial(add3, 1, 2) | |
| console.log(addOnePlusTwo(1)) // 4 | |
| console.log(addOnePlusTwo(10)) // 13 |