diff --git a/docs.md b/docs.md index 54e0239..a4762a0 100644 --- a/docs.md +++ b/docs.md @@ -16,9 +16,8 @@ item, etc. If `list` contains no items, returns `initial-value` and `fn` is not called. ```cl -(!reduce-from (quote +) 7 (quote nil)) ;; => 7 -(!reduce-from (quote +) 7 (quote (1))) ;; => 8 (!reduce-from (quote +) 7 (quote (1 2))) ;; => 10 +(!reduce-from (lambda (memo item) (+ memo item)) 7 (quote (1 2))) ;; => 10 (!!reduce-from (+ acc it) 7 (quote (1 2 3))) ;; => 13 ``` @@ -31,12 +30,9 @@ reduce returns the result of calling `fn` with no arguments. If `list` has only 1 item, it is returned and `fn` is not called. ```cl -(!reduce (quote +) (quote nil)) ;; => 0 -(!reduce (quote +) (quote (1))) ;; => 1 (!reduce (quote +) (quote (1 2))) ;; => 3 (!reduce (lambda (memo item) (format %s-%s memo item)) (quote (1 2 3))) ;; => 1-2-3 (!!reduce (format %s-%s acc it) (quote (1 2 3))) ;; => 1-2-3 -(!!reduce (format %s-%s acc it) (quote nil)) ;; => nil-nil ``` ## !filter `(fn list)` @@ -57,8 +53,6 @@ Returns a new list of the items in `list` for which `fn` returns nil. (!remove (lambda (num) (= 0 (% num 2))) (quote (1 2 3 4))) ;; => (quote (1 3)) (!remove (quote even?) (quote (1 2 3 4))) ;; => (quote (1 3)) (!!remove (= 0 (% it 2)) (quote (1 2 3 4))) ;; => (quote (1 3)) -(let ((mod 2)) (!remove (lambda (num) (= 0 (% num mod))) (quote (1 2 3 4)))) ;; => (quote (1 3)) -(let ((mod 2)) (!!remove (= 0 (% it mod)) (quote (1 2 3 4)))) ;; => (quote (1 3)) ``` ## !concat `(&rest lists)` @@ -67,7 +61,6 @@ Returns a new list with the concatenation of the elements in the supplied `lists`. ```cl -(!concat) ;; => nil (!concat (quote (1))) ;; => (quote (1)) (!concat (quote (1)) (quote (2))) ;; => (quote (1 2)) (!concat (quote (1)) (quote (2 3)) (quote (4))) ;; => (quote (1 2 3 4)) @@ -140,7 +133,5 @@ or with `!compare-fn` if that's non-nil. ```cl (!contains? (quote (1 2 3)) 1) ;; => t (!contains? (quote (1 2 3)) 2) ;; => t -(!contains? (quote nil) (quote nil)) ;; => nil -(!contains? (quote nil) 1) ;; => nil -(!contains? (quote (1 2 4)) 3) ;; => nil +(!contains? (quote (1 2 3)) 4) ;; => nil ``` diff --git a/examples-to-docs.el b/examples-to-docs.el index 79d59ec..0ff201a 100644 --- a/examples-to-docs.el +++ b/examples-to-docs.el @@ -32,9 +32,23 @@ (let ((command-name (car function)) (signature (cadr function)) (docstring (quote-docstring (cadr (cdr function)))) - (examples (mapconcat 'identity (cadr (cddr function)) "\n"))) - (format "## %s `%s`\n\n%s\n\n```cl\n%s\n```\n" command-name signature docstring examples))) + (examples (cadr (cddr function)))) + (format "## %s `%s`\n\n%s\n\n```cl\n%s\n```\n" + command-name + signature + docstring + (mapconcat 'identity (three-first examples) "\n")))) (defun create-docs-file () (with-temp-file "./docs.md" (insert (mapconcat 'function-to-md (nreverse functions) "\n")))) + +(defun three-first (list) + (let (first) + (when (car list) + (setq first (cons (car list) first)) + (when (cadr list) + (setq first (cons (cadr list) first)) + (when (car (cddr list)) + (setq first (cons (car (cddr list)) first))))) + (nreverse first))) diff --git a/examples.el b/examples.el index dad913e..addad95 100644 --- a/examples.el +++ b/examples.el @@ -1,5 +1,8 @@ ;; -*- lexical-binding: t -*- +;; Only the first three examples per function are shown in the docs, +;; so make those good. + (require 'bang) (defun even? (num) (= 0 (% num 2))) @@ -11,17 +14,18 @@ (!!map (* it it) '(1 2 3 4)) => '(1 4 9 16)) (defexamples !reduce-from - (!reduce-from '+ 7 '()) => 7 - (!reduce-from '+ 7 '(1)) => 8 (!reduce-from '+ 7 '(1 2)) => 10 - (!!reduce-from (+ acc it) 7 '(1 2 3)) => 13) + (!reduce-from (lambda (memo item) (+ memo item)) 7 '(1 2)) => 10 + (!!reduce-from (+ acc it) 7 '(1 2 3)) => 13 + (!reduce-from '+ 7 '()) => 7 + (!reduce-from '+ 7 '(1)) => 8) (defexamples !reduce - (!reduce '+ '()) => 0 - (!reduce '+ '(1)) => 1 (!reduce '+ '(1 2)) => 3 (!reduce (lambda (memo item) (format "%s-%s" memo item)) '(1 2 3)) => "1-2-3" (!!reduce (format "%s-%s" acc it) '(1 2 3)) => "1-2-3" + (!reduce '+ '()) => 0 + (!reduce '+ '(1)) => 1 (!!reduce (format "%s-%s" acc it) '()) => "nil-nil") (defexamples !filter @@ -37,10 +41,10 @@ (let ((mod 2)) (!!remove (= 0 (% it mod)) '(1 2 3 4))) => '(1 3)) (defexamples !concat - (!concat) => nil (!concat '(1)) => '(1) (!concat '(1) '(2)) => '(1 2) - (!concat '(1) '(2 3) '(4)) => '(1 2 3 4)) + (!concat '(1) '(2 3) '(4)) => '(1 2 3 4) + (!concat) => nil) (defexamples !mapcat (!mapcat 'list '(1 2 3)) => '(1 2 3) @@ -68,6 +72,6 @@ (defexamples !contains? (!contains? '(1 2 3) 1) => t (!contains? '(1 2 3) 2) => t - (!contains? '() '()) => nil + (!contains? '(1 2 3) 4) => nil (!contains? '() 1) => nil - (!contains? '(1 2 4) 3) => nil) + (!contains? '() '()) => nil)