diff --git a/guile/mu/Makefile.am b/guile/mu/Makefile.am index 3a4ddc27..73eb4934 100644 --- a/guile/mu/Makefile.am +++ b/guile/mu/Makefile.am @@ -21,6 +21,7 @@ include $(top_srcdir)/gtest.mk scmdir=${prefix}/share/guile/site/2.0/mu/ scm_DATA= \ message.scm \ - contact.scm + contact.scm \ + stats.scm EXTRA_DIST=$(scm_DATA) diff --git a/guile/mu/contact.scm b/guile/mu/contact.scm index 9c2c1231..81387548 100644 --- a/guile/mu/contact.scm +++ b/guile/mu/contact.scm @@ -24,6 +24,8 @@ :use-module (mu message) :export ( ;; classes + ;; global methods + mu:for-each-contact ;; contact methods name email timestamp frequency last-seen )) diff --git a/guile/mu/message.scm b/guile/mu/message.scm index 7364cb31..379a1377 100644 --- a/guile/mu/message.scm +++ b/guile/mu/message.scm @@ -21,7 +21,6 @@ :export ( ;; classes mu:for-each-message - mu:for-each-contact mu:message-list ;; internal mu:for-each-msg-internal diff --git a/guile/mu/stats.scm b/guile/mu/stats.scm new file mode 100644 index 00000000..7f1ee96b --- /dev/null +++ b/guile/mu/stats.scm @@ -0,0 +1,57 @@ +;; +;; Copyright (C) 2011-2012 Dirk-Jan C. Binnema +;; +;; This program is free software; you can redistribute it and/or modify it +;; under the terms of the GNU General Public License as published by the +;; Free Software Foundation; either version 3, or (at your option) any +;; later version. +;; +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. +;; + +;; You should have received a copy of the GNU General Public License +;; along with this program; if not, write to the Free Software Foundation, +;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +(define-module (mu stats) + :use-module (oop goops) + :use-module (mu message) + :export ( + mu:tabulate-messages + mu:average-messages)) + + +(define* (mu:tabulate-messages func #:optional (expr #t)) + "Execute FUNC for each message matching EXPR, and return an alist +with maps each result of FUNC to its frequency. FUNC is a function +takes a instance as its argument. For example, to +tabulate messages by weekday, one could use: + (mu:tabulate-messages (lambda(msg) (tm:wday (localtime (date msg)))))." + (let ((table '())) + (mu:for-each-message + (lambda(msg) + (let* ((val (func msg)) + (old-freq (or (assoc-ref table val) 0))) + (set! table (assoc-set! table val (1+ old-freq))))) + expr) + table)) + + +(define* (mu:average-messages func #:optional (expr #t)) + "Execute FUNC for each message matching EXPR, and return the average +value of the results of FUNC. FUNC is a function that takes a + instance as its argument, and returns some number. For +example, to get the average message size of messages related to +icecream: (mu:average (lambda(msg) (size msg)) \"icecream\" ." +(let ((count 0) (sum 0)) + (mu:for-each-message + (lambda (msg) + (set! count (+1 count)) + (set! sum (+ sum (func msg)))) + expr) + (if (= count 0) + 0 + (exact->inexact (/ sum count)))))