[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: File handling and list-unique

William James

8/11/2015 8:25:00 AM

Pascal Bourguignon wrote:

> > In order to learn Lisp I'm trying to do some small tasks with it. I have
> > some files, ending with ".tif" or ".jpg". Sometimes a file can be in tif
> > and jpg, e.g. "test.jpg" and "test.tif". Then I want to delete the older
> > file. This is my solution:
> >
> > (setq path "c:/tmp/images/")
> > (setq names
> > (mapcar #'pathname-name
> > (directory (concatenate 'string path "*"))))
> > (setq names
> > (sort names #'string<))
> > (setq unique nil)
> > (dolist (x names)
> > (if (string/= (car unique) x)
> > (setq unique (cons x unique))))
> > (dolist (name unique)
> > (let ((tif (concatenate 'string path name ".tif"))
> > (jpg (concatenate 'string path name ".jpg")))
> > (if (and (probe-file jpg) (probe-file tif))
> > (if (< (file-write-date tif) (file-write-date jpg))
> > (delete-file tif)
> > (delete-file jpg)
> > ))))
> >
> > Looks a bit complicated, but I think in Java it would be more
> > complicated, but in Bash or Perl it would be shorter (but not so easy to
> > read). Any comments how to write it better?
>
> (defparameter path "c:/tmp/images/")
> (dolist (name (delete-duplicates
> (mapcar (function pathname-name)
> (append
> (directory (concatenate 'string path "*.jpg"))
> (directory (concatenate 'string path "*.tif"))))))
> (mapcar (function delete-file)
> (cdr (sort (directory (concatenate 'string path name ".*"))
> (lambda (a b) (>= (file-write-date a)
> (file-write-date b)))))))

Gauche Scheme:

(use gauche.collection :only (group-collection))
(use file.util :only (file-mtime delete-file))

(define path "/info/tmp")
(define names (filter #/[.](tif|jpg)$/ (glob (string-append path "/*"))))

(for-each
(lambda (group)
(when (> (length group) 1)
(delete-file (car (sort group < file-mtime)))))
(group-collection names :key (^x (rxmatch->string #/.*[.]/ x))
:test string=?))

--
He has nothing but kind sentiments for those who would destroy his home and
family.... He is universally tolerant.... If he has any principles, he keeps
them well concealed.... He is, to the extent of his abilities, exactly like
the next citizen, who, he trusts, is trying to be exactly like him: a faceless,
characterless putty-man. --- Father Feeney; "Should Hate Be Outlawed?"