Skip to content

Instantly share code, notes, and snippets.

@emirotin
Created July 23, 2016 20:05
Show Gist options
  • Save emirotin/8f2e469f3704de09c5b1009240d3535b to your computer and use it in GitHub Desktop.
Save emirotin/8f2e469f3704de09c5b1009240d3535b to your computer and use it in GitHub Desktop.
const replace = (arr, i, newEl) => arr.map((el, k) => {
return k === i ? newEl : el
})
const replaceWhere = (arr, pred, transform) => arr.map((el, i) => {
return pred(el, i) ? transform(el, i) : el
})
const swap = (arr, i, j) => arr.map((el, k) => {
if (k === i) return arr[j]
if (k === j) return arr[i]
return el
})
// Usage:
const reducer = state => {
...state,
todos: swap(state.todos, i, j)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment