From 97bd23330dbd9954daced2e18ec5969e8ab81dfd Mon Sep 17 00:00:00 2001 From: Matus Goljer Date: Mon, 11 Sep 2017 15:11:07 +0200 Subject: [PATCH 1/2] Use lexical binding for -grade-up/down --- dash.el | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/dash.el b/dash.el index 5f6b8e5..c7759a1 100644 --- a/dash.el +++ b/dash.el @@ -1758,21 +1758,17 @@ Note: `it' need not be used in each form." "Grade elements of LIST using COMPARATOR relation, yielding a permutation vector such that applying this permutation to LIST sorts it in ascending order." - ;; ugly hack to "fix" lack of lexical scope - (let ((comp `(lambda (it other) (funcall ',comparator (car it) (car other))))) - (->> (--map-indexed (cons it it-index) list) - (-sort comp) - (-map 'cdr)))) + (->> (--map-indexed (cons it it-index) list) + (-sort (lambda (it other) (funcall comparator (car it) (car other)))) + (-map 'cdr))) (defun -grade-down (comparator list) "Grade elements of LIST using COMPARATOR relation, yielding a permutation vector such that applying this permutation to LIST sorts it in descending order." - ;; ugly hack to "fix" lack of lexical scope - (let ((comp `(lambda (it other) (funcall ',comparator (car other) (car it))))) - (->> (--map-indexed (cons it it-index) list) - (-sort comp) - (-map 'cdr)))) + (->> (--map-indexed (cons it it-index) list) + (-sort (lambda (it other) (funcall comparator (car other) (car it)))) + (-map 'cdr))) (defvar dash--source-counter 0 "Monotonic counter for generated symbols.") From 3034638ec13bb6087542ea11c6a5a195e7a0d39b Mon Sep 17 00:00:00 2001 From: "Basil L. Contovounesios" Date: Sat, 9 Jan 2021 23:12:32 +0000 Subject: [PATCH 2/2] Tidy up -grade-up and -grade-down a bit * dash.el (-grade-up, -grade-down): Fix docstring. Simplify. * dev/examples.el (-grade-up, -grade-down): Quote function symbols as such. * README.md: * dash.texi: Regenerate docs. Re: #235. --- README.md | 20 ++++++++++---------- dash.el | 16 ++++++++-------- dash.texi | 20 ++++++++++---------- dev/examples.el | 8 ++++---- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 669c241..778fa2c 100644 --- a/README.md +++ b/README.md @@ -1624,24 +1624,24 @@ predicate `pred`, in ascending order. #### -grade-up `(comparator list)` -Grade elements of `list` using `comparator` relation, yielding a -permutation vector such that applying this permutation to `list` -sorts it in ascending order. +Grade elements of `list` using `comparator` relation. +This yields a permutation vector such that applying this +permutation to `list` sorts it in ascending order. ```el -(-grade-up '< '(3 1 4 2 1 3 3)) ;; => '(1 4 3 0 5 6 2) -(let ((l '(3 1 4 2 1 3 3))) (-select-by-indices (-grade-up '< l) l)) ;; => '(1 1 2 3 3 3 4) +(-grade-up #'< '(3 1 4 2 1 3 3)) ;; => '(1 4 3 0 5 6 2) +(let ((l '(3 1 4 2 1 3 3))) (-select-by-indices (-grade-up #'< l) l)) ;; => '(1 1 2 3 3 3 4) ``` #### -grade-down `(comparator list)` -Grade elements of `list` using `comparator` relation, yielding a -permutation vector such that applying this permutation to `list` -sorts it in descending order. +Grade elements of `list` using `comparator` relation. +This yields a permutation vector such that applying this +permutation to `list` sorts it in descending order. ```el -(-grade-down '< '(3 1 4 2 1 3 3)) ;; => '(2 0 5 6 3 1 4) -(let ((l '(3 1 4 2 1 3 3))) (-select-by-indices (-grade-down '< l) l)) ;; => '(4 3 3 3 2 1 1) +(-grade-down #'< '(3 1 4 2 1 3 3)) ;; => '(2 0 5 6 3 1 4) +(let ((l '(3 1 4 2 1 3 3))) (-select-by-indices (-grade-down #'< l) l)) ;; => '(4 3 3 3 2 1 1) ``` diff --git a/dash.el b/dash.el index c7759a1..053eabf 100644 --- a/dash.el +++ b/dash.el @@ -1755,20 +1755,20 @@ Note: `it' need not be used in each form." it)) (defun -grade-up (comparator list) - "Grade elements of LIST using COMPARATOR relation, yielding a -permutation vector such that applying this permutation to LIST -sorts it in ascending order." + "Grade elements of LIST using COMPARATOR relation. +This yields a permutation vector such that applying this +permutation to LIST sorts it in ascending order." (->> (--map-indexed (cons it it-index) list) (-sort (lambda (it other) (funcall comparator (car it) (car other)))) - (-map 'cdr))) + (mapcar #'cdr))) (defun -grade-down (comparator list) - "Grade elements of LIST using COMPARATOR relation, yielding a -permutation vector such that applying this permutation to LIST -sorts it in descending order." + "Grade elements of LIST using COMPARATOR relation. +This yields a permutation vector such that applying this +permutation to LIST sorts it in descending order." (->> (--map-indexed (cons it it-index) list) (-sort (lambda (it other) (funcall comparator (car other) (car it)))) - (-map 'cdr))) + (mapcar #'cdr))) (defvar dash--source-counter 0 "Monotonic counter for generated symbols.") diff --git a/dash.texi b/dash.texi index a52633a..352e790 100644 --- a/dash.texi +++ b/dash.texi @@ -2405,17 +2405,17 @@ predicate @var{pred}, in ascending order. @anchor{-grade-up} @defun -grade-up (comparator list) -Grade elements of @var{list} using @var{comparator} relation, yielding a -permutation vector such that applying this permutation to @var{list} -sorts it in ascending order. +Grade elements of @var{list} using @var{comparator} relation. +This yields a permutation vector such that applying this +permutation to @var{list} sorts it in ascending order. @example @group -(-grade-up '< '(3 1 4 2 1 3 3)) +(-grade-up #'< '(3 1 4 2 1 3 3)) @result{} '(1 4 3 0 5 6 2) @end group @group -(let ((l '(3 1 4 2 1 3 3))) (-select-by-indices (-grade-up '< l) l)) +(let ((l '(3 1 4 2 1 3 3))) (-select-by-indices (-grade-up #'< l) l)) @result{} '(1 1 2 3 3 3 4) @end group @end example @@ -2423,17 +2423,17 @@ sorts it in ascending order. @anchor{-grade-down} @defun -grade-down (comparator list) -Grade elements of @var{list} using @var{comparator} relation, yielding a -permutation vector such that applying this permutation to @var{list} -sorts it in descending order. +Grade elements of @var{list} using @var{comparator} relation. +This yields a permutation vector such that applying this +permutation to @var{list} sorts it in descending order. @example @group -(-grade-down '< '(3 1 4 2 1 3 3)) +(-grade-down #'< '(3 1 4 2 1 3 3)) @result{} '(2 0 5 6 3 1 4) @end group @group -(let ((l '(3 1 4 2 1 3 3))) (-select-by-indices (-grade-down '< l) l)) +(let ((l '(3 1 4 2 1 3 3))) (-select-by-indices (-grade-down #'< l) l)) @result{} '(4 3 3 3 2 1 1) @end group @end example diff --git a/dev/examples.el b/dev/examples.el index 564d3ef..d83d282 100644 --- a/dev/examples.el +++ b/dev/examples.el @@ -776,12 +776,12 @@ value rather than consuming a list to produce a single value." (-find-indices (-partial 'string-lessp "baz") '("bar" "foo" "baz")) => '(1)) (defexamples -grade-up - (-grade-up '< '(3 1 4 2 1 3 3)) => '(1 4 3 0 5 6 2) - (let ((l '(3 1 4 2 1 3 3))) (-select-by-indices (-grade-up '< l) l)) => '(1 1 2 3 3 3 4)) + (-grade-up #'< '(3 1 4 2 1 3 3)) => '(1 4 3 0 5 6 2) + (let ((l '(3 1 4 2 1 3 3))) (-select-by-indices (-grade-up #'< l) l)) => '(1 1 2 3 3 3 4)) (defexamples -grade-down - (-grade-down '< '(3 1 4 2 1 3 3)) => '(2 0 5 6 3 1 4) - (let ((l '(3 1 4 2 1 3 3))) (-select-by-indices (-grade-down '< l) l)) => '(4 3 3 3 2 1 1))) + (-grade-down #'< '(3 1 4 2 1 3 3)) => '(2 0 5 6 3 1 4) + (let ((l '(3 1 4 2 1 3 3))) (-select-by-indices (-grade-down #'< l) l)) => '(4 3 3 3 2 1 1))) (def-example-group "Set operations" "Operations pretending lists are sets."