Guest User

Untitled

a guest
Apr 27th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. (defpackage :newsgroups
  2. (:use :common-lisp :cl-ppcre :pcall))
  3.  
  4. (in-package :newsgroups)
  5.  
  6. (defun distribution (items)
  7. (loop with result = (make-hash-table :test #'equal)
  8. for item in items
  9. do (setf (gethash item result) (1+ (gethash item result 0)))
  10. finally (return result)))
  11.  
  12. (defun distribution/directory (directory)
  13. (flet ((distribution/file (path)
  14. (distribution (split "\\W+" (nstring-downcase (slurp path))))))
  15. (apply #'merge-with #'+ (pmapcar #'distribution/file (directory directory)))))
  16.  
  17. (defun output-distribution/directory (input-directory output-directory)
  18. (let ((result (loop for key being the hash-key using (hash-value value)
  19. of (distribution/directory input-directory)
  20. collect (list key value))))
  21. (flet ((save-ordered-by (filename function key)
  22. (spit (make-pathname :name filename :defaults output-directory)
  23. (format nil "~{~{~a~16t~a~}~%~}"
  24. (sort (copy-list result) function :key key)))))
  25. (save-ordered-by "descending" #'> #'second)
  26. (save-ordered-by "alphabetical" #'string< #'first))))
  27.  
  28. (defun pmapcar (function list)
  29. (let ((result (mapcar (lambda (e) (pexec (funcall function e))) list)))
  30. (map-into result #'join result)))
  31.  
  32. (defun merge-with (merge-function &rest hash-tables)
  33. (loop with result = (make-hash-table :test #'equal)
  34. for hash in hash-tables
  35. do (merge-into result hash :merge-function merge-function)
  36. finally (return result)))
  37.  
  38. (defun merge-into (into from &key (merge-function #'values))
  39. (flet ((merge-entry (merge-function key value hash)
  40. (multiple-value-bind (into-value value-exists) (gethash key hash)
  41. (if value-exists
  42. (setf (gethash key hash)
  43. (funcall merge-function into-value value))
  44. (setf (gethash key hash) value)))))
  45. (loop for key being the hash-keys using (hash-value value) of from
  46. do (merge-entry merge-function key value into)
  47. finally (return into))))
  48.  
  49. (defun slurp (path)
  50. (with-open-file (stream path)
  51. (let ((string (make-string (file-length stream))))
  52. (values string (read-sequence string stream)))))
  53.  
  54. (defun spit (path string &optional (if-exists :supersede))
  55. (with-open-file (stream path :direction :output
  56. :if-does-not-exist :create
  57. :if-exists if-exists)
  58. (write-string string stream) nil))
Add Comment
Please, Sign In to add comment