These are chat archives for ramda/ramda

19th
Sep 2017
Zehua Liu
@zehua
Sep 19 2017 03:08
guys, it looks like the online repl does not support generator syntax?
Adrian Skierniewski
@AdrianSkierniewski
Sep 19 2017 08:30
Hi, is there a easy way to pass multiple arguments to next function in pipe?
Adrian Skierniewski
@AdrianSkierniewski
Sep 19 2017 08:39
so here is my dirty function that decorates some promises to build single result object, but sometimes those functions needs more that one argument. I could pass them directly during "decoration" but I don't know how I could get those args from result object. extractor function needs to return more than one arg and pass it to R.apply(fun, rest). I see that this doesn't looks nice and I'd probably refactor this, but I'm just trying to see what is possibile to do for this case
const makeWrappedVersionWithArg = (returnKey, extractor, fun) => (...rest) => {
  return R.pipe(
    R.chain(R.assoc(returnKey))(R.pipe(extractor, R.apply(fun, rest))),
    Bluebird.props
  )
}
Kurt Milam
@kurtmilam
Sep 19 2017 08:54
@AdrianSkierniewski Would using R.props as your extractor to pull more than one property from the object help?
Adrian Skierniewski
@AdrianSkierniewski
Sep 19 2017 08:57
how can I pull more that one property using R.props ? I was using other function like pick but it was returning singe object with two properties instead two objects.
eventually I need R.apply(fun, rest)(first)(second) but I've got R.apply(fun, rest)({first,second})
Kurt Milam
@kurtmilam
Sep 19 2017 08:58
R.props( [ 'a', 'b' ], { a: 1, b: 2, c: 3 } ) //-> [ 1, 2 ]
Then use R.apply(R.apply(fun, rest)).
That would call fun like this: fun( ...rest )( ...extractor )
So, if rest = [ 1, 2 ] and extractor returns [ 3, 4 ], the call would be fun( 1, 2 )( 3, 4 )
Adrian Skierniewski
@AdrianSkierniewski
Sep 19 2017 09:02
nice
I'll try that
Kurt Milam
@kurtmilam
Sep 19 2017 09:02
:+1:

This would probably also work:

R.chain(R.assoc(returnKey))(R.pipe(extractor, R.concat(rest), R.apply(fun)))

Then, if rest = [ 1, 2 ] and extractor returns [ 3, 4 ], the call would be fun( 1, 2, 3, 4 ).

Adrian Skierniewski
@AdrianSkierniewski
Sep 19 2017 09:09
I'm getting undefined when using R.prop with array
oh
now I get it
it's props
I was blind
version with R.concat looks cleaner
Kurt Milam
@kurtmilam
Sep 19 2017 09:12
If your extractor always returns an array, you could probably just have one makeWrappedVersion that works whether ...rest is empty or not.
Adrian Skierniewski
@AdrianSkierniewski
Sep 19 2017 09:13
you're a magician for me @kurtmilam
so simply solutions for all those problems
Kurt Milam
@kurtmilam
Sep 19 2017 09:13
So, you could use R.props(['a']) in place of R.prop('a').
Adrian Skierniewski
@AdrianSkierniewski
Sep 19 2017 09:13
yes now I see it
Kurt Milam
@kurtmilam
Sep 19 2017 09:14
I'm the nerd, in this case :)
But yes, the more you work with these concepts, the easier it becomes to find a simple solution to sometimes complex problems.
Adrian Skierniewski
@AdrianSkierniewski
Sep 19 2017 09:15
;D
I don't see those patterns yet and I'm still learning all those ramda functions how to combine them
Kurt Milam
@kurtmilam
Sep 19 2017 09:17
That's OK, it took me a while to figure everything out.
Fortunately, I think you've picked a good project to start learning FP with. Some people pick less appropriate projects (e.g. refactoring some imperative code that sometimes isn't the best target).
Adrian Skierniewski
@AdrianSkierniewski
Sep 19 2017 09:19
it's probably better to start from scratch
Kurt Milam
@kurtmilam
Sep 19 2017 09:19
It often is, yes.
Adrian Skierniewski
@AdrianSkierniewski
Sep 19 2017 09:22
the problem when I try to use only one version of that makeWrappedVersion function is the way how I'm passing those functions further
makeWrappedVersionWithArg have one additional nested closure with ...rest
Kurt Milam
@kurtmilam
Sep 19 2017 09:22
Quite possibly - I'm going off of memory here to a large extent, so I may be forgetting something.
Adrian Skierniewski
@AdrianSkierniewski
Sep 19 2017 09:23
const setup = R.pipeP(setUp.user, setUp.account, setUp.subscriptions([64, 11]))({})
  .then(result => console.log('RESULTS', result))
  .catch(err => console.log('ERR', err))
this is how it look right now
as you can see those setUp methods without args not need to be invoked
I can just pass reference
but when I use withArgs I'd need to run every single function even without arguments
Kurt Milam
@kurtmilam
Sep 19 2017 09:25
Right. I think you could make it work, but it's probably better to leave it as it is.
The solution with a specialized fn for when you need to pass in additional args is probably clearer.
hkrutzer
@hkrutzer
Sep 19 2017 13:36
Can I execute a function on a part of a list? like R.inc, [1,1,1,1,1], and range 1-3 -> [1,2,2,2,1]
Denis Stoyanov
@xgrommx
Sep 19 2017 13:59
Strange, could u please explain what u want
Adam Szaraniec
@mimol91
Sep 19 2017 14:04
I think he wants to increase only specific keys in array (which are in range from 1, to 3)
@hkrutzer I m quire sure that its not possible out of the box,
Denis Stoyanov
@xgrommx
Sep 19 2017 14:07
addIndex(map)((v,i) => [1,2,3]. includes (I) ? Inc(v) : v)
Sorry from phone
Ramda's includes is contains
Kurt Milam
@kurtmilam
Sep 19 2017 14:11
@hkrutzer Here's a reusable solution:
const mapIndexes = 
  fn => idxs => xs =>
    { const wrapFn =
        ( x, i ) =>
          contains( i )( idxs )
            ? fn( x )
            : x
      return addIndex( map )( wrapFn )( xs )
    }

mapIndexes( R.inc )
          ( range( 1 )( 4 ) )
          ( [ 1, 1, 1, 1, 1 ] )

// -> [1, 2, 2, 2, 1]
It's not point-free, as you can see, but it's a little more flexible, as it just takes an array of indexes to which the fn should be applied. You can build that array with R.range or some other method.
I'd like to know whether there's a cleaner, perhaps traversal-based solution that's also reusable.
Adam Szaraniec
@mimol91
Sep 19 2017 14:14
Amazing, I have to learn how to think, how I can make more generic functions
Denis Stoyanov
@xgrommx
Sep 19 2017 14:14
Hm) almost as my idea
Adam Szaraniec
@mimol91
Sep 19 2017 14:16
Yup, I guess it will take a lot of time when I would be able to write similar functions, and twice more to be able to write it 'on paper' like you :P
hkrutzer
@hkrutzer
Sep 19 2017 14:18
Thanks guys :)
Philipp Wille
@Yord
Sep 19 2017 14:20
const f = o(reduceRight, adjust)

f(inc)([1,1,1,1,1])(range(1, 4)) // [1,2,2,2,1]
Kurt Milam
@kurtmilam
Sep 19 2017 14:21
@hkrutzer I added one based on lenses:
const mapIndexes2 = 
  fn => idxs => xs =>
    { 
      const wrapFn =
        ( acc, x ) =>
          over( lensIndex( x ) )
              ( fn )
              ( acc )
      return reduce( wrapFn )( xs )( idxs )
    }

mapIndexes2( R.inc )
           ( range( 1 )( 4 ) )
           ( [ 1, 1, 1, 1, 1 ] )

// -> [1, 2, 2, 2, 1]
@Yord nice!
Stefano Vozza
@svozza
Sep 19 2017 15:07
very nice!
junsnow
@awaitsnow_twitter
Sep 19 2017 16:13

import R from "ramda"; if (movies[index]) { R.mergeDeepWith(R.concat, item, movies[index]); }
Unhandled Rejection (TypeError): 4455 does not have a method named "concat" or "fantasy-land/concat"

It's the same with
import { concat, mergeDeepWith } from "ramda";

junsnow
@awaitsnow_twitter
Sep 19 2017 16:25
Brad Compton (he/him)
@Bradcomp
Sep 19 2017 16:37
@ram-bot
4455.concat(1234);
ram-bot
@ram-bot
Sep 19 2017 16:37
Invalid or unexpected token
Brad Compton (he/him)
@Bradcomp
Sep 19 2017 16:38
@ram-bot
var a = 4455;

a.concat(1234);
ram-bot
@ram-bot
Sep 19 2017 16:38
a.concat is not a function
Brad Compton (he/him)
@Bradcomp
Sep 19 2017 16:41
When you use mergeDeepWith it will try to apply your merging function to all the properties that exist on both objects you are merging. Because Numbers don't have a concat method it will throw.
Check this though
I wouldn't recommend modifying prototypes though
Denis Stoyanov
@xgrommx
Sep 19 2017 16:44
ugly idea extend prototype of native objects
Brad Compton (he/him)
@Bradcomp
Sep 19 2017 16:46
I wouldn't recommend modifying prototypes though
Was a POC to show that it was simply a matter of trying to concat things that don't have a concat method
Also, ugly but fun :stuck_out_tongue: . You can do some pretty cool magic, but I would never do it in production code
Kurt Milam
@kurtmilam
Sep 19 2017 21:11
Another solution to the map matching indexes question, this time using partial.lenses:
const mapIndexes =
  o( L.modify )
   ( apply( L.seq ) )

mapIndexes( range( 1 )( 4 ) )
          ( inc )
          ( [ 1, 1, 1, 1, 1 ] )

// -> [ 1, 2, 2, 2, 1 ]