Skip to content

Commit

Permalink
mlib: frequencies accepts generators as well
Browse files Browse the repository at this point in the history
  • Loading branch information
mayerrobert committed Dec 5, 2024
1 parent 23d17f9 commit ef27457
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
9 changes: 7 additions & 2 deletions mlib.html
Original file line number Diff line number Diff line change
Expand Up @@ -1023,13 +1023,18 @@
from within `function` is not supported.

### Function: frequencies
(frequencies sequence [test]) -> hash-table
(frequencies sequence-or-generator [test]) -> hash-table

Since: 1.5

Count the number of times each value occurs in the sequence
Count the number of times each value occurs in `sequence-or-generator`
according to the test function `test` which defaults to `eql`.

Sample usage:

(frequencies ()) ; ==> nil
(frequencies #(1 2 3 1 2 1)) ; ==> #H(eql 1 3 2 2 3 1)

### Function: identity
(identity object) -> object

Expand Down
9 changes: 7 additions & 2 deletions mlib.md
Original file line number Diff line number Diff line change
Expand Up @@ -1014,13 +1014,18 @@ Similar to CL's `maphash` but modifying the hash-table
from within `function` is not supported.

### Function: frequencies
(frequencies sequence [test]) -> hash-table
(frequencies sequence-or-generator [test]) -> hash-table

Since: 1.5

Count the number of times each value occurs in the sequence
Count the number of times each value occurs in `sequence-or-generator`
according to the test function `test` which defaults to `eql`.

Sample usage:

(frequencies ()) ; ==> nil
(frequencies #(1 2 3 1 2 1)) ; ==> #H(eql 1 3 2 2 3 1)

### Function: identity
(identity object) -> object

Expand Down
5 changes: 3 additions & 2 deletions samples.murmel-mlib/mlib-test.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -1530,8 +1530,9 @@ all the result list to a single list. FUNCTION must return a list."

#+murmel
(tests frequencies
(hash-equal (frequencies '(1 2 2 3 3 3 4 4 4 4 5 5 5 5 5)) #H(eql 1 1 2 2 3 3 4 4 5 5)) => t
(hash-equal (frequencies #(1 2 2 3 3 3 4 4 4 4 5 5 5 5 5)) #H(eql 1 1 2 2 3 3 4 4 5 5)) => t)
(hash-equal (frequencies '(1 2 2 3 3 3 4 4 4 4 5 5 5 5 5)) #H(eql 1 1 2 2 3 3 4 4 5 5)) => t
(hash-equal (frequencies #(1 2 2 3 3 3 4 4 4 4 5 5 5 5 5)) #H(eql 1 1 2 2 3 3 4 4 5 5)) => t
(hash-equal (frequencies (scan #(1 2 2 3 3 3 4 4 4 4 5 5 5 5 5))) #H(eql 1 1 2 2 3 3 4 4 5 5)) => t)


;;; - higher order
Expand Down
29 changes: 22 additions & 7 deletions samples.murmel-mlib/mlib.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -2708,19 +2708,34 @@


;;; = Function: frequencies
;;; (frequencies sequence [test]) -> hash-table
;;; (frequencies sequence-or-generator [test]) -> hash-table
;;;
;;; Since: 1.5
;;;
;;; Count the number of times each value occurs in the sequence
;;; Count the number of times each value occurs in `sequence-or-generator`
;;; according to the test function `test` which defaults to `eql`.
;;;
;;; Sample usage:
;;;
;;; (frequencies ()) ; ==> nil
;;; (frequencies #(1 2 3 1 2 1)) ; ==> #H(eql 1 3 2 2 3 1)
(defun frequencies (seq . test)
(let ((counts (make-hash-table (car test))))
(if (listp seq)
(dolist (x seq counts)
(incf (hashref counts x 0)))
(dovector (x seq counts)
(incf (hashref counts x 0))))))
(cond ((null seq) ())

((listp seq)
(dolist (x seq counts)
(incf (hashref counts x 0))))

((vectorp seq)
(dovector (x seq counts)
(incf (hashref counts x 0))))

((functionp seq)
(dogenerator (x seq counts)
(incf (hashref counts x 0))))

(t (jerror 'simple-type-error "frequencies - not a sequence or generator: '%s'" seq)))))


; higher order ********************************************************
Expand Down

0 comments on commit ef27457

Please sign in to comment.