These are chat archives for ramda/ramda

12th
Oct 2016
GÁBOR Áron Zsolt
@ashnur
Oct 12 2016 13:14
if I have a variable x and I would like to check if x has property y and this y is an array, and then use .includes on this array for z and then return the boolean result. I am stuck with the includes part, i don't really want to write a lamda anywhere :D
Michael Malura
@maluramichael
Oct 12 2016 13:16
@ashnur Would you mind posting some sample data and the result you want to archive?
GÁBOR Áron Zsolt
@ashnur
Oct 12 2016 13:19
const x = {y: ['z']}
const f = (x) => x != null && x.hasOwnProperty('y') && Array.isArray(x.y) && x.y.includes('z')
@maluramichael ^
and the result is a boolean, as i said before :)
GÁBOR Áron Zsolt
@ashnur
Oct 12 2016 13:33
:( seems like it's not good enough then
Ben Farr
@JaminFarr
Oct 12 2016 13:48
const f = pipe(prop('y'), ifElse(isArrayLike, contains('z'), F));
GÁBOR Áron Zsolt
@ashnur
Oct 12 2016 13:52
contains!!
i missed it 3 times...
Ben Farr
@JaminFarr
Oct 12 2016 13:59
:-)
Ben Farr
@JaminFarr
Oct 12 2016 14:16
This message was deleted
const isArrayAndContains = pipe(contains, both(isArrayLike));
const propContains = useWith(pipe, [prop, isArrayAndContains]);

const f = propContains('y', 'z');

f({y: ['z']}) // true
GÁBOR Áron Zsolt
@ashnur
Oct 12 2016 14:19
my problem is that i never know where to put these functions so I don't recreate them all the time :)
having a util.js is not fun
Keith Cirkel
@keithamus
Oct 12 2016 14:39
Hello all! I'm trying to use assoc in combination with a function (basically, I want a function which I can pass an object, and it returns the object with a boolean property flipped). I have
const flipComplete = R.assoc('complete', R.not)

flipComplete({ complete: true }) // should be { complete: false } but returns {}
Denis Stoyanov
@xgrommx
Oct 12 2016 14:40
@keithamus use evolve
Keith Cirkel
@keithamus
Oct 12 2016 14:41
Great, thanks @xgrommx!
Thats much nicer!
Denis Stoyanov
@xgrommx
Oct 12 2016 14:43
:+1:
Rick Medina
@rickmed
Oct 12 2016 15:40
hello! anyone have references to good/balanced articles about the benefits/drawbacks of fp?
Denis Stoyanov
@xgrommx
Oct 12 2016 15:41
@rickmed hi, I have some repo on my account, please take a look in
LeonineKing1199
@LeonineKing1199
Oct 12 2016 15:42
It can make code easier to reason about, it can also be overly abstracted and alienate people who wish to read your code
So it's just like every other paradigm :P
Rick Medina
@rickmed
Oct 12 2016 15:45
@xgrommx the awesome-fp right?
Denis Stoyanov
@xgrommx
Oct 12 2016 15:47
@rickmed yes
LeonineKing1199
@LeonineKing1199
Oct 12 2016 15:47
I think the largest benefits of FP come from higher-order functions and a push for immutability and a staunch resistance to relying on variables outside your current function scope.
Denis Stoyanov
@xgrommx
Oct 12 2016 15:47
@rickmed awesome-functional-programming
Rick Medina
@rickmed
Oct 12 2016 15:49
@xgrommx :+1:
Markus Pfundstein
@MarkusPfundstein
Oct 12 2016 17:49
drawback: once you go FP its hard to go back. And even harder to not enforce it on everyone around you lol
LeonineKing1199
@LeonineKing1199
Oct 12 2016 17:56
lol
Michael Malura
@maluramichael
Oct 12 2016 19:44
Is it possible to do my custom sort at the bottom with a single composition of the other methods or do i have to do it like i done?
const name = R.compose(
  R.head,
  R.values,
  R.pick(['name', 'title'])
);

const lowerCaseName = R.compose(
  R.toLower,
  name
);

const year = R.compose(
  (date)=> {
    var y = moment(date, 'YYYY-MM-DD').year();
    console.log(y);
    return y;
  },
  R.head,
  R.values,
  R.pick(['first_air_date', 'release_date'])
);

const sortByYearAscending = R.sortBy(year);
const sortByYearDescending = R.compose(
  R.reverse,
  R.sortBy(year)
);

const sortByNameAscending = R.sortBy(name);
const sortByNameDescending = R.compose(
  R.reverse,
  R.sortBy(name)
);

const sortByNameAndYear = R.compose(
  sortByYearDescending,
  sortByNameAscending
);

const customSort = (left, right)=> {
  const nameLeft = lowerCaseName(left); // inferno
  const nameRight = lowerCaseName(right); // walking dead
  const yearLeft = year(left); // 2016
  const yearRight = year(right); // 2010

  // year descending
  if (yearLeft > yearRight) return -1;
  if (yearLeft < yearRight) return 1;

  // name ascending
  if (nameLeft < nameRight) return -1;
  if (nameLeft > nameRight) return 1;

  return 0;
};

data = R.sort(customSort, data);
This message was deleted
This message was deleted
Later i want to be able to just switch between sortByYearAscending and sortByYearDescending and so on
LeonineKing1199
@LeonineKing1199
Oct 12 2016 21:32
Your custom sort looks good but I don't think the compose(reverse, sort) is very efficient. It's like you're sorting an entire array and then reversing it. You can probably just use complement or not to do the same thing. compose(not, predicate)