Skip to content

Proposal: pattern-matching pluck #889

Closed

Description

@mlucy

I think that in addition to attribute names, we should let people pass patterns to pluck specified as objects, with the following semantics:

  • If an attribute maps to true, include that attribute.
  • If an attribute maps to an object, recurse into that object.
  • If an attribute maps to an array, recurse into that array.
  • If an attribute maps to NULL or false, exclude it. (This is useful because it allows people to construct plucks that behave differently based on a piece of code they execute.)
  • Any other value for an attribute is an error.

For example:

> obj = {:a => {:b => 1, :c => 2}, 
         :d => [{:e => 1, :f => 2}, 
                {:e => 3, :f => 4}]}

> obj.pluck({:a => {:b => true}})
{:a => {:b => 1}}

> obj.pluck({:a => true, :d => [{:e => true}]})
{:a => {:b => 1, :c => 2}, :d => [{:e => 1}, {:e => 3}]}

> obj.pluck({:a => true, :d => [{:e => true, :f => false}]})
{:a => {:b => 1, :c => 2}, :d => [{:e => 1}, {:e => 3}]}

> obj.pluck({:a => obj[:a].do{|a| a[:b].eq(1)}, :d => false})
{:a => {:b => 1, :c => 2}}

> obj.pluck({:a => obj[:a].do{|a| a[:b].eq(2)}, :d => false})
{}

Activity

  1. ghost assigned on May 22, 2013
  2. coffeemug commented on Jun 11, 2013

    @coffeemug
    Contributor

    Moving to 1.7. A couple of comments on this proposal:

    • This syntax should also work for with_fields and has_fields
    • I think we should be able to say things like obj.pluck({:a => {:b => true}}, :c)
  3. mlucy commented on Jun 11, 2013

    @mlucy
    MemberAuthor

    I agree on both.

  4. neumino commented on Jun 11, 2013

    @neumino
    Member

    Are we keeping the current behavior?
    Can I still pass a string or multiple strings to pluck (first level fields)?

    If yes, I would be cool with the proposal.

  5. coffeemug commented on Jun 11, 2013

    @coffeemug
    Contributor

    Yes, we're also keeping the current behavior (at least I always assumed that's part of this proposal).

  6. neumino commented on Jun 11, 2013

    @neumino
    Member

    Sounds good to me then.

  7. jdoliner commented on Jun 11, 2013

    @jdoliner
    Contributor

    So it actually seems like there's now a huge number of ways to perform a pluck and I fear it may wind up a bit confusing. I'm having a lot of trouble wrapping my head around all the different behaviors. A few things that seem weird to me:

    • mapping to true to mean "include this" seems really weird. In particular it's weird that at the top level I say:
    obj.pluck("foo", "bar")
    

    but if it's a level down I say:

    obj.pluck({"foo" : {"foo" : True, "bar" : True}})
    

    I'd much prefer if the syntax looked like this:

    obj.pluck({"foo" : ["foo", "bar"]})
    

    it's more concise and it means nested pluck looks very similar to unnested pluck. In fact this might be an argument for replacing variadic pluck with pluck on an array (i.e pluck(["foo", "bar"]) instead of pluck("foo", "bar")) which would make them look identical.

    • Your use of arrays here doesn't make any sense to me there aren't any examples of a multi element array does that have any meaning in this scheme? When you call pluck on an array at the top level it automatically maps the pluck over the array. Why can't we do the same thing for sub arrays? Rewriting a relevant example:
    obj.pluck({:a => true, :d => [{:e => true, :f => false}]})
    

    Would become

    obj.pluck("a", {"d" : "e"})
    
    • The whole functional syntax seems like it might be a bit too complicated. It's also unclear to me how this works if you're running pluck on a sequence. It seems like all of your examples hinge on having the object in scope to execute the function. What would it look like executed on a sequence?
  8. mlucy commented on Jun 11, 2013

    @mlucy
    MemberAuthor

    @jdoliner --

    • How would you represent obj.pluck({:a => {:b => {:c => true}, :d => true}}) in your scheme? As obj.pluck({:a => [{:b => [:c]}, :d]})? It's much less obvious what that does at first sight, at least to me. I think we should at least provide people the option to use the boolean notation.
    • I'd be fine with having pluck automatically map over arrays when it recurses down. I think that's probably what people will want. If we do this, then we could re-purpose arrays to match your proposed syntax and just accept both (i.e. {:a => {:b => true}} is identical to {:a => [:b]} or {:a => :b}).
    • The examples with obj inside of pluck were just meant to demonstrate that you could provide an arbitrary predicate in addition to a literal true or false. I re-used obj because I didn't want to introduce a new variable, but in retrospect it just made the examples confusing. I wasn't really trying to say anything special there.
  9. jdoliner commented on Jun 11, 2013

    @jdoliner
    Contributor
    • obj.pluck({:a => {:b => {:c => true}, :d => true}})
      
      
    would be
    

    obj.pluck({:a => {:b => :c}}, :d)

    I'm still not seeing a place where we really need the mapping to true syntax.
    
    - Automatically recursing seems great to me.
    
    - Ah I see. So you can't for example provide a function that references the rows of a sequence? I think that would be too complicated for `pluck`.
    
  10. mlucy commented on Jun 11, 2013

    @mlucy
    MemberAuthor

    I think obj.pluck({:a => {:b => :c}}, :d) is obj.pluck({:a => {:b => {:c => true}}, :d => true}}), not obj.pluck({:a => {:b => {:c => true}, :d => true}}), unless I misunderstood your syntax.

  11. jdoliner commented on Jun 11, 2013

    @jdoliner
    Contributor

    Oh sorry, these are getting hard to read. I think the actually way to do it then would be:

    obj.pluck({:a => {:b => {:c => true}, :d => true}})
    # goes to
    obj.pluck({:a => [{:b => :c}, :d]})
    
  12. mlucy commented on Jun 11, 2013

    @mlucy
    MemberAuthor

    The first seems to read better, at least to me, because the structure of what you type exactly matches the structure of your output. When I read the second one I have to think for a second about what the resulting object will look like.

    That might just be me, though, and the second one is definitely shorter.

    What do other people think?

  13. coffeemug commented on Jun 11, 2013

    @coffeemug
    Contributor

    We'll also have to extend this syntax (whatever it will end up) to without and possible many other functions we missed. We should consider the list of functions this applies to carefully.

  14. coffeemug commented on Jun 11, 2013

    @coffeemug
    Contributor

    I think I like the original proposal much more than JD's proposal. { :a => { :b => true }} seems much more clear to me than { :a => b }. The former is a bit longer, but it reads very intuitively (possibly because it maps directly to the object's structure?), where's the latter seems odd. @neumino, @mglukhovsky, what do you think?

    (Also, the original proposal seemed odd to me initially, but I really grew to like it after sleeping on it for a bit. It does what we need and is pretty intuitive, IMO. We'd be hard-pressed to do much better (not that we shouldn't try))

  15. jdoliner commented on Jun 11, 2013

    @jdoliner
    Contributor

    I think the main thing that bugs me about the map solution is that it's actually going to lead to a lot of typing as soon as you're plucking any reasonably large number of fields. I just worry that after a bit of serious use it would start to feel like you were having to type the word True an awful lot to accomplish what you wanted. This is exacerbated by the fact that if you want to pluck nested attributes at all you need to convert everything to a map for example:

    # suppose you have the code
    obj.pluck("id", "user", "location", "text", "date", "created_at")
    # and you decide you just want the "name" attribute 
    # from the "user" field not the whole structure
    # you have to change your code to:
    obj.pluck({"id" : True, "user" : {"name" : True}, "location" : True, "text" : True, "date" : True, "created_at" : True})
    # instead of
    obj.pluck("id", {"user" : True}, "location", "text", "date", "created_at")

    It just seems like hitting that linear increase in typing is going to be really annoying. @neumino, @mglukhovsky and @al3xandru what do you guys think about it?

    Also it's worth considering that this syntax is going to apply in a number of other places. Such as has_fields, without, group_by contains which aren't going to benefit from having the structure of the pluck specifier (or whatever we want to call it) looking like the structure of their output (although I suppose they still look like the input). But we should consider how usable the extra verbosity would be with those

  16. 47 remaining items

  17. coffeemug commented on Jun 24, 2013

    @coffeemug
    Contributor

    Ok, just talked to @jdoliner and he felt the same way as @mlucy (in that we've discussed this ad-nauseum, if we let this sit we'll come back to it and have to waste a bunch of time getting to where we are now, etc.)

    So I retract my previous comment. Let's just do it as discussed and see what happens.

  18. jdoliner commented on Jun 24, 2013

    @jdoliner
    Contributor

    Working on this now since it seems like the most important optional issue.

  19. mlucy commented on Jun 24, 2013

    @mlucy
    MemberAuthor

    @jdoliner -- it would be really nice if we ended up with some sort of pathspec_t at the end of this so that we could easily use the same paths in other commands.

  20. mlucy commented on Jun 24, 2013

    @mlucy
    MemberAuthor

    (also, as_pathspec on val_t and datum_t would make this really nice to implement in the other commands that we want to support paths)

  21. jdoliner commented on Jun 27, 2013

    @jdoliner
    Contributor

    This is almost done I just need to confer with @mlucy about how to make it not recurse into nested arrays. It also needs tests and docs but it should be done by tomorrow.

  22. coffeemug commented on Jun 27, 2013

    @coffeemug
    Contributor

    🙇

  23. jdoliner commented on Jun 28, 2013

    @jdoliner
    Contributor

    This is in next as of 8c6b353. Ships in 1.7 CR 677 by @mlucy.

  24. coffeemug commented on Jun 29, 2013

    @coffeemug
    Contributor

    Does the new syntax apply only to pluck or other ops too (without, etc.?)

  25. mlucy commented on Jun 29, 2013

    @mlucy
    MemberAuthor

    Just pluck for 1.7.

  26. jdoliner commented on Jun 29, 2013

    @jdoliner
    Contributor

    I didn't want to do it on other commands without some formal discussion. I
    think we should just have an issue for next sprint where we discuss which
    functions should take pathspecs and what the semantics should be. Hopefully
    this will be pretty straight forward.

    On Fri, Jun 28, 2013 at 5:15 PM, coffeemug [email protected] wrote:

    Does the new syntax apply only to pluck or other ops too (without, etc.?)

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/889#issuecomment-20220992
    .

  27. coffeemug commented on Jun 29, 2013

    @coffeemug
    Contributor

    Cool, I think we'll market this as experimental syntax for 1.7, get feedback on it, roll it into all relevant commands in 1.8 with any enhancements we might do based on the feedback, and market it as a full-blown feature then. This works out really well, actually.

  28. coffeemug commented on Jun 29, 2013

    @coffeemug
    Contributor

    I created an issue for it -- #1094.

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

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions