Function: -frequencies

-frequencies is a byte-compiled function defined in dash.el.

Signature

(-frequencies LIST)

Documentation

Count the occurrences of each distinct element of LIST.

Return an alist of (ELEMENT . N), where each ELEMENT occurs N times in LIST.

The test for equality is done with equal, or with -compare-fn if that is non-nil.

See also -count and -group-by.

View in manual

Source Code

;; Defined in ~/.emacs.d/elpa/dash-20260221.1346/dash.el
(defun -frequencies (list)
  "Count the occurrences of each distinct element of LIST.

Return an alist of (ELEMENT . N), where each ELEMENT occurs N
times in LIST.

The test for equality is done with `equal', or with `-compare-fn'
if that is non-nil.

See also `-count' and `-group-by'."
  (declare (important-return-value t))
  (let (test len freqs)
    (cond ((null list))
          ((and (setq test (dash--hash-test-fn))
                (> (setq len (length list)) dash--short-list-length))
           (let ((ht (make-hash-table :test test :size len)))
             ;; Share structure between hash table and returned list.
             ;; This affords a single pass that preserves the input
             ;; order, conses less garbage, and is faster than a
             ;; second traversal (e.g., with `maphash').
             (--each list
               (let ((freq (gethash it ht)))
                 (if freq
                     (setcdr freq (1+ (cdr freq)))
                   (push (puthash it (cons it 1) ht) freqs))))))
          ((let ((assoc (dash--assoc-fn)))
             (--each list
               (let ((freq (funcall assoc it freqs)))
                 (if freq
                     (setcdr freq (1+ (cdr freq)))
                   (push (cons it 1) freqs)))))))
    (nreverse freqs)))