Created
July 23, 2016 20:05
-
-
Save emirotin/8f2e469f3704de09c5b1009240d3535b to your computer and use it in GitHub Desktop.
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 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