[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: One of the best reasons to learn SERIES

William James

5/2/2015 7:34:00 AM

Jason Trenouth wrote:

> Fernando> Here's a great example of why SERIES is so cool:
> Fernando>
> Fernando> (defun dump-htable-of-htables (tb)
> Fernando> (iterate (((key val) (scan-hash tb)))
> Fernando> (format t "~%Key: ~S Value: ~%" key)
> Fernando> (iterate (((sub-key sub-val) (scan-hash val)))
> Fernando> (format t "~% Key: ~S Value: ~S ~%" sub-key sub-val))))
> Fernando>
> Fernando> Write the same using WITH-HASH-TABLE-ITERATOR instead,
> Fernando> and you'll see..
>
> Alternatively (in Common Lisp):
>
> [1]
>
> (defun dump-htable-of-htables (tb)
> (loop for key being each hash-key of db using (hash-value val) do
> (format t "~%Key: ~S Value: ~%" key)
> (loop for sub-key being each hash-key of val using (hash-value sub-val) do
> (format t "~% Key: ~S Value: ~S ~%" sub-key sub-val))))

Testing with SBCL:

* (DUMP-HTABLE-OF-HTABLES 2)

debugger invoked on a UNBOUND-VARIABLE: The variable DB is unbound.

>
> [2]
>
> (defun dump-htable-of-htables (tb)
> (maphash #'(lambda (key val)
> (format t "~%Key: ~S Value: ~%" key)
> (maphash #'(lambda (sub-key sub-val)
> (format t "~% Key: ~S Value: ~S ~%" sub-key sub-val))
> val))
> db))

Testing with SBCL:

* (DUMP-HTABLE-OF-HTABLES 2)

debugger invoked on a UNBOUND-VARIABLE: The variable DB is unbound.

> --
> _____________________________________________________________________________
> | Jason Trenouth, | EMAIL: jason@uk.co.harlequin |
> | Harlequin Ltd, Barrington Hall, | TEL: (0223) 872522 |
> | Barrington, Cambridge CB2 5RG, UK | FAX: (0223) 872519 |


This blooming idiot posted both of those functions without testing
either one. The arrogance and stupidity of CL worshippers is unsurpassed.


Gauche Scheme:

(define htable
(alist->hash-table
(list
(cons 'a (alist->hash-table '((m . 22) (n . 33))))
(cons 'b (alist->hash-table '((u . 88) (v . 99)))))))

(define (dump-table-of-tables table)
(hash-table-for-each table
(lambda (k v)
(print #"Key: ~k Value: ")
(hash-table-for-each v
(lambda (k2 v2)
(print #" Key: ~k2 Value: ~v2"))))))

gosh> (dump-table-of-tables htable)
Key: b Value:
Key: v Value: 99
Key: u Value: 88
Key: a Value:
Key: m Value: 22
Key: n Value: 33

--
Latinos beat, threaten to rape and kill white schoolgirl after finding out she
opposes illegal immigration. -- violenceagainstwhites.wordpress.com/2013/04/29/
Pakistani rapists in the U.K.:
http://www.amren.com/news/2011/08/girl_o...