Ramda Compose Functional Data Pipeline

Build point-free data transformation pipelines with Ramda compose, lens-based updates, and curried predicates that stay testable end to end.

JavascriptData Integrity

Code

javascriptsnippet
25 lines
import { compose, map, filter, prop, sortBy, descend, lensPath, over, add, curry } from 'ramda';

const orders = [
  { id: 1, customer: 'ada', total: 120, status: 'paid' },
  { id: 2, customer: 'grace', total: 85, status: 'refunded' },
  { id: 3, customer: 'ada', total: 340, status: 'paid' },
];

const applyLoyalty = curry((rate, order) =>
  over(lensPath(['total']), add(Math.round(order.total * rate)), order),
);

const summarize = compose(
  map(prop('customer')),
  sortBy(descend(prop('total'))),
  map(applyLoyalty(0.1)),
  filter((o) => o.status === 'paid'),
);

console.log(summarize(orders));

const raise = curry((amount, order) =>
  over(lensPath(['total']), add(amount), order),
);
console.log(raise(5)(orders[0]));

How the snippet works

The snippet’s external surface is compose, map, filter, prop, sortBy, all of it via ramda.

Module-level bindings hold the data — orders, applyLoyalty, summarize, raise — with no function scope in between.

Caveats

  • Plan for an install step: ramda is a real dependency, resolved by the package manager.

Statements that matter

const raise = curry((amount, order) =>
over(lensPath(['total']), add(amount), order),
);
const applyLoyalty = curry((rate, order) =>
over(lensPath(['total']), add(Math.round(order.total * rate)), order),
);

Interactive Tools for This Topic

FAQ

Does this require external packages?
Yes — `ramda`. Add it to your project before running the snippet.