ashaffer / mini-hamt

18 stars 0 forks source link

Question: nested structures #1

Open rstacruz opened 8 years ago

rstacruz commented 8 years ago

I know this is out of scope for mini-hamt, but what do you think would be the best way to implement nested structures?

I want to be able to do this:

var data = hamt.set(hamt.empty, 'user', { name: 'john' })
hamt.get('user.name') //=> 'john'

This is likely going to be a layer on top of mini-hamt, and I'd love to know your thoughts on how to go about with this.

ashaffer commented 8 years ago

Hmm...so the way i'd suggest doing that at the moment is just to nest HAMTs and provide a wrapper around them, maybe?

So:

const users = hamt.set(hamt.empty, 'user', hamt.set(hamt.empty, 'name', 'john'))

function get (path, obj) {
  return path.split('.').reduce((obj, key) => hamt.get(obj, key), obj)
}

Something like that maybe?

rstacruz commented 8 years ago

haha, interesting... I took a stab at a similar approach where it deserializes to:

data = nestedSet(data, 'user', { name: 'john', age: 30 })

// same as:
data = hamt.set(data, 'user', { keys: { name: true, age: true } })
data = hamt.set(data, 'user.name', { value: 'John' })
data = hamt.set(data, 'user.age', { value: 30 })

...a nested get() would then reconstruct the object using those reference points.

no idea what implementation would be faster though :) - proof of concept here.

ashaffer commented 8 years ago

Ah cool. Ya i'm not sure which is faster, and I think which one is better probably depends on what you want to use it for. One advantage of the approach I outlined I think is that it nests recursively in an obvious way.

rstacruz commented 8 years ago

it seems the nested structure you described is much faster!

ashaffer commented 8 years ago

Ah, cool. Should I close this issue then?