Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

counsel.el (counsel-colors--name-to-hex): protect against invalid input. #1854

Merged
merged 1 commit into from Dec 12, 2018

Conversation

@mookid
Copy link
Contributor

commented Dec 11, 2018

fix #1853

@mookid mookid force-pushed the mookid:safename-to-hex branch 2 times, most recently from 4e932a9 to 12bb41a Dec 11, 2018

@mookid mookid force-pushed the mookid:safename-to-hex branch from 12bb41a to a1b30ea Dec 11, 2018

'dups (cdr cell))))
(list-colors-duplicates)))
(let* ((colors
(delete nil

This comment has been minimized.

Copy link
@basil-conto

basil-conto Dec 12, 2018

Collaborator

Better to use delq here.

This comment has been minimized.

Copy link
@mookid

mookid Dec 12, 2018

Author Contributor

yes, in fact it is probably better not to allocate two lists and use mapcan instead.

edit: this is misleading. delete does not copy.

This comment has been minimized.

Copy link
@basil-conto

basil-conto Dec 12, 2018

Collaborator

It's not always clear when it's better to use (delq nil (mapcar ...)) vs mapcan. The thing about mapcan is it was only added in Emacs 25, so you have to use the wrapper cl-mapcan, and you still need to allocate a cons cell in the loop for each element you want to keep:

(cl-mapcan (lambda (cell)
             (let* ((name (car cell))
                    (dups (cdr cell))
                    (hex (counsel-colors--name-to-hex name)))
               (and hex (list (propertize name 'hex hex 'dups dups)))))
           (list-colors-duplicates))

Benchmarking the two approaches suggests delq+mapcar is a hair faster and smaller than cl-mapcan they're pretty evenly matched. I really don't mind either way, I'm just saying there are tradeoffs, so it basically comes down to personal preference.

This comment has been minimized.

Copy link
@mookid

mookid Dec 12, 2018

Author Contributor

Thanks for running the experimentation.

Note that contrary to what I said, delete is destructive and does not make a new list (but remove does).

This comment has been minimized.

Copy link
@basil-conto

basil-conto Dec 12, 2018

Collaborator

Note that contrary to what I said, delete is destructive and does not make a new list (but remove does).

Yup. The difference between delq and delete is that the former only accepts lists and uses eq for equality, thus making it the standard function for use in (delq nil (mapcar ...)). Using delete is less common and reserved for cases where equivalence under equal is needed.

@abo-abo

This comment has been minimized.

Copy link
Owner

commented Dec 12, 2018

Thanks.

@abo-abo abo-abo merged commit a1b30ea into abo-abo:master Dec 12, 2018

1 of 2 checks passed

continuous-integration/travis-ci/push The Travis CI build is in progress
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@mookid mookid deleted the mookid:safename-to-hex branch Dec 12, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.