These are chat archives for ramda/ramda

5th
Oct 2018
Andreas Herd
@mendrik
Oct 05 2018 06:08
what would be the easiest way to get a list of properties from two property-equal objects where the values differ?
Pedro Ávila
@avilapedro
Oct 05 2018 06:58

@mendrik Here's how I achieved it, not sure if it's the easiest way though:

const a = { foo: 50, bar: 80, baz: 42, qux: 13 }
const b = { foo: 40, bar: 80, baz: 42, qux: 73 }

const differs = complement (equals)

pipe (
  mergeWith (pair),
  map (apply (differs)),
  filter (identity),
  keys
) (a, b)
// => ["foo", "qux"]

REPL: https://goo.gl/SKs4Hr

BTW, how would you name such a function?
Riku Tiira
@rikutiira
Oct 05 2018 07:16
Hello, quick question! I noticed that flow-typed has defintions for Ramda, but how does flowtype work with R.curry? From what I had understood, flowtype and currying don’t go well together.
Andreas Herd
@mendrik
Oct 05 2018 07:21
@pedro thanks! looks nice let me check if it works, not sure about the name maybe propertiesByDifference
Riku Tiira
@rikutiira
Oct 05 2018 07:31
Seems like curry is typed up to arity of 6 if I understand flowtyped correctly, but curryN cannot be typed (I tried to make arbitrary arity curry in flowtype REPL myself and couldn’t get it to work)
Kurt Milam
@kurtmilam
Oct 05 2018 07:38
@avilapedro yeah, I have had to deal with the use case (and similar use cases) that you describe. I generalized the problem from 'how to deal with an array of objects where each object has a unique key' to 'how to deal with an arbitrary collection of objects where each object has a unique key'. Using an object rather than an array to represent the collection is a lot easier to work with in a number of different situations.

For instance, say you're caching some objects and want to update the cache. You want to add new items to the cache where necessary and replace others where necessary:

const cache = {a: {id: 'a', val: 1}, b: {id: 'b', val: 2}}
const newCache = { ...cache, ...{b: {id: 'b', val: 3}, c: {id: 'c', val: 4}} }
// => {a: {id: 'a', val: 1}, b: {id: 'b', val: 3}, c: {id: 'c', val: 4}}}

Removal of expired items is also ergonomic but requires a little extra work. For that, I get a full list of ids that should be in the cache and generate a new cache that only contains those ids.

Kurt Milam
@kurtmilam
Oct 05 2018 07:46
Later on, I learned that Dan Abramov recommends the identical pattern. I don't always agree with Dan Abramov (creator of redux), but I do in this case :D
Pedro Ávila
@avilapedro
Oct 05 2018 16:09

@kurtmilam yeah, I've read that in Redux docs: https://redux.js.org/basics/reducers#note-on-relationships

I even built a small app with such kind of state (normalized and flattened). It was a good experience, but I found to be hard to manage the state, lenses helped me a lot on that.