Created
January 25, 2023 02:14
-
-
Save appblue/bf4672b1eb33377c1ce875ae6bd1eef2 to your computer and use it in GitHub Desktop.
Rudimentary file reading and string processing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;;Sets up the file for use | |
(defparameter *my-file* (open "~/_work_/parse.lisp")) | |
;;;Reads the entire file printing each line, (loopfile file) | |
(defun loopfile (x) | |
(loop for line = (read-line x nil) | |
while line do (print line))) | |
;; Strings are vectors. Vectors are sequences. | |
;; Strings are vectors and sequences. So all operations for them apply: | |
;; Find an item: | |
;; CL-USER 59 > (find #\X "fooXbar") | |
;; #\X | |
;; CL-USER 60 > (find #\x "fooXbar") | |
;; NIL | |
;; Find the position of an item: | |
;; CL-USER 61 > (position #\x "fooXbar") | |
;; NIL | |
;; CL-USER 62 > (position #\X "fooXbar") | |
;; 3 | |
;; Search for a subsequence: | |
;; CL-USER 63 > (search "X" "fooXbar") | |
;; 3 | |
;; CL-USER 64 > (search "x" "fooXbar") | |
;; NIL | |
;; Default test is EQL | |
;; All functions use EQL as the default test, but you can use another one - for example a case-insensitive find would be: | |
;; CL-USER 65 > (find #\x "fooXbar" :test #'char-equal) | |
;; #\X | |
;;;Sets up the file for use | |
(defparameter *my-file* (open "~/_work_/parse.lisp")) | |
;;;Reads the entire file printing each line, (loopfile file) | |
(defun loopfile (x) | |
(loop for line = (read-line x nil) | |
while line do (print line))) | |
;; Strings are vectors. Vectors are sequences. | |
;; Strings are vectors and sequences. So all operations for them apply: | |
;; Find an item: | |
;; CL-USER 59 > (find #\X "fooXbar") | |
;; #\X | |
;; CL-USER 60 > (find #\x "fooXbar") | |
;; NIL | |
;; Find the position of an item: | |
;; CL-USER 61 > (position #\x "fooXbar") | |
;; NIL | |
;; CL-USER 62 > (position #\X "fooXbar") | |
;; 3 | |
;; Search for a subsequence: | |
;; CL-USER 63 > (search "X" "fooXbar") | |
;; 3 | |
;; CL-USER 64 > (search "x" "fooXbar") | |
;; NIL | |
;; Default test is EQL | |
;; All functions use EQL as the default test, but you can use another one - for example a case-insensitive find would be: | |
;; CL-USER 65 > (find #\x "fooXbar" :test #'char-equal) | |
;; #\X | |
(defun all (l) | |
(reduce #'(lambda (x y) (and x y)) l :initial-value t)) | |
(defun any (l) | |
(reduce #'(lambda (x y) (or x y)) l :initial-value nil)) | |
(defun contains (s c) | |
(any (map 'vector #'(lambda (x) (char= x c)) s))) | |
;;;Reads the entire file printing the line when < is found | |
(defun loopfile_xml (x) | |
(loop for line = (read-line x nil) | |
while line | |
do (if (position #\< line) | |
(print line)))) | |
(defun all (l) | |
(reduce #'(lambda (x y) (and x y)) l :initial-value t)) | |
(defun any (l) | |
(reduce #'(lambda (x y) (or x y)) l :initial-value nil)) | |
(defun contains (s c) | |
(any (map 'vector #'(lambda (x) (char= x c)) s))) | |
;;;Reads the entire file printing the line when < is found | |
(defun loopfile_xml (x) | |
(loop for line = (read-line x nil) | |
while line | |
do (if (position #\< line) | |
(print line)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment