Skip to content

mapKey, mapValue, mapNth, mapLast, mapProp #124

Open
@koraa

Description

@koraa

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions