Re: How to improve my summarizing code
- From: Pascal Bourguignon <usenet@xxxxxxxxxxxxxxxxx>
- Date: Sat, 04 Feb 2006 07:31:20 +0100
Geoffrey King <lordergeoffrey@xxxxxxxxxxxxxxxx> writes:
I am trying to 'correct' my c++/java training and get the lisp
feeling. Below is my third attempt at the problem. I am looking for
pointers on improving it, or even better changing my approach. Any
comments are appreciated.
The problem is summarizing or rolling up numeric values found in a in
a list of lists. An example of the structure would be:
((a b 1) (a b 2) (a c 3) (a c 7))
Yes it is a simplified database table style structure and does have
uniform 'columns'. The result of summarizing should produce a result
of:
((a b 3) (a c 10))
One other criteria is that result should conform to this general
structure, otherwise a hash may tempt me.
If you don't need the result sorted, then using a hash table you can
reach a complexity of O(n) while sort is O(n*log(n)). And when you
use APPEND in a loop, you usually get O(n²).
(defun key (row) (first row))
(defun value (row) (second row))
(defun make-row (key value) (list key value))
(defun summarize (rows)
(loop with h = (make-hash-table :test (function equal)) ; for complex keys
for row in rows
do (incf (gethash (key row) h 0) (value row))
finally (let ((result '()))
(maphash (lambda (k v) (push (make-row k v) result)) h)
(return result))))
(summarize '((c 7) (a 1) (a 3) (b 1) (b 10) (b 100)))
--> ((C 7) (A 4) (B 111))
(defun key (row) (subseq row 0 2))
(defun value (row) (elt row 2))
(defun make-row (key value) (append key (list value)))
(summarize '((a b 1) (a b 2) (a c 3) (a c 7)))
--> ((A B 3) (A C 10))
You probably need a more sophisticated data structure for rows,
involving a vector of fields and some meta-data.
Also, you can easily keep more data than the sum in the hash table:
....
(incf (cdr (gethash (key row) h (cons (data row) 0))) (value row))
....
(push (make-row k (car v) (cdr v)) result)
....
Or you can accumulate several columns at the same time, etc...
--
__Pascal Bourguignon__ http://www.informatimago.com/
"Specifications are for the weak and timid!"
.
- Follow-Ups:
- Re: How to improve my summarizing code
- From: Geoffrey King
- Re: How to improve my summarizing code
- References:
- How to improve my summarizing code
- From: Geoffrey King
- How to improve my summarizing code
- Prev by Date: Re: Interview with Samantha Kleinberg on CL-GODB, Common Lisp & Bioinformatics
- Next by Date: Re: CLISP [1]> ?
- Previous by thread: How to improve my summarizing code
- Next by thread: Re: How to improve my summarizing code
- Index(es):
Relevant Pages
|