Public Vars

Back

frequencies (clj)

(source)

function

(frequencies coll)
Returns a map from distinct items in coll to the number of times they appear.

Examples

typedclojure/typedclojure
(ns ^:no-doc typed.ann.clojure
  "Type annotations for the base Clojure distribution."
  #?(:cljs (:require-macros [typed.ann-macros.clojure :as macros]))
  (:require [clojure.core :as cc]
            [typed.clojure :as t]
            #?(:clj [typed.ann-macros.clojure :as macros])
            #?(:clj typed.ann.clojure.jvm) ;; jvm annotations
            #?(:clj clojure.core.typed))
  #?(:clj
     (:import (clojure.lang PersistentHashSet PersistentList
                            APersistentMap #_IPersistentCollection
                            #_ITransientSet
                            IRef)
              (java.util Comparator Collection))))

cc/frequencies (t/All [a] [(t/Seqable a) :-> (t/Map a t/AnyInteger)])
fluree/db
(ns fluree.db.json-ld.api-test
  (:require #?(:clj  [clojure.test :refer [deftest is testing]]
               :cljs [cljs.test :refer-macros [deftest is testing async]])
            #?@(:cljs [[clojure.core.async :refer [go <!]]
                       [clojure.core.async.interop :refer [<p!]]])
            [fluree.db.did :as did]
            [fluree.db.dbproto :as dbproto]
            [fluree.db.json-ld.api :as fluree]
            [fluree.db.query.range :as query-range]
            [fluree.db.test-utils :as test-utils]
            [fluree.db.util.async :refer [<?? <?]]
            [fluree.db.util.context :as ctx-util]
            [fluree.db.util.core :as util]
            #?(:clj  [test-with-files.tools :refer [with-tmp-dir]
                      :as twf]
               :cljs [test-with-files.tools :as-alias twf])))

       (is (= {"BORG" 4 "Murray" 1}
              (frequencies @(fluree/query db2 {"@context" [test-utils/default-str-context
                                                           {"ex" "ns:ex/"}]
                                               "where"    {"id" "?s", "ex:name" "?name"}
                                               "select"   "?name"}))))
       (is (= [{"id"          "ex:mp"
                "type"        "ex:Cat"
                "ex:name"     "Murray"
                "ex:favs"     ["Persey" {"id" "ex:dp"}]
                "ex:address"  {"ex:street" "55 Bashford" "ex:city" "St. Paul" "ex:zip" 55105 "ex:state" "MN"}
                "ex:isOrange" true
                "ex:isPerson" false
                "ex:nickname" "The Wretch"}]
              @(fluree/query db2 {"@context" [test-utils/default-str-context
                                              {"ex" "ns:ex/"}]
                                  "where"    {"id" "?s", "ex:name" "Murray"}
                                  "select"   {"?s" ["*" {"ex:address" ["ex:street" "ex:city" "ex:state" "ex:zip"]}]}})))
damballa/parkour
(ns parkour.multiplex-test
  (:require [clojure.test :refer :all]
            [clojure.core.reducers :as r]
            [parkour (conf :as conf) (fs :as fs) (mapreduce :as mr)
                     (wrapper :as w) (graph :as pg)]
            [parkour.io (dseq :as dseq) (mux :as mux)
                        (text :as text) (avro :as mravro)]
            [parkour.test-helpers :as th]))

(deftest test-input
  (let [text (text/dseq "dev-resources/word-count-input.txt")
        avro (mravro/dseq [:default] "dev-resources/words.avro")
        multi (mux/dseq text avro)]
    (is (= {[:text "apple"]  3, [:text "banana"]  2, [:text "carrot"]  1,
            [:avro "applez"] 3, [:avro "bananaz"] 2, [:avro "carrotz"] 1}
           (->> multi
                (r/map (fn [[k v]] (if v [:text v] [:avro k])))
                (into [])
                (frequencies))))))

(deftest test-malkovich-malkovich
  (let [text (text/dseq "dev-resources/word-count-input.txt")
        avro (mravro/dseq [:default] "dev-resources/words.avro")
        multi (mux/dseq (mux/dseq text) (mux/dseq avro))]
    (is (= {[:text "apple"]  3, [:text "banana"]  2, [:text "carrot"]  1,
            [:avro "applez"] 3, [:avro "bananaz"] 2, [:avro "carrotz"] 1}
           (->> multi
                (r/map (fn [[k v]] (if v [:text v] [:avro k])))
                (into [])
                (frequencies))))))
reborg/parallel
(require '[parallel.core :as p])
(require '[criterium.core :refer [bench quick-bench]])
(require '[clojure.core.reducers :as r])

;; small overlapping
(quick-bench (frequencies small-overlapping))
;; 441 ms
(quick-bench (p/frequencies small-overlapping))
;; 190 ms
(binding [p/*mutable* true] (quick-bench (p/frequencies small-overlapping)))
;; 92ms


;; bigger overlapping
(quick-bench (frequencies big-overlapping))
;; 172ms
(quick-bench (p/frequencies big-overlapping))
;; 52ms
(binding [p/*mutable* true] (quick-bench (p/frequencies big-overlapping)))
;; 28ms

(quick-bench (frequencies (eduction (keep :samplevalue) (map int) small-overlapping)))
;; 238 ms
(quick-bench (p/frequencies small-overlapping (keep :samplevalue) (map int)))
;; 91 ms
(binding [p/*mutable* true] (quick-bench (p/frequencies small-overlapping (keep :samplevalue) (map int))))
;; 50 ms

(quick-bench (frequencies no-overlapping))
;; 335 µs
(quick-bench (p/frequencies no-overlapping))
;; 299 µs

(time (dorun (frequencies bigger-data)))
;; 4320.984379 ms
(time (dorun (p/frequencies bigger-data)))
;; 1980.512017 ms