sanctuary
instead of something like purescript/elm or alikes??
pipe
rather than method chaining, @MikaAK. Let me find an example featuring fromMaybe
.
const replaceIndex = curry((array, replacement, index) => set(lensIndex(index), replacement, array))
const replaceInArray = (findFn, array, replacement) => findIndex(findFn)
.map(replaceIndex(array, replacement))
// need fromMaybe(array.concat(replacement))
// findIndex_ :: (a -> Boolean, Array a) -> Maybe Integer
const findIndex_ = (pred, xs) => {
const idx = R.findIndex(pred, xs);
return idx >= 0 ? S.Just(idx) : S.Nothing();
};
// f :: (a -> Boolean, a, Array a) -> Array a
const f = (pred, repl, xs) =>
S.maybe(R.append(repl, xs),
S.compose(R.set(R.__, repl, xs), R.lensIndex),
findIndex_(pred, xs));
[f(R.equals('baz'), 'quux', []),
f(R.equals('baz'), 'quux', ['foo']),
f(R.equals('baz'), 'quux', ['foo', 'bar']),
f(R.equals('baz'), 'quux', ['foo', 'bar', 'baz'])];
R.update
// findIndex_ :: (a -> Boolean, Array a) -> Maybe Integer
const findIndex_ = (pred, xs) => {
const idx = R.findIndex(pred, xs);
return idx >= 0 ? S.Just(idx) : S.Nothing();
};
// f :: (a -> Boolean, a, Array a) -> Array a
const f = (pred, repl, xs) =>
S.maybe(R.append(repl, xs),
R.update(R.__, repl, xs),
findIndex_(pred, xs));
[f(R.equals('baz'), 'quux', []),
f(R.equals('baz'), 'quux', ['foo']),
f(R.equals('baz'), 'quux', ['foo', 'bar']),
f(R.equals('baz'), 'quux', ['foo', 'bar', 'baz'])];
// findIndex_ :: (a -> Boolean, Array a) -> Maybe Integer
const findIndex_ = (pred, xs) => {
const idx = R.findIndex(pred, xs);
return idx >= 0 ? S.Just(idx) : S.Nothing();
};
// f :: (a -> Boolean, a, Array a) -> Array a
const f = (pred, repl, xs) => {
const thunk =
S.maybe(() => R.append(repl, xs),
idx => () => R.update(idx, repl, xs),
findIndex_(pred, xs));
return thunk();
};
[f(R.equals('baz'), 'quux', []),
f(R.equals('baz'), 'quux', ['foo']),
f(R.equals('baz'), 'quux', ['foo', 'bar']),
f(R.equals('baz'), 'quux', ['foo', 'bar', 'baz'])];