Permalink
Browse files

Added nil safe combinators

  • Loading branch information...
1 parent cda56f2 commit a309645f132c791259a695e2633421925047f199 @sroccaserra committed Jun 15, 2010
Showing with 84 additions and 33 deletions.
  1. +18 −20 smart-compile.el
  2. +30 −10 tools.el
  3. +36 −3 tools.elk
View
@@ -1,5 +1,20 @@
+(require 'tools)
+
(defvar *default-compilation-file* "Makefile")
+(comment
+ "I use it like this"
+ (global-set-key [(f9)] 'smart-compile)
+
+ (eval-after-load \"compile\"
+ '(setq compilation-process-setup-function 'reach-compilation-file))
+
+ (eval-after-load \"compile\"
+ '(define-key
+ compilation-mode-map
+ [remap compile-goto-error]
+ 'compile-goto-error-and-close-compilation-window)))
+
(defun smart-compile ()
"Saves current buffer, and depending on context:
@@ -10,12 +25,7 @@
command.
- otherwise, switches to rake for the current buffer and
- compiles.
-
-I use it like this:
-
-(global-set-key [(f9)] 'smart-compile)
-"
+ compiles."
(interactive)
(save-buffer)
(if eldoc-mode
@@ -40,11 +50,7 @@ I use it like this:
(defun reach-compilation-file ()
"If your compile command containts 'make', goes up in the path
-until it finds a makefile. I use it like this:
-
-(eval-after-load \"compile\"
- '(setq compilation-process-setup-function 'reach-compilation-file))
-"
+until it finds a makefile."
(when (string-match "make"
(car compilation-arguments))
(let ((compilation-file (nearest-compilation-file (expand-file-name default-directory)
@@ -55,15 +61,7 @@ until it finds a makefile. I use it like this:
(defun compile-goto-error-and-close-compilation-window ()
"Useful to close compilation windows, so you have only one
-window open at any time (I set `pop-up-windows' to nil). I use
-it like this:
-
-(eval-after-load \"compile\"
- '(define-key
- compilation-mode-map
- [remap compile-goto-error]
- 'compile-goto-error-and-close-compilation-window))
-"
+window open at any time (I set `pop-up-windows' to nil)."
(interactive)
(compile-goto-error)
(delete-windows-on "*compilation*"))
View
@@ -66,16 +66,36 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Clojure's Trush operators
-(defmacro -> (result &rest body)
- (dolist (form body result)
- (setq form (sequence form)
- result (append (list (car form) result)
- (cdr form)))))
-
-(defmacro ->> (result &rest body)
- (dolist (form body result)
- (setq form (sequence form)
- result (append form (list result)))))
+(defmacro -> (x &optional form &rest more)
+ (cond ((not (null more))
+ `(-> (-> ,x ,form) ,@more))
+ ((not (null form))
+ (if (sequencep form)
+ `(,(first form) ,x ,@(rest form))
+ (list form x)))
+ (t x)))
+
+(defmacro ->> (x form &rest more)
+ (cond ((not (null more)) `(->> (->> ,x ,form) ,@more))
+ (t (if (sequencep form)
+ `(,(first form) ,@(rest form) ,x)
+ (list form x)))))
+
+(defmacro -?> (x form &rest more)
+ (cond ((not (null more)) `(-?> (-?> ,x ,form) ,@more))
+ (t (if (sequencep form)
+ `(if (null ,x) nil
+ (,(first form) ,x ,@(rest form)))
+ `(if (null ,x) nil
+ ,(list form x))))))
+
+(defmacro -?>> (x form &rest more)
+ (cond ((not (null more)) `(-?>> (-?>> ,x ,form) ,@more))
+ (t (if (sequencep form)
+ `(if (null ,x) nil
+ (,(first form) ,@(rest form) ,x))
+ `(if (null ,x) nil
+ ,(list form x))))))
;;;;;;;;;;;;;;;;;;;;
;; Functional tools
View
@@ -62,21 +62,54 @@
;;;;;;;;;;;;;;
;; Trush tests
+(deftest "trush x is x"
+ (assert-equal 1 (-> 1)))
+
(deftest "applying trush to one form"
(assert-equal 5.0
(-> 25 sqrt)))
(deftest "applying trush to two forms"
- (assert-equal 3.0
- (-> 25 sqrt (- 2))))
+ (assert-equal '(- (-> 25 sqrt) 2)
+ (macroexpand '(-> 25 sqrt (- 2))))
+ (assert-equal '(- (sqrt 25) 2)
+ (macroexpand-all '(-> 25 sqrt (- 2)))))
(deftest "chaining trush combinators"
(assert-equal "-2.0"
(-> 25 sqrt (->> (- 3) number-to-string))))
(deftest "threading three forms"
(assert-equal '(* 6 (+ 4 5 (- 2 3 x)))
- (macroexpand '(->> x (- 2 3) (+ 4 5) (* 6)))))
+ (macroexpand-all '(->> x (- 2 3) (+ 4 5) (* 6)))))
+
+(deftest "nil safe trush tests"
+ (assert-equal 5.0
+ (-?> 25 sqrt))
+ (assert-equal nil
+ (-?> nil sqrt))
+ (assert-equal 5.0
+ (-?> 25 (sqrt)))
+ (assert-equal nil
+ (-?> nil (sqrt)))
+ (assert-equal 3.0
+ (-?> 25 sqrt (- 2)))
+ (assert-equal nil
+ (-?> 25 null (- 2))))
+
+(deftest "nil safe trush at end tests"
+ (assert-equal 5.0
+ (-?>> 25 sqrt))
+ (assert-equal nil
+ (-?>> nil sqrt))
+ (assert-equal 5.0
+ (-?>> 25 (sqrt)))
+ (assert-equal nil
+ (-?>> nil (sqrt)))
+ (assert-equal -3.0
+ (-?>> 25 sqrt (- 2)))
+ (assert-equal nil
+ (-?>> 25 null (- 2))))
(deftest "comment returns nil"
(assert-equal nil

0 comments on commit a309645

Please sign in to comment.