admin管理员组文章数量:1435534
Lodash _.pluck does this
var users = [
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 }
];
_.pluck(users, 'user');
// → ['barney', 'fred']
Good thing about it is it can also go deep like this:
var users = [
{ 'user': {name: 'barney'}, 'age': 36 },
{ 'user': {name: 'fred'}, 'age': 40 }
];
_.pluck(users, 'user.name');
// ["barney", "fred"]
Is there equivalent in Clojure core of this? I mean, I can easily create one line somewhat like this
(defn pluck
[collection path]
(map #(get-in % path) collection))
And use it like this:
(def my-coll [{:a {:z 1}} {:a {:z 2}}])
(pluck my-coll [:a :z])
=> (1 2)
I was just wondering if there's such thing already included in Clojure and I overlooked it.
Lodash _.pluck does this
var users = [
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 }
];
_.pluck(users, 'user');
// → ['barney', 'fred']
Good thing about it is it can also go deep like this:
var users = [
{ 'user': {name: 'barney'}, 'age': 36 },
{ 'user': {name: 'fred'}, 'age': 40 }
];
_.pluck(users, 'user.name');
// ["barney", "fred"]
Is there equivalent in Clojure core of this? I mean, I can easily create one line somewhat like this
(defn pluck
[collection path]
(map #(get-in % path) collection))
And use it like this:
(def my-coll [{:a {:z 1}} {:a {:z 2}}])
(pluck my-coll [:a :z])
=> (1 2)
I was just wondering if there's such thing already included in Clojure and I overlooked it.
Share Improve this question asked Jun 13, 2015 at 8:34 ma2sma2s 1,3121 gold badge12 silver badges24 bronze badges5 Answers
Reset to default 4There is no built-in function for this. You can refer to clojure.core API reference and the cheatsheet to look up what's available.
I would say Clojure's syntax is light enough that it feels sufficient to use a bination of map
and an accessor utility like get-in
.
This also demonstrates a well-adopted principle in Clojure munity: provide mostly simple defaults and let the users pose them as they need. Some people would probably argue that pluck
conflates iteration and querying.
The most simple way is something like:
(map #(:user %) users)
It will return list '(36 40)
Here are simple tricks:
(def my-coll [{:a {:z 1}} {:a {:z 2}}])
(map :a my-coll)
=> ({:z 1} {:z 2})
(map (p :z :a) my-coll)
=> (1 2)
Works because keyword behaves like a function as well.
lodash
deprecated _.pluck
function in favor of _.map
. Thus:
_.pluck(users, 'user')
is now
_.map(users, 'user')
I like the lodash/fp
flavor, wich put the iteratees in front of the collection
_.map('user', users)
You also get autocurry for free
_.map('user')(users)
This function in lodash is now called pick and I believe its closest equivalent in Clojure is select-keys.
本文标签: javascriptWhat is clojurecore equivalent of lodash pluckStack Overflow
版权声明:本文标题:javascript - What is clojure.core equivalent of lodash _.pluck - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745627408a2667058.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论