Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 4.13 KB | None | 0 0
  1. ;;;    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;;       Collection of usefull tools ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3. ;;;    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4.  
  5. (cl:defpackage tools
  6.   (:use cl)
  7.   (:export compiler-policy doc))
  8.  
  9. (in-package tools)
  10.  
  11. (defun compiler-policy (safety debug speed)
  12.   "Sets SBCL's compiler policies safety, debug and speed."
  13.   (sb-ext:restrict-compiler-policy 'safety safety)
  14.   (sb-ext:restrict-compiler-policy 'debug debug)
  15.   (sb-ext:restrict-compiler-policy 'speed speed))
  16.  
  17. (defmacro doc (function)
  18.   "Looking up documentation of function."
  19.   `(documentation ',function 'function))
  20.  
  21. (defun remove* (symbol tree)
  22.   "Removes symbol from tree."
  23.   (cond
  24.     ((null tree) tree)
  25.     ((atom (car tree))
  26.      (if (equalp symbol (car tree))
  27.          (remove* symbol (cdr tree))
  28.          (cons (car tree) (remove* symbol (cdr tree)))))
  29.     (t (cons (remove* symbol (car tree))
  30.              (remove* symbol (cdr tree))))))
  31.  
  32. (defun replace* (old new tree)
  33.   "Replaces old with new symbol in tree."
  34.   (cond
  35.     ((null tree) tree)
  36.     ((atom (car tree))
  37.      (if (equalp old (car tree))
  38.          (cons new (replace* old new (cdr tree)))
  39.          (cons (car tree) (replace* old new (cdr tree)))))
  40.     (t (cons (replace* old new (car tree))
  41.              (replace* old new (cdr tree))))))
  42.  
  43. (defmacro once-only ((&rest names) &body body)
  44.   "Macro once-only from Peter Seibel in PCL."
  45.   (let ((gensyms (loop for n in names collect (gensym))))
  46.     `(let (,@(loop for g in gensyms collect `(,g (gensym))))
  47.        `(let (,,@(loop for g in gensyms for n in names collect ``(,,g ,,n)))
  48.           ,(let (,@(loop for n in names for g in gensyms collect `(,n ,g)))
  49.                 ,@body)))))
  50.  
  51.  
  52. (defun partial (fn &rest args)
  53.   "Returns function FN with partial binded parameters ARGS."
  54.   (lambda (&rest more-args)
  55.     (apply fn (append args more-args))))
  56.  
  57. (defmacro with-partial-macro (name macro list-of-args &body body)
  58.   "TODO: docu"
  59.   `(macrolet ((,name (more-args)
  60.                 `(,,macro ,,@list-of-args ,@more-args)))
  61.      ,@body))
  62.  
  63. (defmacro testmacro (&rest args)
  64.   `(+ ,@args))
  65.  
  66. ;;; TOOLS> (macroexpand-1 '(with-partial-macro partial-plus testmacro (1 2 3)
  67. ;;;                         (partial-plus 4 5 6)))
  68. ;;;
  69. ;;; (MACROLET ((PARTIAL-PLUS (MORE-ARGS)
  70. ;;;              `(,TESTMACRO ,1 ,2 ,3 ,@MORE-ARGS)))
  71. ;;;   (PARTIAL-PLUS 4 5 6))
  72.  
  73.  
  74. ;;; should expand to:
  75. ;;; (macrolet ((meinname (more-args)
  76. ;;;   '(testmacro 1 2 3 ,@more-args))))
  77.  
  78. (defmacro test (list-of-args &body body)
  79.   `(macrolet ((mytest (&args more-args)
  80.               `(testmacro ,,@list-of-args ,@more-args)))
  81.      ,body))
  82.  
  83.  
  84. (defmacro my-destructuring-bind (lambda-list list &body body)
  85.   "Allows to use the symbol _ as a placeholder
  86. when using destructurin-bind.
  87. this
  88. (macroexpand-1 '(my-destructuring-bind (s1 _ s2 &optional _) '(1 2 3)
  89.                  (list s1 s2)))
  90. expands to
  91. (DESTRUCTURING-BIND
  92.     (S1 #:G1057 S2 &OPTIONAL #:G1058)
  93.     '(1 2 3)
  94.   (DECLARE (IGNORE #:G1057 #:G1058))
  95.   (LIST S1 S2))"
  96.   (let (new-lambda-list ignore-symbols-list)
  97.     (dolist (symbol lambda-list)
  98.       (if (eq '_ symbol)
  99.           (let ((gsym (gensym)))
  100.             (push gsym ignore-symbols-list)
  101.             (push gsym new-lambda-list))
  102.           (push symbol new-lambda-list)))
  103.     `(destructuring-bind ,(nreverse new-lambda-list) ,list
  104.          (declare (ignore ,@(nreverse ignore-symbols-list)))
  105.          ,@body)))
  106.  
  107. ;;;    macro for defining recursive expressions
  108. (defmacro lambda*% (args &rest body)
  109.   `(lambda ,args
  110.      (labels ((recur ,args ,@body))
  111.        (recur ,@args))))
  112.  
  113. (defmacro lambda* (fname args &rest body)
  114.   `(lambda ,args
  115.      (labels ((,fname ,args ,@body))
  116.        (fname ,@args))))
  117.  
  118. (defmacro lambda*%% (fname args &rest body)
  119.   (let ((evaluated-args (gensym)))
  120.     `(lambda ,args
  121.        (let ((evaluated-args ,args))
  122.          (labels ((,fname ,evaluated-args ,@body))
  123.            (fname ,@evaluated-args))))))
  124.  
  125. (defmacro lambda*%%% (fname args &rest body)
  126.   (let ((evaluated-args (gensym)))
  127.     `(lambda ,evaluated-args
  128.        (let ((,evaluated-args args))
  129.          (labels ((,fname ,evaluated-args ,@body))
  130.            (fname ,@evaluated-args))))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement