-
Notifications
You must be signed in to change notification settings - Fork 8
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
can't provide my own dissoc-like fn #21
Comments
I think I need to understand your use case better, because it makes no sense to me to supply your own dissoc in this context. (transform foo [:x :y] dissoc) is just a convenient shorthand for (transform foo [:x] #(dissoc % :y)) I hope I got that right, haven’t done Clojure in a while so am quite rusty :P |
Here's my UC. I have a map: (def m {:x "5" :y ""}) I want a fn that will transform the map, dissoc-ing keys, whose values are (= {:x "5"} (foo m))
=> true To do this with As you have shown @boxed, (transform m [:x blank?] dissoc) edited: no that isn't quite right because Then that might become something like: (transform m [:x] #(dissoc % blank?)) But of course edited: we now return to your regularly-scheduled, mostly-coherent discussion. Here's a (defn foo [m pred]
(into {}
(remove
(fn [[k v]](pred v))
m))) and it works: (= {:x "5"} (foo {:x "5" :y ""} blank?))
=> true I could use I have a map: (def m2 {:x [1] :y []}) I want a fn that will transform the map, dissoc-ing keys, whose values are (= {:x [1]} (foo m2 empty?))
=> true |
Hmm, yea we don't have any way to match on the values which you've understood is what you're really asking for. One could imagine something like: (transform m [:x (%>value blank?)] dissoc) What do you think @crisptrutski ? |
Could you branch on the arity of the provided function? If it is 1, then pass in the value at the path as an argument. If it is 2, then pass in the collection and the path as arguments? And explicitly bail out noisily when someone tries to use a multimethod? |
That sounds reasonable too. One could imagine supporting arity 3 toF or even more for some other feature. I'm thinking out loud now but how about that the arguments could be: value, key, path, container where key is in. That seems like it would cover lots of use cases. |
dissoc
apparently operates on the map that has the matching key:✓
Since
dissoc
is a fn of (map, key), I ought to be able to provide my own, more selective, fn of (map,key), right?An exact duplicate of the original example above, should be:
This prompted me to look at the source, from which I see that the
dissoc
fn is treated as a special case.I'd like to be able to provide my own custom dissoc-like fn. Barring that, is there a way to operate on particular associations this way?
I see that a fn in this position only receives one argument: the value of the association:
That fn can change the value of the association, but cannot dissoc it.
Of course, I gave it a whack with capture groups, but I couldn't make that work.
The text was updated successfully, but these errors were encountered: