Repository navigation
Description
This is a bit of a project, but I think it could fix a lot of our problems. I basically think we should implement something similar to Common Lisp's setf, but with a more RQL-ish feel.
Here's a rough sketch of the API I'm thinking of:
obj = {:a => {:b => 1, :c => 2},
:d => [{:e => 3, :f => 4},
{:e => 5, :f => 6},
{:e => 7, :f => 8}]} obj.modify {|x|
[x[:a][:c].set(nil),
x[:d][0].remove]
}
res = {:a => {:b => 1, :c => nil},
:d => [{:e => 5, :f => 6},
{:e => 7, :f => 8}]} obj.modify {|x|
[x[:a].remove,
x[:g].set(nil),
x[:d].set(x[:d].map{|y| y.pluck(:e)})]
}
res = {:g => nil,
:d => [{:e => 3}, {:e => 5}, {:e => 7}]} obj[:d].modify {|x|
[x[0..1].remove,
x[2].modify {|y| [y[:e].remove, y[:g].set(9)]}]
}
res = [{:f => 8, :g => 9}] obj.modify {|x|
[x[:a][:c].set{|y| y*y},
x[:d].remove]
}
res = {:a => {:b => 1, :c => 4}} Basically, inside the body of a modify we'd convert our object and array dereferencing commands (both of which are implemented using [] in Ruby) into location specifiers, and then create a copy of the first argument to modify with the specified locations either set to a new value or removed. If the users sets a non-existent location, we create that location for them. (I think that this is an important feature for working with unstructured data, and also helps us keep the interface minimal by obviating the need for an additional create command.)
set and remove would only be defined inside of modify.
This interface is extremely tentative, and I'm not even sure that this is a good idea to begin with.
Activity
coffeemug commentedon May 28, 2013
👍
Moving to backlog for now, but we'll come back to this!
krzkrzkrz commentedon Sep 27, 2013
Any progress on this?
mlucy commentedon Sep 27, 2013
This probably won't make it into the next few versions (we're working on performance and stability improvements at the moment).
thelinuxlich commentedon Mar 12, 2018
@srh maybe this is already doable with .fold()?
srh commentedon Mar 26, 2018
Closing because this is pretty out there and is unlikely to happen.