These are chat archives for ramda/ramda

29th
Jun 2018
Sergiu Ghenciu
@1024gs
Jun 29 2018 15:09
Hi guys,
Would be there any improvements on this?
const getAll = (id) => {
        return pipe(
          when(
            always(def(id)),
            filter(pipe(prop('foo'), includes(id)))
          ),
          map(omit('foo'))
        )(items);
      };
Sergiu Ghenciu
@1024gs
Jun 29 2018 15:19
The getAll function simply returns all the items (which are outside the function, not the best thing, but nevertheless) but if the id is provided the function returns a subset of items by filtering them.
Mike Lambert
@lax4mike
Jun 29 2018 15:21
what is def?
Sergiu Ghenciu
@1024gs
Jun 29 2018 15:29
const def = (x) => typeof x !== 'undefined';
def stands for isDefined basically
Einar Norðfjörð
@nordfjord
Jun 29 2018 16:07

filter(pipe(prop('foo'), includes(id)))
Is equivalent to

filter(propSatisfies(includes(id), 'foo'))

omit also takes a list of strings, not a string directly
Sergiu Ghenciu
@1024gs
Jun 29 2018 17:29
Please ignore Def and Omit. They are not Ramda functions.
perkee
@perkee
Jun 29 2018 19:09
Hello! ramda is intensely rad. In order to get more fluent with it I am doing some old CodeWars favorites with it. I've come to the problem I always come to where I don't know the word for what I'm trying to do, which is remove a variable from the beginning and end of a function (which doesn't really describe it)
// from a codewars kata, don't read if you don't want
// spoilers for the Matrix Determinant

const { drop, ifElse, propEq, path, prop, negate, identity, __, call, pipe, modulo, always, remove, slice, map, reduce, addIndex, converge, inc, concat, multiply, add } = require('ramda');
const odd = x => x & 1;
const removeCol = pipe(remove(__, 1), map)

const makeReducer = matrix => (sum, c, idx) => pipe(
  drop(1),   // drop the first row of matrix
  removeCol(idx),// remove this column from each row
  determinant,   // take determinant of submatrix
  multiply(c),   // multiply by value in this cell
  ifElse(always(odd(idx, 2)), negate, identity), //negate every other
  add(sum)       // add to sum
)(matrix);

const determinant = ifElse(
  propEq('length', 1),
  path([ 0, 0 ]),
  converge(call, [pipe(makeReducer, addIndex(reduce)(__, 0)), prop(0)])
)
// by way of comparison, this is the old function I replaced.
const wackDeterminant = m => m[1] ? m[0].reduce(
  (p,c,j) => p + Math.pow(-1, j) * c * wackDeterminant(m.slice(1).map(
    v => v.slice(0, j).concat(v.slice(j + 1)
  ))
), 0) : m[0][0];
since makeReducer takes matrix (a square nested array) and then calls the result of pipe with it, I'd think I could just remove it, but I can't for a reason I kind of understand but can't really articulate.
Mike Lambert
@lax4mike
Jun 29 2018 19:12
it's because makeReducer returns another function (waiting for sum, c, idx)
perkee
@perkee
Jun 29 2018 19:13
right, that.
Mike Lambert
@lax4mike
Jun 29 2018 19:13
const makeReducer = matrix => pipe( ... )(matrix) is the same as const makeReducer => pipe( ... )
it's called point-free
perkee
@perkee
Jun 29 2018 19:14
hmmm whenever I try that second one it doesn't actually work. I need to do more intermediate steps to see what's happening, but I end up with functions not numbers
Brad Compton (he/him)
@Bradcomp
Jun 29 2018 19:15
Swap the order of (sum, c, idx) and matrix
Mike Lambert
@lax4mike
Jun 29 2018 19:15
it won't work in your situation because of the (sum, c, idx) in there
Brad Compton (he/him)
@Bradcomp
Jun 29 2018 19:15
Then you can remove the explicit reference ot matrix as a parameter
perkee
@perkee
Jun 29 2018 19:16
@Bradcomp swap them in the order of the arrows? so (sum, c, idx) => matrix => pipe(…)(matrix)
@lax4mike tyvm for this cool new word! will dive in post haste.
also if you'd like me to make a codepen I'm happy to if that makes anything easier
Mike Lambert
@lax4mike
Jun 29 2018 19:20
be careful with coding in the pointfree style, sometimes it makes your code more concise, sometimes it makes it harder to follow. It's a judgement call
Brad Compton (he/him)
@Bradcomp
Jun 29 2018 19:22
Yes! (sum, c, idx) => matrix => pipe(…)(matrix) can be reduced to (sum, c, idx) => pipe(…)
perkee
@perkee
Jun 29 2018 19:22
right, roger. So is there actually a term for what I'm trying to do beyond just pointfree? Like what I'm trying to do to produce the reducer function?
@Bradcomp but it can't every time I try. Let me whip up a codepen to make it easier to see