Comments (11)
Why not just use (->> coll (index-by kf) (map-vals vf))
? It's not particularly more verbose, and it makes it clear which function is the indexer and which function maps the values. Or alternatively, (->> coll (map vf) (index-by kf))
would work as well, no? And that's only four characters longer.
from medley.
Why not just use (->> coll (index-by kf) (map-vals vf))? It's not particularly more verbose, and it makes it clear which function is the indexer and which function maps the values.
What can I say, the main reason is frequency :) . This pattern occurs so often in my code that I will eventually add this utility function anyway.
Another reason might be performance of course, doing one scan and indexing instead of two, though I concede that's less problematic in general.
Or alternatively, (->> coll (map vf) (index-by kf)) would work as well, no? And that's only four characters longer.
No, because in vf
you typically do a projection that loses the information required for indexing by kf
, for instance (def user-id->name (index-and-map-by :id :name users))
.
from medley.
No, because in
vf
you typically do a projection that loses the information required for indexing bykf
Right. So map-vals
needs to be used, which means that a single function would save 9 characters, and using map
and juxt
would save 4:
(index-and-map-by :id :name coll)
(into {} (map (juxt :id :name)) coll)
(->> coll (index-by :id) (map-vals :name))
To my eyes the last one is the clearest in terms of intent, though likely also the least performant. I'll need to think about this one for a while.
from medley.
Maybe and
in the name indications some kind of permutation problem. Maybe I'd also like to have index-and-keep-by
, etc. I don't think medley can possibly host every possible combination of functions?
from medley.
(defn index-and-hof-by
[hof kf vf coll]
(into {}
(f
(fn [x]
[(kf x) (vf x)]))
coll))
from medley.
@borkdude arguable, at this point you're just looking for transduce
:)
I'm really only asking for a narrow but common use case.
from medley.
@weavejester I would argue that maybe characters count is not the best proxy for clarity in this case?
Even if (->> coll (index-by :id) (map-vals :name))
is only 9 characters longer, it means that the reader has to work out the overarching indexing intention by interpreting the two function calls in her head.
If we agreed that this pattern is frequent enough that the reader finds it worth memorizing by the index-and-map-by
name, then (index-and-map-by :id :name coll)
would be the most readable presentation, right?
from medley.
I personally don't find enough use for the function to be worth memorizing, so I'd find index-by
and map-vals
to be easier to understand. The use of the separate functions also makes it clear which key is being used for indexing, and which key is being used to map over the values.
from medley.
@weavejester your call, there's no arguing against personal judgement. I guess I'll keep writing this function on every project :) thanks for considering it!
from medley.
Give me some time to think about it. There may also be performance improvements, or another way to write it. I'll leave this issue open.
from medley.
If that helps, the way I would write it for performance is:
(defn index-and-map-by
[kf vf coll]
(persistent!
(reduce
(fn [tm x]
(let [k (kf x)
v (vf x)]
(assoc! tm k v)))
(transient {})
coll)))
from medley.
Related Issues (20)
- Suggested addition: index-of HOT 3
- dissoc-in has unexpected behaviors HOT 3
- Minor Clojure 1.11 Alpha 4 compatibility issue HOT 1
- Release request, to prevent warnings for Clojure 1.11 users HOT 1
- Suggested addition: update-items HOT 9
- Suggested addition: ClojureCLR support HOT 2
- medley/join may occur StackOverflowError in rare cases HOT 3
- Suggestion: template HOT 3
- Suggested addition: mapmap HOT 2
- NPE from assoc-some if map is nil and there is more than one key supplied HOT 1
- `time` as data? HOT 1
- toggle in set HOT 2
- Add conj-some fn HOT 2
- Suggested addition: replace-subvec HOT 1
- Add flip function HOT 4
- Proposal: re-find-groups-seq HOT 3
- Incompatibility between medley/medley and dev.weavejester/medley HOT 1
- Suggested addition: assoc-conj
- insert-nth: ability to use negative numbers HOT 3
- Suggested addition: generalization of group-by/index-by HOT 12
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from medley.