Skip to content

Instantly share code, notes, and snippets.

@karloscodes
Last active March 6, 2018 13:27
Show Gist options
  • Save karloscodes/acf2bbb8b86da45e6bce9471d1ef758a to your computer and use it in GitHub Desktop.
Save karloscodes/acf2bbb8b86da45e6bce9471d1ef758a to your computer and use it in GitHub Desktop.
Ramda, Javascript, Functional Programming
const R = require('ramda');
const users = [
  { ses: { sesCreds: 1 }, senders: [{ sender: 11 }, { sender: 12 }, { sender: 13 }] },
  { ses: { sesCreds: 2 }, senders: [{ sender: 21 }, { sender: 22 }, { sender: 23 }] },
  { ses: { sesCreds: 3 }, senders: [{ sender: 31 }, { sender: 32 }, { sender: 33 }] }
];

const notificationTypes = ['Bounce', 'Complaint'];

const expected = [
  { "ses": { "sesCreds": 1 }, "sender": { "sender": 11 }, "notification": "Bounce" }, 
  { "ses": { "sesCreds": 1 }, "sender": { "sender": 11 }, "notification": "Complaint" }, 
  { "ses": { "sesCreds": 1 }, "sender": { "sender": 12 }, "notification": "Bounce" }, 
  { "ses": { "sesCreds": 1 }, "sender": { "sender": 12 }, "notification": "Complaint" }, 
  { "ses": { "sesCreds": 1 }, "sender": { "sender": 13 }, "notification": "Bounce" }, 
  { "ses": { "sesCreds": 1 }, "sender": { "sender": 13 }, "notification": "Complaint" }, 
  { "ses": { "sesCreds": 2 }, "sender": { "sender": 21 }, "notification": "Bounce" }, 
  { "ses": { "sesCreds": 2 }, "sender": { "sender": 21 }, "notification": "Complaint" }, 
  { "ses": { "sesCreds": 2 }, "sender": { "sender": 22 }, "notification": "Bounce" }, 
  { "ses": { "sesCreds": 2 }, "sender": { "sender": 22 }, "notification": "Complaint" }, 
  { "ses": { "sesCreds": 2 }, "sender": { "sender": 23 }, "notification": "Bounce" }, 
  { "ses": { "sesCreds": 2 }, "sender": { "sender": 23 }, "notification": "Complaint" }, 
  { "ses": { "sesCreds": 3 }, "sender": { "sender": 31 }, "notification": "Bounce" }, 
  { "ses": { "sesCreds": 3 }, "sender": { "sender": 31 }, "notification": "Complaint" }, 
  { "ses": { "sesCreds": 3 }, "sender": { "sender": 32 }, "notification": "Bounce" }, 
  { "ses": { "sesCreds": 3 }, "sender": { "sender": 32 }, "notification": "Complaint" }, 
  { "ses": { "sesCreds": 3 }, "sender": { "sender": 33 }, "notification": "Bounce" }, 
  { "ses": { "sesCreds": 3 }, "sender": { "sender": 33 }, "notification": "Complaint" }
];

const getSesSenderPairs = R.map(R.props(['ses', 'senders']));
const expandSesPerSenders = R.map(([first, ...second]) => R.xprod([first], ...second));
const expandSesSenderPairsPerNotification = R.chain((sesSenderPair) => R.xprod(sesSenderPair, notificationTypes));
const buildSesSenderNotificationStructure = R.map(([[ses, sender], notification]) => ({ ses, sender, notification }));

console.log(getSesSenderPairs(users))
const transform = R.pipe(
  getSesSenderPairs,
  expandSesPerSenders,
  expandSesSenderPairsPerNotification,
  buildSesSenderNotificationStructure
);

const finalResults = transform(users);

console.log(JSON.stringify(finalResults));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment