Code Monkey home page Code Monkey logo

Comments (3)

Suor avatar Suor commented on May 24, 2024 1

Ok, if you frame it as adding del_in() then it makes sense for completeness and symmetry. It will need to support lists though for the same reasons. I'll mark this with looking for more interest label for now. Thanks for bringing this up, I missed it somehow during the initial design.

from funcy.

Suor avatar Suor commented on May 24, 2024

There are a couple of issues with this:

  • the API does not permit omitting several things, like omit() does.
  • what about of projecting instead of omitting? Do we also need project_in()?
  • what if we want to filter keys by some predicate? Do we need select_keys_in()?

So we come to a combinatoric explosion, which is traditionally solved with composition. In this case we can pass things to update_in():

update_in({'a': {'b': 0, 'c': 1}}, ['a'], rpartial(omit, {'c'}))

# if we expect path may not always be present
update_in(coll, path, rpartial(omit, keys), default={})

# several things to omit
update_in(coll, path, rpartial(omit, {'c', 'd'}))

# whitelist keys on path
update_in(coll, path, rpartial(project, {'a', 'b'}))

# ...

If you happen to do any particular of these operations a lot you may write a one-line helper. I am not sure that one should be included into funcy just yet.

from funcy.

ruancomelli avatar ruancomelli commented on May 24, 2024

Wow, I think I just didn't spend enough time toying around with update_in to realize how powerful it is. Even set_in is defined in terms of update_in. Very clever!
I would close this issue now since I agree with you that it doesn't seem worth the trouble, but there's still something bothering me here:

  • if you wish to do d.get(...) or d[...] in a deep/nested collection, you use get_in
  • if you wish to do d[...] = ... in a deep/nested collection, you use set_in
  • if you wish more general operations, perhaps update_in is the answer

So, loosely speaking, we have dict.__getitem__ and dict.__setitem__ available through get_in and set_in. However, the analogous to del d[...] (or dict.__delitem__) is still missing. I know that it can be easily implemented using update_in (as you showed), but then also can set_in. It was this "del_in" analogous that I had in mind and felt that was missing in funcy when I wrote omit_in.

Having the complete set of basic dict operations would be nice, but I also think that it may indeed be too much trouble for too little value (since nobody else seemed to need this feature up to now). I'll leave this issue open for now if you wish to add any closing remarks, but I'm satisfied with tweaking update_in.

from funcy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.