ferrum icon indicating copy to clipboard operation
ferrum copied to clipboard

mapKey, mapValue, mapNth, mapLast, mapProp

Open koraa opened this issue 5 years ago • 0 comments

Map on a specific element of a set or object:

const euclidRem = (n, d) => n<0 ? d+(n%d) : n%d; / for d>0
const mapNth = (seq, idx, fn) => map(seq, (tup) => {
  const l = list(tup);
  const i = euclidRem(idx, l.length);
  l[i] = fn(l[i]);
  return l;
});
const mapKey = (seq, fn) => mapNth(seq, 0, fn);
const mapValue = (seq, fn) => mapNth(seq, 1, fn);
const mapLast = (seq, fn) => mapNth(seq, -1, fn);

const mapProp = (seq, name, fn) => map(seq, (o) => { ...o, [name]: fn(o[name]) });

mapProp may be unsuitable; it would be more ferrums style to use get()/set(). This may also be a suitable generalization, but would drop the implicit coercion to list by mapKey/Value/Nth/Last.

koraa avatar Oct 12 '20 17:10 koraa