Last active
December 29, 2022 07:49
-
-
Save branneman/35955ac5518ee5db97c6a6f702301871 to your computer and use it in GitHub Desktop.
Code Simplicity is the Ultimate Sophistication - https://youtu.be/zxJnyMXhyvw
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
// Imperative: 'what' + 'how' | |
const makes1 = [] | |
for (let i = 0; i < cars.length; i += 1) { | |
makes1.push(cars[i].make) | |
} | |
// Declarative: only 'what' | |
const makes2 = cars.map((car) => car.make) |
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
// more syntax: more complex, less readable | |
const getAction1 = (newPayload, isRejected) => ({ | |
type: `${type}_${isRejected ? REJECTED : FULFILLED}`, | |
...(newPayload ? { payload: newPayload } : {}), | |
...(!!meta ? { meta } : {}), | |
...(isRejected ? { error: true } : {}), | |
}) | |
// less syntax: less complex, more readable | |
const getAction2 = (newPayload, isRejected) => { | |
const action = { | |
type: `${type}_${isRejected ? REJECTED : FULFILLED}`, | |
} | |
if (newPayload) { | |
action.payload = newPayload | |
} | |
if (Boolean(meta)) { | |
action.meta = meta | |
} | |
if (isRejected) { | |
action.error = true | |
} | |
return action | |
} |
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 list = [1, 2, 3, 42, 42, 73, 1337] | |
// unique with reduce | |
const a = list.reduce( | |
(curr, acc) => (curr.includes(acc) ? curr : [...curr, acc]), | |
[] | |
) | |
// unique with filter | |
const b = list.filter((v, i, a) => a.indexOf(v) === i) | |
// unique by converting to a Set | |
const c = Array.from(new Set(list)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment