Next: , Previous: Basics, Up: Statistics   [Contents]


6.2 Tabulating values

(mu:tabulate <procedure> [<search-expr>]) applies <procedure> to each message matching <search-expr> (leave empty to match all messages), and returns a associative list (a list of pairs) with each of the different results of <procedure> and their frequencies. For fields that contain lists of values (such as address-fields), each of the values in the list is added separately.

6.2.1 Example: messages per weekday

We demonstrate mu:tabulate with an example. Suppose we want to know how many messages we receive per weekday:

#!/bin/sh
exec guile -s $0 $ !#

(use-modules (mu) (mu stats) (mu plot))
(mu:initialize)

;; create a list like (("Sun" . 13) ("Mon" . 23) ...)
(define weekday-table
  (mu:weekday-numbers->names
    (sort
      (mu:tabulate
	(lambda (msg)
	  (tm:wday (localtime (mu:date msg)))))
      (lambda (a b) (< (car a) (car b))))))

(for-each
  (lambda (elm)
    (format #t "~a: ~a\n" (car elm) (cdr elm)))
  weekday-table)

The procedure weekday-table uses mu:tabulate-message to get the frequencies per hour – this returns a list of pairs:

((5 . 2339) (0 . 2278) (4 . 2800) (2 . 3184) (6 . 1856) (3 . 2833) (1 . 2993))

We sort these pairs by the day number, and then apply mu:weekday-numbers->names, which takes the list, and returns a list where the day numbers are replace by there abbreviated name (in the current locale). Note, there is also mu:month-numbers->names.

The script then outputs these numbers in the following form:

Sun: 2278
Mon: 2993
Tue: 3184
Wed: 2833
Thu: 2800
Fri: 2339
Sat: 1856

Clearly, Saturday is a slow day for e-mail...