These are chat archives for ramda/ramda

23rd
Jan 2017
Sudarsan Balaji
@artfuldev
Jan 23 2017 05:14
@mikiubo can you describe using code/notations?
eg., do you want
fn2(c)(fn1(c))
or fn2(fn1(c))(c)
or fn2(fn1(c))?
where c is your last param?
Vladimir Starkov
@iamstarkov
Jan 23 2017 14:00
hi everybody
i have this function
const splitKeywords = R.unary(R.pipe(
  R.split(','),
  R.map(R.trim),  <- Бежим по массиву ррраз
  R.reject(R.isEmpty) <- Бежим по массиву два
));
here i have two loops
while in non-fp solution, you can achieve the same result with one loop
David Chambers
@davidchambers
Jan 23 2017 14:03
You can use chain rather than map. ;)
Vladimir Starkov
@iamstarkov
Jan 23 2017 14:03
can you help me?
const splitKeywords = str => {
  const splitted = str.split(',');
  const result = [];
  for (let i in splitted) {
    const trimmed = i.trim();
    if (trimmmed !== '') {
      result.push(i);
    }
  }
  return result;
}
this is non-fp variant
David Chambers
@davidchambers
Jan 23 2017 14:08
@ram-bot
R.chain(R.pipe(R.trim, R.of, R.reject(R.isEmpty)), R.split(',', 'foo, bar, baz, '))
ram-bot
@ram-bot
Jan 23 2017 14:08
[ 'foo', 'bar', 'baz' ]
Vladimir Starkov
@iamstarkov
Jan 23 2017 14:11
@davidchambers thanks
can we get around of & reject with smth like when(isEmpty, always(null) ?
yes, we can https://goo.gl/dGHgiR
yay
thanks
David Chambers
@davidchambers
Jan 23 2017 14:13
When chaining over an array the function must return an array. You can return [] to append nothing to the resulting array, or [R.trim(s)] to append one string to the resulting array.
There's a type error in your code, @iamstarkov, since you're not returning an array in one of the branches. Ramda doesn't care, of course. ;)
Vladimir Starkov
@iamstarkov
Jan 23 2017 14:19
got it
David Chambers
@davidchambers
Jan 23 2017 14:22

Valid:

S.chain(S.compose(s => s === '' ? [] : [s], S.trim), S.splitOn(',', ',, ,, as, ,asd'));
// => ['as', 'asd']

Invalid:

S.chain(S.compose(s => s === '' ? [] : s, S.trim), S.splitOn(',', ',, ,, as, ,asd'));
// ! TypeError: Type-variable constraint violation
//
//   chain :: Chain m => (a -> m b) -> m a -> m b
//                             ^^^     ^^^
//                              1       2
//
//   1)  [] :: Array ???
//       [] :: Array ???
//       [] :: Array ???
//       [] :: Array ???
//       "as" :: String
//
//   2)  ["", "", " ", "", " as", " ", "asd"] :: Array String
//
//   Since there is no type of which all the above values are members, the type-variable constraint has been violated.
Vladimir Starkov
@iamstarkov
Jan 23 2017 14:23
fixed it here https://goo.gl/OsDIjA
David Chambers
@davidchambers
Jan 23 2017 14:23
It's up to you to decide whether a well-typed program is important. So long as you have good test coverage relying on implementation details of Ramda shouldn't be too dangerous.
The fix looks good. I find unconditionally wrapping the value and using filter a bit clearer than using ifElse, though.
Vladimir Starkov
@iamstarkov
Jan 23 2017 14:26
i thought one loop for each element in collection is kinda bad
is it the case for transduce?
David Chambers
@davidchambers
Jan 23 2017 14:29
Transducers would allow you to write code that looks similar to your first version but with the execution order of your second version.
Anton Strömkvist
@ahstro
Jan 23 2017 16:16
How would replicate Array.prototype.sort's default sort for strings in Ramda without writing a long diff function that comparse Unicode code points?
mikiubo
@mikiubo
Jan 23 2017 17:14

@artfuldev e.g.

str->str->int->str
const a = R.curry( (t,n,v) => t+" "+n+"="+v );

int->int
const inc = n=>n+1

I want c that inc v in a

then d=c("var"), e=c("const") f=d("ciao") ecc...

Sudarsan Balaji
@artfuldev
Jan 23 2017 17:49
@mikiubo
so you want,
var x = 1
const x = 1
ciao x = 1
etc as outputs?
or,
var x = 1
const x = 2
ciao x = 3
etc as outputs?
Vladimir Starkov
@iamstarkov
Jan 23 2017 20:15
@davidchambers on the related note about R.pipe(split, map, reject).
i ended up with reduce
because i think transducers will cause 1 loop over N items and then N over 1 item loops, where N is array after split length
am i right?
Denis Stoyanov
@xgrommx
Jan 23 2017 20:20
@iamstarkov ramda has transducers
David Chambers
@davidchambers
Jan 23 2017 20:21
If performance is such a concern you should probably stick to your imperative version, @iamstarkov. :stuck_out_tongue_winking_eye:
Vladimir Starkov
@iamstarkov
Jan 23 2017 20:22
@davidchambers its not a concern for me, its a concern for friend of mine who is into both fp and c++
@xgrommx yes, dont transducers will cause 1 loop over N items, and 2*N loops over 1 item?
pseudo code pipe(split, transduce(pipe(map, reject)))
fent
@fent
Jan 23 2017 22:46
Does ramda support accessing props that are behind a function? Besides giving it a custom function to get the value of it?