dash.el icon indicating copy to clipboard operation
dash.el copied to clipboard

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

Open emacksnotes opened this issue 10 months ago • 1 comments

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 avatar Aug 15 '23 12:08 emacksnotes