123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- ;;;; test-of-iterators.lisp
- (load "iterators.lisp")
- (defpackage #:iterators-simple.tests
- (:use :cl
- :iterators-simple)
- (:export #:number-lines
- #:count-lines
- #:count-characters))
- (in-package #:iterators-simple.tests)
- ;;; "test-of-iterators" goes here. Hacks and glory await!
- (defun number-lines (file &optional (output-stream t))
- "Print each line prepended with its number."
- (let ((n 1)
- (get-line (create-iterator :stream file :line)))
- (flet ((get-line ()
- (call-iterator get-line)))
- (loop for line = (get-line)
- if (not line) do (return)
- else
- do (progn
- (format output-stream "~4d: ~a~%" n line)
- (incf n 1))))))
- (defun number-of-lines (file)
- "Count the number of lines in a file."
- (let ((n 0)
- (get-line (create-iterator :stream file :line)))
- (flet ((get-line ()
- (call-iterator get-line)))
- (loop for line = (get-line)
- if (not line) do (return)
- else
- do (incf n 1))
- n)))
- (defun count-characters (file)
- "This counts the number of characters in a file."
- (let ((n 0)
- (get-character (create-iterator :stream file :char)))
- (flet ((get-character ()
- (call-iterator get-character)))
- (loop for char = (get-character)
- if (not char) do (return)
- else
- do (incf n 1))
- n)))
|