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 -distribute --- Distribute a set of objects among different bins #408

Open
emacksnotes opened this issue Aug 15, 2023 · 2 comments

Comments

@emacksnotes
Copy link

emacksnotes commented Aug 15, 2023

Proposal -distribute --- Distribute a set of objects among different bins; each bin is associated with a predicate function. In other words, an object belongs to a bin, if it satisfies the corresponding bin function.

See also

(defun distribute (data bin-functions)
  (let* ((f (lambda (x)
              (->> bin-functions
                   (--some (when (funcall it x)
                             it))))))
    (->> data
         (--group-by (funcall f it))
         ;; First item of the result is the input passed
         (cons data))))
(distribute (-iterate (lambda (_) (random 10)) (random 10) 20)
            (list (lambda (x)
                    (zerop (mod x 4)))
                  (lambda (x)
                    (zerop (mod x 2)))))
((8 2 7 3 6 7 3 1 6 2 1 3 0 7 5 7 8 1 2 0)
 ((closure (t) (x) (= 0 (mod x 4))) 8 0 8 0)
 ((closure (t) (x) (= 0 (mod x 2))) 2 6 6 2 2)
 (nil 7 3 7 3 1 1 3 7 5 7 1))
(distribute (-iterate (lambda (_) (random 10)) (random 10) 20)
            (list 'cl-oddp
                  'cl-evenp))
((0 0 5 0 3 7 4 3 2 7 9 0 3 5 8 8 6 8 3 2)
 (cl-evenp 0 0 0 4 2 0 8 8 6 8 2)
 (cl-oddp 5 3 7 3 7 9 3 5 3))
(distribute '(:hi "ho" "ho" :he 2.5)
            '(floatp
              keywordp
              stringp)
            )
((:hi "ho" "ho" :he 2.5)
 (keywordp :hi :he)
 (stringp "ho" "ho")
 (floatp 2.5))
@emacksnotes
Copy link
Author

emacksnotes commented Aug 15, 2023

See also collect-cond! a variant of -separate and -separate-multi

The following thing was implicit in the discussion, but was never openly stated: The resulting subsets are disjoint, and an object can belong to one and only one bin.

There could be cases where we may want a single object to belong to multiple bins.

@emacksnotes
Copy link
Author

emacksnotes commented Aug 16, 2023

See also collect-cond! a variant of -separate and -separate-multi

The following thing was implicit in the discussion, but was never openly stated: The resulting subsets are disjoint, and an object can belong to one and only one bin.

There could be cases where we may want a single object to belong to multiple bins.

If you look at the examples I cited above, you will see that the bin-function annotates a list of objects ('annotate' here is used in the sense of -annotate). Or viewed other way you are slapping tags--as in Org tags or Twitter tags--to objects. So, an obect can have multiple tags.

The word tag (in the sense of org tag) also make sense because the original request was raised by the org-superagenda and org-ql author ... So, instead of -distribute or whatever other names have been proposed choose a name that reflects what the operation here is; and the operation is "Annotate and Group by Annotation" (if you squint a bit)

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