These are chat archives for ramda/ramda

28th
Jul 2017
Julien Bonnin
@JulienBonnin-ABTasty
Jul 28 2017 13:27
Hello :) I am trying to generate multiple versions of an object and this time I don't know where to start. What i want to do is a kind of "unmerge" using an array of key I want to "unmerge"
http://ramdajs.com/repl/?v=0.24.1#?const%20obj%20%3D%20%7B%0A%20%20languages%3A%20%5B%27en%27%2C%20%27fr%27%5D%2C%0A%20%20properties%3A%20%7B%0A%20%20%20%20name%3A%20%7B%0A%20%20%20%20%20%20fr%3A%20%27mon%20nom%27%2C%0A%20%20%20%20%20%20en%3A%20%27my%20name%27%0A%20%20%20%20%7D%2C%0A%20%20%20%20age%3A%2010%2C%0A%20%20%20%20description%3A%20%7B%0A%20%20%20%20%20%20short%3A%20%7B%0A%20%20%20%20%20%20%20%20fr%3A%20%27une%20description%20courte%27%2C%0A%20%20%20%20%20%20%20%20en%3A%20%27a%20short%20description%27%0A%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20long%3A%20%7B%0A%20%20%20%20%20%20%20%20fr%3A%20%27une%20description%20longue%27%2C%0A%20%20%20%20%20%20%20%20en%3A%20%27a%20long%20description%27%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%3B%0A%0A%2F%2F%20THE%20OUTPUT%20I%20WANT%20%3A%0A%0Aconst%20output%20%3D%20%7B%0A%20%20en%3A%20%7B%0A%20%20%20%20properties%3A%20%7B%0A%20%20%20%20%20%20name%3A%20%27my%20name%27%2C%0A%20%20%20%20%20%20age%3A%2010%2C%0A%20%20%20%20%20%20description%3A%20%7B%0A%20%20%20%20%20%20%20%20short%3A%20%27a%20short%20description%27%2C%0A%20%20%20%20%20%20%20%20long%3A%20%27a%20long%20description%27%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%7D%2C%0A%20%20fr%3A%20%7B%0A%20%20%20%20properties%3A%20%7B%0A%20%20%20%20%20%20name%3A%20%27mon%20nom%27%2C%0A%20%20%20%20%20%20age%3A%2010%2C%0A%20%20%20%20%20%20description%3A%20%7B%0A%20%20%20%20%20%20%20%20short%3A%20%27une%20description%20courte%27%2C%0A%20%20%20%20%20%20%20%20long%3A%20%27une%20description%20longue%27%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%3B
This example will be better to show you what I am trying to do.
Michael Rosata
@mrosata
Jul 28 2017 14:00
@JulienBonnin-ABTasty That seems like a complex way to get something simple. (I recognize the complexities because I myself usually make things overly complicated). It feels like you want a hybrid invert/groupBy or recursive filtering.. but it might be better easier to code and read something along these lines:
// Create a function to get the paths to properties on an object based on language
const getLangPaths = (lang) => ({
  name: ['properties', 'name', lang],
  age: ['properties', 'age'],
  description: {
    short: ['properties', 'description', 'short', lang],
    long: ['properties', 'description', 'long', lang]
  },
})

// Get the paths
const texts = {
  en: getLangPaths('en'),
  fr: getLangPaths('fr'),
}

// Read a path
path(texts.en.description.short, obj)
path(texts.fr.description.short, obj)
@JulienBonnin-ABTasty or with lenses:
// With Lenses
const getLangLenses = (lang) => ({
  name: lensPath(['properties', 'name', lang]),
  age: lensPath(['properties', 'age']),
  description: {
    short: lensPath(['properties', 'description', 'short', lang]),
    long: lensPath(['properties', 'description', 'long', lang])
  },
})

// Build the lens paths
const textsL = getLangLenses('fr')
// getters
view(textsL.name, obj)
// setters
set(textsL.description.long, 'nom nom nom nom nom', obj)
Julien Bonnin
@JulienBonnin-ABTasty
Jul 28 2017 14:41
Thanks :) unfortunately, the format of the entry object can be very different from this one and the keys can have any name. I can't know in advance the format. All I know is the array of languages I want to support, and that I have an object where some nested properties needs to be translated. But it gave me a few ideas to manage to do it :)
Michael Rosata
@mrosata
Jul 28 2017 14:53
:)
@JulienBonnin-ABTasty let me know what you land on, I'd be interested to know the end result
Julien Bonnin
@JulienBonnin-ABTasty
Jul 28 2017 14:54
I am trying to get an array of all pathes possible inside any object first
then I order those path by nested level
reject the pathes with the language I am not interested in, then rebuilt the object with the filtered pathes
Michael Rosata
@mrosata
Jul 28 2017 14:59
:) sounds complex
could have getters that check if a "en" or "fr" property exists and if so grab it
const getter = lang => p => either(path(append(lang, p)), path(p))
const en = getter('en')
const fr = getter('fr')
en(['properties', 'age'])(obj) // properties.age === 10
fr(['properties', 'age'])(obj) // properties.age === 10
fr(['properties', 'description', 'short'])(obj) // properties.description.short.fr === 'une description courte'
Aaron Mc Adam
@aaronmcadam
Jul 28 2017 15:10
is there a function like pick that takes a scalar value? I always forget to pass an array
mpontus
@mpontus
Jul 28 2017 15:17
@aaronmcadam unapply(pick)?
Aaron Mc Adam
@aaronmcadam
Jul 28 2017 15:17
ah, of course :)
thanks @mpontus, now I have to think of a good name for that hah
objProp :fire:
mpontus
@mpontus
Jul 28 2017 15:18
Is there a function of following form: subject -> fn -> fn(subject)?
@aaronmcadam Are you sure you're not looking for prop?
Aaron Mc Adam
@aaronmcadam
Jul 28 2017 15:19
yes, I'm sure.
I want to build an object from the prop
mpontus
@mpontus
Jul 28 2017 15:19
Ah, right.
Aaron Mc Adam
@aaronmcadam
Jul 28 2017 15:20
yeah, I want pick semantics, so unapply is right :)
Aaron Mc Adam
@aaronmcadam
Jul 28 2017 15:27
Is there a short way to set the key to use for pick?
e.g. my data structure is a.b, but I'd like to use c for the key for b's value
I'm guessing objOf is my best option
mpontus
@mpontus
Jul 28 2017 15:55

Can somebody suggest a way to replace this:

const [col, row] = [
  Math.floor((x - minX) / spanX),
  Math.floor((y - minY) / spanY),
];

with something like this:

const [col, row] = [
  [x, minX, spanX],
  [y, minY, spanY],
].map(
  ...
).map(Math.floor);
Brad Compton (he/him)
@Bradcomp
Jul 28 2017 16:03
@mpontus Something like this?
mpontus
@mpontus
Jul 28 2017 16:04
@Bradcomp Interesting. It's not how I imagined it to be, but this looks good.
I'm still trying to get the hang of functional style, and how can it improve readability of my code. Sometimes I may be going into the wrong direction.
Brad Compton (he/him)
@Bradcomp
Jul 28 2017 16:05
There are a lot of ways you could break it up... This seemed to make sense to me

Is there a function of following form: subject -> fn -> fn(subject)?

What you're looking for is the Thrush combinator. I don't think Ramda has it right now, there might be an issue for it open

ramda/ramda#1846
ramda/ramda#1847
mpontus
@mpontus
Jul 28 2017 16:10
@Bradcomp Thank you! I've been wondering for ages if this concept has a name.
Brad Compton (he/him)
@Bradcomp
Jul 28 2017 16:12
:bowtie:
@ram-bot
applySpec({c: prop('b')})({b: 'hi'})
ram-bot
@ram-bot
Jul 28 2017 16:13
{ c: 'hi' }
Brad Compton (he/him)
@Bradcomp
Jul 28 2017 16:13
@aaronmcadam :point_up: like that?
Aaron Mc Adam
@aaronmcadam
Jul 28 2017 16:14
ooo I like that @Bradcomp
thanks!!
Brad Compton (he/him)
@Bradcomp
Jul 28 2017 16:14
:+1:
Denis Stoyanov
@xgrommx
Jul 28 2017 17:30
@Bradcomp T = flip(identity)
Brad Compton (he/him)
@Bradcomp
Jul 28 2017 21:12
@xgrommx :scream: cool!
Doesn't work in Ramda though
Denis Stoyanov
@xgrommx
Jul 28 2017 23:38

@Bradcomp lol!

const C = x => y => z => x(z)(y)

C(identity)('test')(toUpper)

Ramda...

@Bradcomp works! flip(call)('test')(toUpper)