Skip to content

Proposal: Generic Object and Array Modification #895

Closed

Description

@mlucy

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

  1. ghost assigned on May 23, 2013
  2. coffeemug commented on May 28, 2013

    @coffeemug
    Contributor

    👍

    Moving to backlog for now, but we'll come back to this!

  3. krzkrzkrz commented on Sep 27, 2013

    @krzkrzkrz

    Any progress on this?

  4. mlucy commented on Sep 27, 2013

    @mlucy
    MemberAuthor

    This probably won't make it into the next few versions (we're working on performance and stability improvements at the moment).

  5. thelinuxlich commented on Mar 12, 2018

    @thelinuxlich

    @srh maybe this is already doable with .fold()?

  6. srh commented on Mar 26, 2018

    @srh
    Contributor

    Closing because this is pretty out there and is unlikely to happen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions