These are chat archives for ramda/ramda

6th
May 2018
Waqas Noor
@waqasnoor
May 06 2018 08:46
anyone here?
there is a piece of code which i want to optimize
Waqas Noor
@waqasnoor
May 06 2018 11:54
const input = [
  {
    "user": "1"
  },
  {
    "user": "2"
  },
  {
    "name": "Belgian Waffles"
  },
  {
    "price": "$5.95"
  },
  {
    "description": "Two of our famous Belgian Waffles with plenty of real maple syrup"
  },
  {
    "calories": "650"
  }
]

const arrayToObject = (inputArray) => {
  const valueObject = {};
  R.forEach((x) => {
    R.forEachObjIndexed((valueofNode, key) => {
      if (R.has(key)(valueObject)) {
        valueObject[key] = isArray(valueObject[key]) ? valueObject[key] : [valueObject[key]];
        valueObject[key].push(valueofNode);
      } else {
        valueObject[key] = valueofNode;
      }
    }, x);
  }, inputArray);
  return valueObject;
};
arrayToObject(input);

// const output = {
//   "user": [
//     "1",
//     "2"
//   ],
//   "name": "Belgian Waffles",
//   "price": "$5.95",
//   "description": "Two of our famous Belgian Waffles with plenty of real maple syrup",
//   "calories": "650"
// }
Is there any alternative way to achieve functionalty of arrayToObject?
Denis Stoyanov
@xgrommx
May 06 2018 12:21
@waqasnoor my small (maybe not nice solution) but it works
compose(
  mergeAll,
  map(data => data.length > 1 ? reduce(mergeWith((x, y) => Array.isArray(x) ? [...x, y] : [x, y]), {}, data) : head(data)),
  groupWith(useWith(useWith(equals, [compose(head, head), compose(head, head)]), [toPairs, toPairs]))
)(input)
Qquanwei
@Qquanwei
May 06 2018 13:00
hi guys. I want know how many programming style in javascript, like pointfree or chain , is there have anyelse styles?
Waqas Noor
@waqasnoor
May 06 2018 16:21

@Qquanwei the thing you want to study is Programming paradigm

Common programming paradigms include:
Immparative like OOP and procedural
declarative like functional

JavaScript is a multi-paradigm language, supporting imperative/procedural programming along with OOP (Object-Oriented Programming) and functional programming. JavaScript supports OOP with prototypal inheritance.