Advertisement
eudoxia

eutils.lisp file

May 7th, 2011
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 6.75 KB | None | 0 0
  1. (defun flatten (tree)
  2.     "David S. Toutersky."
  3.     (cond ((atom tree) (list tree))
  4.         (t (append (flatten (car tree))
  5.                    (flatten (cdr tree))))))
  6.                    
  7. (defun pairings (list &optional &key remove-nil)
  8.     "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"
  9.     (let ((out (loop for n from 0 to (length list) append (list (list (nth n list) (nth (1+ n) list))) do (incf n))))
  10.         (if (not (null remove-nil))
  11.             (setf out (remove-if #'(lambda (x) (or (null (car x)) (null (cadr x)))) out)))
  12.         (append out)))
  13.  
  14. (defun sequence-from-pairings (input-list pairing-list)
  15.     "Take an INPUT-LIST, split it into many lists using the pair values (START, END) from the lists in PAIRING-LIST."
  16.     (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)))))
  17.  
  18. (defun split-string (string char)
  19.   "Thanks to the Common Lisp Cookbook. http://cl-cookbook.sourceforge.net/strings.html#reverse"
  20.     (let* ((parsed-string (loop for i = 0 then (1+ j)
  21.           as j = (position char string :start i)
  22.           collect (subseq string i j)
  23.           while j)))
  24.       (remove-if #'(lambda (x) (equalp x "")) parsed-string)))
  25.  
  26.      
  27. (defun tokenize (string replacement &key (test #'char=))
  28.     "Thanks to the Common Lisp Cookbook. http://cl-cookbook.sourceforge.net/strings.html#manip
  29.     Returns a new string in which all the occurences of the part
  30.     is replaced with replacement."
  31.     (with-output-to-string (out)
  32.       (loop with part-length = (length "(")
  33.             for old-pos = 0 then (+ pos part-length)
  34.             for pos = (search "(" string
  35.                               :start2 old-pos
  36.                               :test test)
  37.             do (write-string string out
  38.                              :start old-pos
  39.                              :end (or pos (length string)))
  40.             when pos do (write-string " ( " out)
  41.             while pos))
  42.     (with-output-to-string (out)
  43.       (loop with part-length = (length ")")
  44.             for old-pos = 0 then (+ pos part-length)
  45.             for pos = (search ")" string
  46.                               :start2 old-pos
  47.                               :test test)
  48.             do (write-string string out
  49.                              :start old-pos
  50.                              :end (or pos (length string)))
  51.             when pos do (write-string " ) " out)
  52.             while pos))
  53. )
  54.  
  55. (defun trim-excess-chars (string char)
  56.     "Remove all the instances of CHAR from STRING. CHAR must be a single-character string, not a character."
  57.     (coerce (delete (car (coerce char 'list))  (coerce string 'list)) 'string))
  58.    
  59. (defun list-subseq (list start &optional end)
  60.     "Return the items from a LIST between START and (Optionally, defaults to length of string) END."
  61.     (if end (loop for a from start to end collecting (nth a list))
  62.             (loop for a from start to (1- (length list)) collecting (nth a list))))            
  63.  
  64. (defun collapse-list (list start end)
  65.     "Replace all the items from a LIST between START and END with a list containing said items."
  66.     (nconc (list-subseq list 0 (1- start))
  67.            (list (list-subseq list start end))
  68.            (list-subseq list (1+ end) (1- (length list)))))
  69.            
  70.            
  71. (defun enclose-delimiters (list delimiter &optional expand-left expand-right)
  72.    "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
  73.   enclose items up to a certain number to the left or right (Respectively) into the output list."
  74.    (let* ((delimiter-list '()))
  75.    (loop for n from 0 to (length list) do
  76.         (if (equalp delimiter (nth n list))
  77.             (push n delimiter-list)
  78.             ))
  79.     (setf delimiter-list (append (list (first (reverse delimiter-list))) (last (reverse delimiter-list))))
  80.     (if expand-left  (decf (car delimiter-list) expand-left))
  81.         (if (< (car delimiter-list) 0) (setf (car delimiter-list) 0))
  82.     (if expand-right (incf (car (last delimiter-list)) expand-right))
  83.         (if (> (car (last delimiter-list)) (length list)) (setf (car (last delimiter-list)) (length list)))
  84.     (collapse-list list (car delimiter-list) (car (last delimiter-list)))))
  85.    
  86. (defun collect-item-positions (list item &optional &key append-beginning append-end single?)
  87.     "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."
  88.     (let ((delimiter-positions '()))
  89.         (loop for n from 0 to (length list) do
  90.             (if (equalp item (nth n list))
  91.                 (progn  (setf delimiter-positions (nconc delimiter-positions (list n)))
  92.                         (if single? (return n)))))
  93.         (append delimiter-positions)))
  94.  
  95. (defun list-split-multiple-delimiters (list delimiter)
  96.     "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."
  97.     (let ((output-list list)
  98.           (item-positions '()))
  99.         (loop for n from 0 to (length output-list) do
  100.             (cond ((equalp delimiter (nth n output-list))
  101.                     (setf output-list (nconc  (list-subseq output-list 0 (1- n))
  102.                                           (list delimiter delimiter)
  103.                                           (list-subseq output-list (1+ n))))
  104.                     (incf n 1)
  105.                     (setf output-list (remove-if #'null (flatten output-list))))))
  106.         (setf output-list (nconc (list delimiter) output-list (list delimiter)))
  107.         (setf item-positions (collect-item-positions output-list delimiter))
  108.         (setf item-positions (pairings item-positions :remove-nil t))
  109.         (setf output-list (sequence-from-pairings output-list item-positions))
  110.         (setf output-list (reverse (rest (reverse (loop for n from 0 to (length output-list) collecting
  111.             (list-subseq (nth n output-list) 1 (- (length (nth n output-list)) 2)))))))
  112. ))
  113.            
  114. (defun concatenate-list-of-strings (list)
  115.     "Concatenate the strings in LIST into a single string (hurr durr)."
  116.         (apply #'concatenate 'string list))
  117.  
  118. (defun concatenate-strings-with-spaces (list)
  119.     "Concatenate the strings in LIST into a single string but separate each string them with a space. Except the last one."
  120.     (loop for n from 0 to (- (length list) 2) do (setf (nth n list) (concatenate 'string (nth n list) " ")))
  121.     (setf list (concatenate-list-of-strings list)))
  122.    
  123. ;;TODO: Add optional :test=#' arguments to a bunch of these
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement