Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

RunKit + npm: sanctuary

const {set, ifElse, lensIndex, __, curry, update, compose, findIndex, append, equals} = require("ramda") const S = require('sanctuary') const isIndex = (i) => i >= 0 const indexOrNothing = ifElse(isIndex, S.Just, S.Nothing) const findIndexx = curry(compose(indexOrNothing, findIndex)) const replaceIndex = curry((array, replacement, index) => set(lensIndex(index), replacement, array)) const findAndReplace = (findFn, array, replacement) => findIndexx(findFn, array) .map(replaceIndex(array, replacement)) const addOrReplace = (findFn, array, replacement) => S.maybe(append(replacement, array), update(__, replacement, array), findIndexx(findFn, array)) const input = ['bar', 'baz'] const isBar = equals('bar') console.log(addOrReplace(equals('bar'), input, 'boo')) console.log(addOrReplace(equals('baa'), input, 'boo'))
Loading…

no comments

    sign in to comment