From 9ff9fd06dae73aad20cf21d8eb2c81d9a2bca4a2 Mon Sep 17 00:00:00 2001 From: Magnar Sveen Date: Mon, 22 Oct 2012 16:02:57 +0200 Subject: [PATCH] Move functions that are not strictly list related last. --- README.md | 143 ++++++++++++++++++++++++++-------------------------- dash.el | 3 +- examples.el | 57 ++++++++++----------- 3 files changed, 100 insertions(+), 103 deletions(-) diff --git a/README.md b/README.md index 58da129..6e55e09 100644 --- a/README.md +++ b/README.md @@ -30,11 +30,6 @@ Or you can just dump `dash.el` in your load path somewhere. * [-interpose](#interpose-sep-list) `(sep list)` * [-replace-where](#replace-where-pred-rep-list) `(pred rep list)` * [-first](#first-fn-list) `(fn list)` -* [-partial](#partial-fn-rest-args) `(fn &rest args)` -* [-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)` @@ -42,6 +37,11 @@ Or you can just dump `dash.el` in your load path somewhere. * [-any?](#any-fn-list) `(fn list)` * [-all?](#all-fn-list) `(fn list)` * [-each](#each-list-fn) `(list fn)` +* [-partial](#partial-fn-rest-args) `(fn &rest args)` +* [-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)` There are also anaphoric versions of these functions where that makes sense, prefixed with two dashs instead of one. @@ -158,8 +158,7 @@ Takes a nested list `l` and returns its contents as a single, flat list. ### -concat `(&rest lists)` -Returns a new list with the concatenation of the elements in -the supplied `lists`. +Returns a new list with the concatenation of the elements in the supplied `lists`. ```cl (-concat '(1)) ;; => '(1) @@ -269,71 +268,6 @@ To get the first item in the list no questions asked, use `car`. (--first (> it 2) '(1 2 3)) ;; => 3 ``` -### -partial `(fn &rest args)` - -Takes a function `fn` and fewer than the normal arguments to `fn`, -and returns a fn that takes a variable number of additional `args`. -When called, the returned function calls `fn` with `args` first and -then additional args. - -```cl -(funcall (-partial '- 5) 3) ;; => 2 -(funcall (-partial '+ 5 2) 3) ;; => 10 -``` - -### -rpartial `(fn &rest args)` - -Takes a function `fn` and fewer than the normal arguments to `fn`, -and returns a fn that takes a variable number of additional `args`. -When called, the returned function calls `fn` with the additional -args first and then `args`. - -Requires Emacs 24 or higher. - -```cl -(funcall (-rpartial '- 5) 8) ;; => 3 -(funcall (-rpartial '- 5 2) 10) ;; => 3 -``` - -### -> `(x &optional form &rest more)` - -Threads the expr through the forms. Inserts `x` as the second -item in the first form, making a list of it if it is not a list -already. If there are more forms, inserts the first form as the -second item in second form, etc. - -```cl -(-> "Abc") ;; => "Abc" -(-> "Abc" (concat "def")) ;; => "Abcdef" -(-> "Abc" (concat "def") (concat "ghi")) ;; => "Abcdefghi" -``` - -### ->> `(x form &rest more)` - -Threads the expr through the forms. Inserts `x` as the last item -in the first form, making a list of it if it is not a list -already. If there are more forms, inserts the first form as the -last item in second form, etc. - -```cl -(->> "Abc" (concat "def")) ;; => "defAbc" -(->> "Abc" (concat "def") (concat "ghi")) ;; => "ghidefAbc" -(->> 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`. @@ -415,6 +349,71 @@ Calls `fn` with every item in `list`. Returns nil, used for side-effects only. (let (s) (--each '(1 2 3) (setq s (cons it s))) s) ;; => '(3 2 1) ``` +### -partial `(fn &rest args)` + +Takes a function `fn` and fewer than the normal arguments to `fn`, +and returns a fn that takes a variable number of additional `args`. +When called, the returned function calls `fn` with `args` first and +then additional args. + +```cl +(funcall (-partial '- 5) 3) ;; => 2 +(funcall (-partial '+ 5 2) 3) ;; => 10 +``` + +### -rpartial `(fn &rest args)` + +Takes a function `fn` and fewer than the normal arguments to `fn`, +and returns a fn that takes a variable number of additional `args`. +When called, the returned function calls `fn` with the additional +args first and then `args`. + +Requires Emacs 24 or higher. + +```cl +(funcall (-rpartial '- 5) 8) ;; => 3 +(funcall (-rpartial '- 5 2) 10) ;; => 3 +``` + +### -> `(x &optional form &rest more)` + +Threads the expr through the forms. Inserts `x` as the second +item in the first form, making a list of it if it is not a list +already. If there are more forms, inserts the first form as the +second item in second form, etc. + +```cl +(-> "Abc") ;; => "Abc" +(-> "Abc" (concat "def")) ;; => "Abcdef" +(-> "Abc" (concat "def") (concat "ghi")) ;; => "Abcdefghi" +``` + +### ->> `(x form &rest more)` + +Threads the expr through the forms. Inserts `x` as the last item +in the first form, making a list of it if it is not a list +already. If there are more forms, inserts the first form as the +last item in second form, etc. + +```cl +(->> "Abc" (concat "def")) ;; => "defAbc" +(->> "Abc" (concat "def") (concat "ghi")) ;; => "ghidefAbc" +(->> 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" +``` + ## Development diff --git a/dash.el b/dash.el index 392019d..6e3d266 100644 --- a/dash.el +++ b/dash.el @@ -137,8 +137,7 @@ Alias: `-reject'" (list l))) (defun -concat (&rest lists) - "Returns a new list with the concatenation of the elements in -the supplied LISTS." + "Returns a new list with the concatenation of the elements in the supplied LISTS." (apply 'append lists)) (defmacro --mapcat (form list) diff --git a/examples.el b/examples.el index fb03e5c..438f82b 100644 --- a/examples.el +++ b/examples.el @@ -105,33 +105,6 @@ (-first 'even? '(1 3 5)) => nil (--first (> it 2) '(1 2 3)) => 3) -(defexamples -partial - (funcall (-partial '- 5) 3) => 2 - (funcall (-partial '+ 5 2) 3) => 10) - -(unless (version< emacs-version "24") - (defexamples -rpartial - (funcall (-rpartial '- 5) 8) => 3 - (funcall (-rpartial '- 5 2) 10) => 3)) - -(defexamples -> - (-> "Abc") => "Abc" - (-> "Abc" (concat "def")) => "Abcdef" - (-> "Abc" (concat "def") (concat "ghi")) => "Abcdefghi" - (-> 5 square) => 25 - (-> 5 (+ 3) square) => 64) - -(defexamples ->> - (->> "Abc" (concat "def")) => "defAbc" - (->> "Abc" (concat "def") (concat "ghi")) => "ghidefAbc" - (->> 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) @@ -167,5 +140,31 @@ (let (s) (-each '(1 2 3) (lambda (item) (setq s (cons item s))))) => nil (let (s) (-each '(1 2 3) (lambda (item) (setq s (cons item s)))) s) => '(3 2 1) (let (s) (--each '(1 2 3) (setq s (cons it s))) s) => '(3 2 1) - (let (s) (--each (reverse (three-letters)) (setq s (cons it s))) s) => '("A" "B" "C") - ) + (let (s) (--each (reverse (three-letters)) (setq s (cons it s))) s) => '("A" "B" "C")) + +(defexamples -partial + (funcall (-partial '- 5) 3) => 2 + (funcall (-partial '+ 5 2) 3) => 10) + +(unless (version< emacs-version "24") + (defexamples -rpartial + (funcall (-rpartial '- 5) 8) => 3 + (funcall (-rpartial '- 5 2) 10) => 3)) + +(defexamples -> + (-> "Abc") => "Abc" + (-> "Abc" (concat "def")) => "Abcdef" + (-> "Abc" (concat "def") (concat "ghi")) => "Abcdefghi" + (-> 5 square) => 25 + (-> 5 (+ 3) square) => 64) + +(defexamples ->> + (->> "Abc" (concat "def")) => "defAbc" + (->> "Abc" (concat "def") (concat "ghi")) => "ghidefAbc" + (->> 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")