These are chat archives for ramda/ramda

4th
Feb 2018
joao@kapmug
@jay-jlm
Feb 04 2018 14:46
hey guys how do I safely access a path in an object, and by safely I mean it will create one at that location if it doesn't yet exist and return it to me?
I want to do something like this :
const deepProp = safeGet( myObj, ['a', 'b', 'c'])
and it will create 'c' at the specified locaiton even if a and b don't yet exist
isidorosp
@isidorosp
Feb 04 2018 14:50
maybe use pathOr?
joao@kapmug
@jay-jlm
Feb 04 2018 14:51
hmm... that doesnt create anything so to use that I'd have to do something like:
isidorosp
@isidorosp
Feb 04 2018 14:51
?
This message was deleted
what's the damn markdown for code :/
This message was deleted
joao@kapmug
@jay-jlm
Feb 04 2018 14:52
safeGet = (obj, path) =>  pathOr( path,  R.merge( obj, .. stuff) )
isidorosp
@isidorosp
Feb 04 2018 14:53
oh you want to CREATE something, yeah sorry
i read return
joao@kapmug
@jay-jlm
Feb 04 2018 14:53
triple backticks :)
isidorosp
@isidorosp
Feb 04 2018 14:54
yeah Im not sure how to do that
joao@kapmug
@jay-jlm
Feb 04 2018 14:54
ok so pathOr would work I just have to learn how to create that property there.. however I dont know if merge is inteligent enough to not overwrite stuff
isidorosp
@isidorosp
Feb 04 2018 14:54
it's a bit confusing though, why are you setting with a getter?
joao@kapmug
@jay-jlm
Feb 04 2018 14:54
for instance, say 'a' exists but not 'b'
isidorosp
@isidorosp
Feb 04 2018 14:55
but why would you awnt soemthign that retrieves the value of a.b to put in a blank object in b if nothing's there?
joao@kapmug
@jay-jlm
Feb 04 2018 14:55
the create function will be called but it the merge should not ovewrite the existing 'a' property, instead it should just add 'b' to exiting 'a'
isidorosp
@isidorosp
Feb 04 2018 14:56
yeah I get that, I just dont' get why :P
you probably need to use IfElse if you want to do it purely with Ramda
joao@kapmug
@jay-jlm
Feb 04 2018 14:58
I'm creating a 'memory' slot to keep easy access to my game state in each agent
say agent.memory.world.house.frontDoor
i dont want to keep them as lists because I need filtering and it's got to be super fast, access to this must be at the milisecond level (hundreds of agents will be on the screen at any given point in time)
so i'm keeping this as a hashmap (aka objects in JS)
so if I need to throw stuff into this , I it has to Just Work ™ , regarless of the the path already existing or not
so if the agent has not 'house' in memory.world.. it should still work
isidorosp
@isidorosp
Feb 04 2018 15:03
i see
joao@kapmug
@jay-jlm
Feb 04 2018 15:06
but I dont have the path as an object to pass to the merge... I dont have { b: { c: } } as a hardcoded value, these are string keys
so I will probably have to create a function that can build a path from string keys :-(
can Ramda also help me there?
isidorosp
@isidorosp
Feb 04 2018 15:10
perhaps, I'm still really new so I don't know much
joao@kapmug
@jay-jlm
Feb 04 2018 15:11
sure no prob mate. I'm also still learning the ropes ;-)
plus I just remembered that merge will return me a brand new object (like most ramda functions) and not add it to my memory object
so I guess I cannot use merge there :/
joao@kapmug
@jay-jlm
Feb 04 2018 15:12
thanks will check it out!
isidorosp
@isidorosp
Feb 04 2018 15:12
yeah ramda is not about altering the original object
joao@kapmug
@jay-jlm
Feb 04 2018 15:14
wait, that stackoverflow question there is just about accessing a path from a string
that R.path already does for you... just use R.split
isidorosp
@isidorosp
Feb 04 2018 15:15
that questionw as for the part about "building a path from string keys"
but yeah using R.split woudl work to do that
joao@kapmug
@jay-jlm
Feb 04 2018 15:16
buiding an access path.. not building nested properties inside of an object
these are two completely separate things
isidorosp
@isidorosp
Feb 04 2018 15:17
sure
different contexts, but we were discussing R.path and path
so I understood path to mean that :P
have to make sure that doesnt overwrite existing data thou.. that's the most tricky part
isidorosp
@isidorosp
Feb 04 2018 15:19
yeah that looks like more what you want
joao@kapmug
@jay-jlm
Feb 04 2018 15:20
it's easy to create a.b.c.d... from a blank stale object, however if a.b already exists it should just merge with b, instead of overwriting 'b'
but I dont want to use a lib for that.. I'm sure I can create this createNested function with at most 3 lines of ramda code, even if I have to mutate the object... which ramda won't help me with
give me 10 min ;-)
isidorosp
@isidorosp
Feb 04 2018 15:23
gl
joao@kapmug
@jay-jlm
Feb 04 2018 15:25
btw, this propably "almost" does what I need, except that it doesnt mutate the original object: https://github.com/ramda/ramda/wiki/Cookbook#mergepath
hm... gotta go home now. back later. thanks for the chat ;-)
joao@kapmug
@jay-jlm
Feb 04 2018 17:02
@tommmyy will take a look, but will this mutate the original object? because that's what i need
Guys, here's how I did it. If there's a way to make it better let me know, (just keep in mind I need to mutate):
const myObj = { a : { b: { c: 1 } }  }
const getOrCreate = (acc, k) => {  // needs to mutate so doesn't use Ramda
  acc[k] ? acc[k] : acc[k] = {}
  return acc[k]
}
const getOrCreatePath = (obj, pathArr) => {
  if(!R.path(pathArr, myObj)) R.reduce(getOrCreate, myObj, pathArr)
  return R.path(pathArr, myObj)
}
getOrCreatePath(myObj, R.split('.', 'a.b.d'))
joao@kapmug
@jay-jlm
Feb 04 2018 18:40
hey can Ramda help with matrix intersection?
well let me put it this way.. Say I have 2 lists of [x, y] coordinates .. I need to know the intersection
maybe just normal R.intersection will work