Is there any way to do group-by and count at once in Clojure?

ntalbs

Assume that there is a sequence like below:

["ab" "ba" "ac" "ca" "bc" "cc"]

I want to know the frequencies but the key should be sorted string. In short, I want to get the result like this:

{"ab" 2, "ac" 2, "bc" 1, "cc" 1}

Clojure has frequencies function, but it doesn't accept key function. So, usually I can do this by the combination of group-by and map:

(->> ["ab" "ba" "ac" "ca" "bc" "cc"]
     (group-by #(apply str (sort %)))
     (map (fn [[k vs]] [k (count vs)]))
     (int {}))

But, this looks verbose. Even in Java, I can do grouping and counting at the same time with Stream API, like this: (Assuming that there is a method sortedStr(s)

Arrays.asList("aa", "ab", "ab", "bb", "cc" , "ca")
  .stream()
  .collect(groupingBy(s->sortedStr(s), counting()));

Is there any way to group-by and counting at once in clojure like Java8?

leetwinski

@Stefan answer works well, but it is not the most efficient, because it first maps over the coll (producing an intermediate collection) and then finds frequencies. So it doesn't really conform to "group-by and count at once" part of your question. I would rather go with reduce:

user> (reduce #(update %1 (apply str (sort %2)) (fnil inc 0))
              {} ["ab" "ba" "ac" "ca" "bc" "cc"])
{"ab" 2, "ac" 2, "bc" 1, "cc" 1}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Do something once click count reaches X

分類Dev

any efficient way to count binary values in columns of big data table?

分類Dev

Clojure core.async, any way to control number of threads in that (go...) thread pool?

分類Dev

How do i eliminate the duplicate count from mysql Group by result?

分類Dev

Is there any way to do email confirmation for Firebase user creation and/or password reset?

分類Dev

Is there any way to quickly and efficiently get a count of IP host addresses in thousands of CIDR/netmask ranges in Bash?

分類Dev

Is there any way to quickly and efficiently get a count of IP host addresses in thousands of CIDR/netmask ranges in Bash?

分類Dev

Is there any way to do Custom dynamic mapping of different number of columns in Azure dataflow or any other options to achieve this?

分類Dev

Mysql pull a count, and group by that count

分類Dev

count all values and group by with count

分類Dev

Any way to search across all log streams in a cloud watch log group?

分類Dev

MongoDB SELECT COUNT GROUP BY

分類Dev

Pandas Group By and Count

分類Dev

NULL value count in group by

分類Dev

Query with count and group by eloquent

分類Dev

Count and group by Power BI

分類Dev

group array and get count

分類Dev

LINQ Group by and "count-if"

分類Dev

LINQ group by for count

分類Dev

Select, count and display (by group?)

分類Dev

Rails: group by the count of a column

分類Dev

Group by, distinct, count in django

分類Dev

SQL Group By With Count Zero

分類Dev

MYSQL Count(Distinct)Group By

分類Dev

Count and group by in same query

分類Dev

MySQL SELECT COUNT GROUP

分類Dev

LINQ IQueryable with group by and count

分類Dev

SQL group by(count、sum)

分類Dev

How do you group around a time period in active record, but only count one occurrance per user?

Related 関連記事

  1. 1

    Do something once click count reaches X

  2. 2

    any efficient way to count binary values in columns of big data table?

  3. 3

    Clojure core.async, any way to control number of threads in that (go...) thread pool?

  4. 4

    How do i eliminate the duplicate count from mysql Group by result?

  5. 5

    Is there any way to do email confirmation for Firebase user creation and/or password reset?

  6. 6

    Is there any way to quickly and efficiently get a count of IP host addresses in thousands of CIDR/netmask ranges in Bash?

  7. 7

    Is there any way to quickly and efficiently get a count of IP host addresses in thousands of CIDR/netmask ranges in Bash?

  8. 8

    Is there any way to do Custom dynamic mapping of different number of columns in Azure dataflow or any other options to achieve this?

  9. 9

    Mysql pull a count, and group by that count

  10. 10

    count all values and group by with count

  11. 11

    Any way to search across all log streams in a cloud watch log group?

  12. 12

    MongoDB SELECT COUNT GROUP BY

  13. 13

    Pandas Group By and Count

  14. 14

    NULL value count in group by

  15. 15

    Query with count and group by eloquent

  16. 16

    Count and group by Power BI

  17. 17

    group array and get count

  18. 18

    LINQ Group by and "count-if"

  19. 19

    LINQ group by for count

  20. 20

    Select, count and display (by group?)

  21. 21

    Rails: group by the count of a column

  22. 22

    Group by, distinct, count in django

  23. 23

    SQL Group By With Count Zero

  24. 24

    MYSQL Count(Distinct)Group By

  25. 25

    Count and group by in same query

  26. 26

    MySQL SELECT COUNT GROUP

  27. 27

    LINQ IQueryable with group by and count

  28. 28

    SQL group by(count、sum)

  29. 29

    How do you group around a time period in active record, but only count one occurrance per user?

ホットタグ

アーカイブ