Skip to content

Instantly share code, notes, and snippets.

@Izhaki
Created July 15, 2018 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Izhaki/02e9218918a7f6e21d1c000204d19673 to your computer and use it in GitHub Desktop.
Save Izhaki/02e9218918a7f6e21d1c000204d19673 to your computer and use it in GitHub Desktop.
Some examples of comparison between imperative and more function style - hinting the latter is not always better.

Imperative:

export default expressions => mapIterator(row => {
  const projection = {};
  for (const colName in expressions) {
    const expression = expressions[colName];
    projection[colName] = expression(row);
  }
  return projection;
});

functional:

const mapObjectValues = obj => mapper => {
  const result = {};
  Object.entries(obj).forEach(([key, value]) => {
    result[key] = mapper(value);
  });
  return result;
};

export default expressions => {
  const mapExpressions = mapObjectValues(expressions);
  return mapIterator(row => mapExpressions(expression => expression(row)));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment