Advertisement
eudoxia

eutils.lisp v1.5

Dec 11th, 2011
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 7.54 KB | None | 0 0
  1. ;(mapcar #'ql:quickload '(qt qt-test qt-repl qt-tutorial cffi named-readtables cl-ppcre alexandria closer-mop iterate trivial-garbage))
  2.  
  3. (defun flatten (tree)
  4.     "David S. Toutersky."
  5.     (cond ((atom tree) (list tree))
  6.         (t (append (flatten (car tree))
  7.                    (flatten (cdr tree))))))
  8.                    
  9. (defmacro nappend (list &rest input)
  10.     `(setf ,list (append ,list ,@input)))
  11.                    
  12. (defun pairings (list &optional &key remove-nil)
  13.     "Turn a list into a list of lists, each with pairs of value (n and n+1 on the index). :REMOVE-NIL, if T, removes all the NIL values. Just bite the bullet and use it"
  14.     (let ((out (loop for n from 0 to (length list) append (list (list (nth n list) (nth (1+ n) list))) do (incf n))))
  15.         (if (not (null remove-nil))
  16.             (setf out (remove-if #'(lambda (x) (or (null (car x)) (null (cadr x)))) out)))
  17.         (append out)))
  18.  
  19. (defun sequence-from-pairings (input-list pairing-list)
  20.     "Take an INPUT-LIST, split it into many lists using the pair values (START, END) from the lists in PAIRING-LIST."
  21.     (loop for n from 0 to (1- (length pairing-list)) collecting (list-subseq input-list (car (nth n pairing-list)) (cadr (nth n pairing-list)))))
  22.  
  23. (defun split-string (string char)
  24.   "Thanks to the Common Lisp Cookbook. http://cl-cookbook.sourceforge.net/strings.html#reverse"
  25.     (let* ((parsed-string (loop for i = 0 then (1+ j)
  26.           as j = (position char string :start i)
  27.           collect (subseq string i j)
  28.           while j)))
  29.       (remove-if #'(lambda (x) (equalp x "")) parsed-string)))
  30.  
  31.      
  32. (defun tokenize (string replacement &key (test #'char=))
  33.     "Thanks to the Common Lisp Cookbook. http://cl-cookbook.sourceforge.net/strings.html#manip
  34.     Returns a new string in which all the occurences of the part
  35.     is replaced with replacement."
  36.     (with-output-to-string (out)
  37.       (loop with part-length = (length "(")
  38.             for old-pos = 0 then (+ pos part-length)
  39.             for pos = (search "(" string
  40.                               :start2 old-pos
  41.                               :test test)
  42.             do (write-string string out
  43.                              :start old-pos
  44.                              :end (or pos (length string)))
  45.             when pos do (write-string " ( " out)
  46.             while pos))
  47.     (with-output-to-string (out)
  48.       (loop with part-length = (length ")")
  49.             for old-pos = 0 then (+ pos part-length)
  50.             for pos = (search ")" string
  51.                               :start2 old-pos
  52.                               :test test)
  53.             do (write-string string out
  54.                              :start old-pos
  55.                              :end (or pos (length string)))
  56.             when pos do (write-string " ) " out)
  57.             while pos))
  58. )
  59.  
  60. (defun trim-excess-chars (string char)
  61.     "Remove all the instances of CHAR from STRING. CHAR must be a single-character string, not a character."
  62.     (coerce (delete (car (coerce char 'list))  (coerce string 'list)) 'string))
  63.    
  64. (defun list-subseq (list start &optional end)
  65.     "Return the items from a LIST between START and (Optionally, defaults to length of string) END."
  66.     (loop for a from start to (if end end (1- (length list))) collecting (nth a list)))
  67.  
  68. (defun string-subseq (string start &optional end)
  69.     "Return the items from a LIST between START and (Optionally, defaults to length of string) END."
  70.     (coerce (loop for a from start to (if end end (1- (length string))) collecting (aref string a)) 'string))
  71.  
  72. (defun collapse-list (list start end)
  73.     "Replace all the items from a LIST between START and END with a list containing said items."
  74.     (nconc (list-subseq list 0 (1- start))
  75.            (list (list-subseq list start end))
  76.            (list-subseq list (1+ end) (1- (length list)))))
  77.            
  78. (defun enclose-delimiters (list delimiter &optional expand-left expand-right)
  79.    "Return a list where all items between the first and last instances of DELIMITER have been enclosed into a list, except the delimiters themselves. OPTIONAL: EXPAND-LEFT and EXPAND-RIGHT
  80.   enclose items up to a certain number to the left or right (Respectively) into the output list."
  81.    (let* ((delimiter-list '()))
  82.    (loop for n from 0 to (length list) do
  83.         (if (equalp delimiter (nth n list))
  84.             (push n delimiter-list)
  85.             ))
  86.     (setf delimiter-list (append (list (first (reverse delimiter-list))) (last (reverse delimiter-list))))
  87.     (if expand-left  (decf (car delimiter-list) expand-left))
  88.         (if (< (car delimiter-list) 0) (setf (car delimiter-list) 0))
  89.     (if expand-right (incf (car (last delimiter-list)) expand-right))
  90.         (if (> (car (last delimiter-list)) (length list)) (setf (car (last delimiter-list)) (length list)))
  91.     (collapse-list list (car delimiter-list) (car (last delimiter-list)))))
  92.    
  93. (defun collect-item-positions (list item &optional &key append-beginning append-end single?)
  94.     "Collect the positions of many instances of ITEM in a LIST. The variable :SINGLE?, if T, tells the interpreter to return after one instance has been found."
  95.     (let ((delimiter-positions '()))
  96.         (loop for n from 0 to (length list) do
  97.             (if (equalp item (nth n list))
  98.                 (progn  (setf delimiter-positions (nconc delimiter-positions (list n)))
  99.                         (if single? (return n)))))
  100.         (append delimiter-positions)))
  101.  
  102. (defun list-split-multiple-delimiters (list delimiter)
  103.     "Split a list into many sublists using the instances of DELIMITER as delimiters. The beginning of the list and endpoint are taken as delimiters too."
  104.     (let ((output-list list)
  105.           (item-positions '()))
  106.         (loop for n from 0 to (length output-list) do
  107.             (cond ((equalp delimiter (nth n output-list))
  108.                     (setf output-list (nconc  (list-subseq output-list 0 (1- n))
  109.                                           (list delimiter delimiter)
  110.                                           (list-subseq output-list (1+ n))))
  111.                     (incf n 1)
  112.                     (setf output-list (remove-if #'null (flatten output-list))))))
  113.         (setf output-list (nconc (list delimiter) output-list (list delimiter)))
  114.         (setf item-positions (collect-item-positions output-list delimiter))
  115.         (setf item-positions (pairings item-positions :remove-nil t))
  116.         (setf output-list (sequence-from-pairings output-list item-positions))
  117.         (setf output-list (reverse (rest (reverse (loop for n from 0 to (length output-list) collecting
  118.             (list-subseq (nth n output-list) 1 (- (length (nth n output-list)) 2)))))))
  119. ))
  120.            
  121. (defun concatenate-list-of-strings (list)
  122.     "Concatenate the strings in LIST into a single string (hurr durr)."
  123.         (apply #'concatenate 'string list))
  124.  
  125. (defun concatenate-strings-with-spaces (list)
  126.     "Concatenate the strings in LIST into a single string but separate each string them with a space. Except the last one."
  127.     (loop for n from 0 to (- (length list) 2) do (setf (nth n list) (concatenate 'string (nth n list) " ")))
  128.     (setf list (concatenate-list-of-strings list)))
  129.    
  130. (defmacro writeline (control-string &optional &rest arguments)
  131.     "Handle output from the interactive fiction engine. By default it works using writeline, though more advanced systems should be made available."
  132.     (if (not (null arguments))
  133.         (append `(format t ,control-string) arguments)
  134.         `(format t ,control-string)))
  135.        
  136. (defmacro qstring (string)
  137.     string)
  138.    
  139. ;;TODO: Add optional :test=#' arguments to a bunch of these
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement