-
Notifications
You must be signed in to change notification settings - Fork 264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add select_keys
.
#393
base: master
Are you sure you want to change the base?
Add select_keys
.
#393
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Welcome back @eigenhombre ! It's good to see you.
Since you've been away much has happened. In general this project has become much more conservative about adding new functions, mostly because we were flooded with many PRs for specialty functions that would have made the namespace harder to navigate. I genuinely don't know what our policy is these days on what to accept and what not to accept. @eriknw may be more up to date than I am.
Another constraint we've considered adding is asking people to submit Cython solutions to CyToolz, though I don't think that this has ever been enforced.
toolz/dicttoolz.py
Outdated
{} | ||
""" | ||
rv = factory() | ||
for k in set(keys).intersection(d.keys()): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recommend spelling this out explicitly for performance reasons:
for k in keys:
if k in d:
rv[k] = d[k]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
@mrocklin Nice to be back. I think |
Lets see if @eriknw has any thoughts in the next day or so and if not, then merge. |
Should also remember to add this to the API documentation |
Indeed, welcome back @eigenhombre! Sorry for my absence. I'm (mostly) off-the-grid this week dealing with a family event.
In [1]: {k: v for k, v in d.items() if k in keys}
In [2]: {k: d[k] for k in keys if k in d}
In [3]: keyfilter(keys.__contains__, d)
In [4]: select_keys(d, keys) I'm not sure about the argument order. It seems to me that We don't require that contributors add to CyToolz as well, but it is appreciated. Okay, gotta go! |
Hi again! @eriknw , in Clojure's Will take a look at the API docs to refresh my memory about how that's done and whether I can add that to the PR.... |
Having the keys first would allow for nice currying and use of Pipe, which has become somewhat idiomatic in the use of toolz from toolz.curried import pipe, select_keys, map, filter, get, frequencies
pipe(data, map(select_keys(['name', 'age'])), filter(lambda d: d['age'] > 10), get('name'), frequencies) Clojure has more options here, so this isn't as big of a deal for them |
👍 I too would like to see this in the IMO |
I don't understand why this was closed. I came looking for |
@epgui : I closed it b/c I'd rather not merge unilaterally without one of @mrocklin / @eriknw +1'ing, since they have put more work into this project than I have. I also would prefer the argument order that Clojure uses. IMO this library has gone in a somewhat different direction than what I would prefer: my preference would be to provide a fairly simple set of functions which are (C)Python drop-ins for the highly cohesive set of collections operations provided by Clojure. I haven't done a broad survey of the available libraries to know if there is a good fit out there, or if another library is called for. Am interested in what other views in the community might be. |
Reopening to encourage further discussion, since clearly there has been some demand for this. |
Strongly agreed, but I don't even think it has to stop there. A big part of why FP is so great, IMO, is that it allows for a richer "vocabulary" of functions, which provides for more expressive and concise code (alternatively, the same idea but in the words of the pytoolz developers). I think we could easily argue for inclusion of functions from Haskell (eg.: much of what you'd find in Prelude), or even what you'd find in other FP libraries (eg.: lodash, ramda.js, etc). Having a long list of functions available doesn't complicate anything for the user, but it does make organizing docs more important. I don't have the strongest opinion on the order of arguments, on the other hand. Clojure's philosophy of having "values" come first and "lists of values" come last works very well with thread-first and thread-last macros, but I also understand the other opinion expressed in this thread. I think you could reasonably argue for either way, and I think the best decision would be the most self-consistent decision (and not necessarily the one that is most consistent with other languages or libraries). |
Fixes #392.