From ec6a85fcb588dc095f02a433499d3c3fec0d7367 Mon Sep 17 00:00:00 2001 From: Magnar Sveen Date: Sun, 21 Oct 2012 18:45:42 +0200 Subject: [PATCH] Add threading macro !!-> with `it` as placeholder. --- README.md | 16 +++++++++++++++- bang.el | 11 +++++++++++ examples.el | 10 ++++++++-- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8cbfadd..5441fd2 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ Or you can just dump `bang.el` in your load path somewhere. * [!rpartial](#rpartial-fn-rest-args) `(fn &rest args)` * [!->](#x-optional-form-rest-more) `(x &optional form &rest more)` * [!->>](#x-form-rest-more) `(x form &rest more)` +* [!!->](#x-form-rest-more) `(x form &rest more)` * [!difference](#difference-list-list2) `(list list2)` * [!intersection](#intersection-list-list2) `(list list2)` * [!distinct](#distinct-list) `(list)` @@ -179,8 +180,8 @@ through the `rep` function. ```cl (!replace-where 'even? 'square '(1 2 3 4)) ;; => '(1 4 3 16) -(!replace-where (lambda (n) (= n 3)) (lambda (n) 0) '(1 2 3 4)) ;; => '(1 2 0 4) (!!replace-where (> it 2) (* it it) '(1 2 3 4)) ;; => '(1 2 9 16) +(!!replace-where (= it 2) 17 '(1 2 3 4)) ;; => '(1 17 3 4) ``` ### !first `(fn list)` @@ -247,6 +248,19 @@ last item in second form, etc. (!->> 5 (- 8)) ;; => 3 ``` +### !!-> `(x form &rest more)` + +Threads the expr through the forms. Inserts `x` at the position +signified by the token `it` in the first form. If there are more +forms, inserts the first form at the position signified by `it` +in in second form, etc. + +```cl +(!!-> "def" (concat "abc" it "ghi")) ;; => "abcdefghi" +(!!-> "def" (concat "abc" it "ghi") (upcase it)) ;; => "ABCDEFGHI" +(!!-> "def" (concat "abc" it "ghi") upcase) ;; => "ABCDEFGHI" +``` + ### !difference `(list list2)` Return a new list with only the members of `list` that are not in `list2`. diff --git a/bang.el b/bang.el index 065662d..4f5c404 100644 --- a/bang.el +++ b/bang.el @@ -215,6 +215,17 @@ last item in second form, etc." (list form x)) `(!->> (!->> ,x ,form) ,@more))) +(defmacro !!-> (x form &rest more) + "Threads the expr through the forms. Inserts X at the position +signified by the token `it' in the first form. If there are more +forms, inserts the first form at the position signified by `it' +in in second form, etc." + (if (null more) + (if (listp form) + (!!replace-where (eq it 'it) x form) + (list form x)) + `(!!-> (!!-> ,x ,form) ,@more))) + (defun !distinct (list) "Return a new list with all duplicates removed. The test for equality is done with `equal', diff --git a/examples.el b/examples.el index 6bfec6f..d894a4d 100644 --- a/examples.el +++ b/examples.el @@ -65,8 +65,9 @@ (defexamples !replace-where (!replace-where 'even? 'square '(1 2 3 4)) => '(1 4 3 16) - (!replace-where (lambda (n) (= n 3)) (lambda (n) 0) '(1 2 3 4)) => '(1 2 0 4) - (!!replace-where (> it 2) (* it it) '(1 2 3 4)) => '(1 2 9 16)) + (!!replace-where (> it 2) (* it it) '(1 2 3 4)) => '(1 2 9 16) + (!!replace-where (= it 2) 17 '(1 2 3 4)) => '(1 17 3 4) + (!replace-where (lambda (n) (= n 3)) (lambda (n) 0) '(1 2 3 4)) => '(1 2 0 4)) (defexamples !first (!first 'even? '(1 2 3)) => 2 @@ -95,6 +96,11 @@ (!->> 5 (- 8)) => 3 (!->> 5 (- 3) square) => 4) +(defexamples !!-> + (!!-> "def" (concat "abc" it "ghi")) => "abcdefghi" + (!!-> "def" (concat "abc" it "ghi") (upcase it)) => "ABCDEFGHI" + (!!-> "def" (concat "abc" it "ghi") upcase) => "ABCDEFGHI") + (defexamples !difference (!difference '() '()) => '() (!difference '(1 2 3) '(4 5 6)) => '(1 2 3)