These are chat archives for ramda/ramda

31st
Aug 2016
James Forbes
@JAForbes
Aug 31 2016 09:37

@stereoit I keep coming back to this problem, I think it is a really interesting example of a weird JS style api solved via FP

So I tried again, this time I'm using groupBy to build a hash key'd by sex, and then merging that hash in with another hash that contains all values key'd with '*'. Then using propOr([]) to return either the values found in the hash or an empty array for any other input.

var f = useWith(
  propOr([]), [
    identity
    ,converge(
      merge
      ,[
        pipe(values, objOf('*'))
        ,groupBy( prop('sex') )
      ]
    )
  ] 
)

http://goo.gl/E2bwhE

I like this solution a lot more for a few reasons. 1. it supports indexing by the more than just m/f, so if you decide you want to represent sex as non binary, you can! 2. The majority of the function is building a data structure that models your problem. The useWith call is just a wrapper to give you the desired api. So you could separate those functions out and access the hash directly. 3. It's a lot simpler than using cond to build a predicate to use with filter like the other example.
Denis K.
@den1ska
Aug 31 2016 14:05

Hi guys, I'm trying to inject data from one object to another by a key. Here's an example of data:

const golden = {
  '8': {
    data: 'something'
  },
  '9': {
    data: 'nothing'
  }
};

const current = {
  golden: '8',
  defs: {}
};

I'd like to replace current.golden value with golden[current.golden]. I came up to the following solution:

const f = (curr, gold) => pipe(
  prop('golden'), 
  prop(__, gold), 
  assoc('golden', __, curr)
)(curr);

It works as expected but I think there could be a more elegant solution. I'd appreciate any help with it.

Ryan Zeigler
@rzeigler
Aug 31 2016 15:03
i think you may be attempting to over leverage composition
although your desired output is unclear to me, would you like current to equal
{
    golden: { data: 'something' }
}
?
i think I would probably just write that as
const inline = (curr, gold) =>  R.assoc('golden',  golden[curr.golden], curr)
Ryan Zeigler
@rzeigler
Aug 31 2016 15:12
@den1ska
Denis K.
@den1ska
Aug 31 2016 15:19

The desired output should be

{
    golden: { data: 'something' }
    defs: {}
}

This is exactly what your inline function does. I have to agree with you, my solution is so unclear and overcomplicated. I just tried to make a point-free composition and then failed. Only 2 weeks of experience with ramda and fp. :)
Thank you for making my doubts disappeared.

Ryan Zeigler
@rzeigler
Aug 31 2016 15:21
i think it was in the haskell cast, where theres like a phase everyone goes through were they think, 'i can make this pointfree' for everything
and you should really just think about what aids in readability
i think the on thing that I might do would be to invert the parameter ordering for currying
since R.curry((gold, curr) => ...
would allow you to first set a repository of data and then pick specific instances
so it extends via application a lot easier that way
Denis K.
@den1ska
Aug 31 2016 15:43
I was impressed by examples posted in this room so that overlooked a simple and readable solution which goes directly from the imperative world current.golden = golden[current.golden]. I think I should stick with inline version because I'm going to use it as a converging function and it works fine for me.