Overview Clamps Packages CM Dictionary Clamps Dictionary Fomus
Next: counter , Previous: copy-ref , Up: C , Home: Overview

Clamps Dictionary

count-elements

Function

(count-elements seq &key (test #'eql) (key #'identity) (sort t))

Count the number of occurences of all mutually equal elems in seq extracted from its items according to the key function and satisfying the test function as predicate. Return the results as list with sublists of the form (elem count) for each elem, optionally sorted according to the setting of sort.

Arguments

seq A Common Lisp Sequence.
:test Function accepted as test function by #'make-hash-table.
:key Function to extract the key to compare from each element.
:sort Boolean indicationg whether and how to sort the results. Possible Values are:
  :from-end
  t
  nil

If :sort is nil, result returns the items in the order of their first occurence, if :sort is :from-end, they are returned in reverse order of occurence, if :sort is t, they are either sorted by their value, if all elems are numbers or by the number of occurences otherwise.

Examples

(count-elements '(1 3 2 6 5 4 3 8 1 3 5 2 4 3 6 5 3 3 4 1))
;; => ((1 3) (2 2) (3 6) (4 3) (5 3) (6 2) (8 1))

(count-elements '(1 3 2 6 5 4 3 8 1 3 5 2 4 3 6 5 3 3 4 1) :sort :from-end)
;; => ((8 1) (4 3) (5 3) (6 2) (2 2) (3 6) (1 3))

(count-elements '(1 3 2 6 5 4 3 8 1 3 5 2 4 3 6 5 3 3 4 1) :sort t)
;; => ((1 3) (2 2) (3 6) (4 3) (5 3) (6 2) (8 1))

(count-elements '(a b a d e c d a e d e b d f d e) :sort t)
;; => ((d 5) (e 4) (a 3) (b 2) (f 1) (c 1))

(count-elements '((a 10) (b 11) (a 12) (d 13)) :key #'first :sort t)
;; => ((a 2) (d 1) (b 1))

Author: Orm Finnendahl

Created: 2025-02-18 Di 12:58

Validate