Skip to content

Instantly share code, notes, and snippets.

@branneman
Last active December 29, 2022 07:49
Show Gist options
  • Save branneman/35955ac5518ee5db97c6a6f702301871 to your computer and use it in GitHub Desktop.
Save branneman/35955ac5518ee5db97c6a6f702301871 to your computer and use it in GitHub Desktop.
Code Simplicity is the Ultimate Sophistication - https://youtu.be/zxJnyMXhyvw
// 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)
// 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
}
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