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

Proposal add -which -- Return indices where the list entry is non-nil #409

Open
emacksnotes opened this issue Aug 15, 2023 · 1 comment
Open

Comments

@emacksnotes
Copy link

Proposal add -which -- Return indices where the list entry is non-nil

This is not so much about how the implementation of -which should look like, but about what the name of such a function could be ...

For example, the R language call this function which. See which: Which indices are TRUE? So, I propose the same name for such a function in dash as well.

(defun -<< (x fs)
  (->> fs
       (--map (funcall it x))))
(defun -which (v)
  (->> (-<< v
            (list 'identity (-compose (-cut -iota <>) #'length)))
       (apply #'-zip-lists)
       (--keep (when (car it)
                 (cadr it)))))
(let* ((data (-iterate (lambda (_) (random 10)) (random 10) 10))
       (v (->> data
               (-map #'cl-oddp))))
  (list :data data
        :v v
        :indices (-which v)))
( :data (3 7 7 3 1 2 4 8 7 4)
  :v (t t t t t nil nil nil t nil)
  :indices
  (0 1 2 3 4 8))
( :data (1 9 6 6 5 8 6 7 9 7)
  :v (t t nil nil t nil nil t t t)
  :indices (0 1 4 7 8 9))
( :data (0 6 6 4 3 3 0 1 0 3)
  :v (nil nil nil nil t t nil t nil t)
  :indices (4 5 7 9))
@emacksnotes
Copy link
Author

On a related note, the name -select-by-indices is a bit too big. May be call it -index. Indexing is used for selecting values in R. See https://adv-r.hadley.nz/subsetting.html, and subset: Subsetting Vectors, Matrices and Data Frames.

This comment is more about what name is picked ... Since I have experience with programming in R, I thought I will draw some parallels.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant