test-of-iterators.lisp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. ;;;; test-of-iterators.lisp
  2. (load "iterators.lisp")
  3. (defpackage #:iterators-simple.tests
  4. (:use :cl
  5. :iterators-simple)
  6. (:export #:number-lines
  7. #:count-lines
  8. #:count-characters))
  9. (in-package #:iterators-simple.tests)
  10. ;;; "test-of-iterators" goes here. Hacks and glory await!
  11. (defun number-lines (file &optional (output-stream t))
  12. "Print each line prepended with its number."
  13. (let ((n 1)
  14. (get-line (create-iterator :stream file :line)))
  15. (flet ((get-line ()
  16. (call-iterator get-line)))
  17. (loop for line = (get-line)
  18. if (not line) do (return)
  19. else
  20. do (progn
  21. (format output-stream "~4d: ~a~%" n line)
  22. (incf n 1))))))
  23. (defun number-of-lines (file)
  24. "Count the number of lines in a file."
  25. (let ((n 0)
  26. (get-line (create-iterator :stream file :line)))
  27. (flet ((get-line ()
  28. (call-iterator get-line)))
  29. (loop for line = (get-line)
  30. if (not line) do (return)
  31. else
  32. do (incf n 1))
  33. n)))
  34. (defun count-characters (file)
  35. "This counts the number of characters in a file."
  36. (let ((n 0)
  37. (get-character (create-iterator :stream file :char)))
  38. (flet ((get-character ()
  39. (call-iterator get-character)))
  40. (loop for char = (get-character)
  41. if (not char) do (return)
  42. else
  43. do (incf n 1))
  44. n)))