Skip to content

Instantly share code, notes, and snippets.

@srdjan
Forked from 1Marc/ramdaprogram.js
Created January 21, 2017 10:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srdjan/27eafc1d8ce93092b0e468e70418bbd0 to your computer and use it in GitHub Desktop.
Save srdjan/27eafc1d8ce93092b0e468e70418bbd0 to your computer and use it in GitHub Desktop.
ramda program
var byMonth = R.groupBy(R.prop('Month'));
var byAuthor = R.groupBy(R.prop('Author'));
var royalty_key = 'Royalty (SUM)';
var months_keys = R.uniq(R.map(R.prop('Month'), data)).sort();
var monthly_revenue =
R.map((group) =>
R.reduce((acc, record) => acc + parseMoney(record[royalty_key]), 0, group),
byMonth(data));
monthly_revenue = R.pickAll(months_keys, monthly_revenue); // sort by months_keys
monthly_revenue = R.map(R.invoker(1, 'toFixed')(2), monthly_revenue);
var data_by_month_keys =
R.map((data_by_authors) =>
R.flatten(R.map((month) =>
R.filter((record) => record.Month == month, data_by_authors),
months_keys)),
byAuthor(data));
var authors = R.keys(data_by_month_keys);
var reduceToUsageBy = R.reduceBy((acc, record) =>
acc + parseUsage(record['PaidUsageInSeconds (SUM)']), 0);
var usageByMonth = reduceToUsageBy((record) => record.Month);
var usage_data = R.map((data_by_authors) => usageByMonth(data_by_authors), data_by_month_keys);
var mergeAuthorNameKeys = R.compose(R.values, R.mapObjIndexed(R.flip(R.assoc('AuthorName'))));
usage_data = mergeAuthorNameKeys(usage_data);
monthly_revenue = Object.assign({}, {AuthorName: "Total Month"}, monthly_revenue);
var data = R.concat(usage_data, [monthly_revenue]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment