-
Notifications
You must be signed in to change notification settings - Fork 136
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
Sequential threading/anaphoric cond-like macro idea #234
Comments
Here's another example where this would be useful: https://github.com/zk-phi/sky-color-clock/blob/master/sky-color-clock.el#L263: (defun sky-color-clock--emoji-moonphase (time)
(let* ((time-in-days (/ (float-time time) 60 60 24))
(phase (mod (- time-in-days sky-color-clock--newmoon) sky-color-clock--moonphase-cycle)))
(cond ((<= phase 1.84) "🌑")
((<= phase 5.53) "🌒")
((<= phase 9.22) "🌓")
((<= phase 12.91) "🌔")
((<= phase 16.61) "🌕")
((<= phase 20.30) "🌖")
((<= phase 23.99) "🌗")
((<= phase 27.68) "🌘")
(t "🌑")))) Which could be: (defun sky-color-clock--emoji-moonphase (time)
(let* ((time-in-days (/ (float-time time) 60 60 24))
(phase (mod (- time-in-days sky-color-clock--newmoon) sky-color-clock--moonphase-cycle)))
(-cond->> (<= phase)
(1.84 "🌑")
(5.53 "🌒")
(9.22 "🌓")
(12.91 "🌔")
(16.61 "🌕")
(20.30 "🌖")
(23.99 "🌗")
(27.68 "🌘")
(t "🌑")))) |
By the way, an anaphoric version: (defmacro --cond-> (test &rest forms)
(declare (indent defun))
`(cond ,@(cl-loop for (it result) in forms
when (eq it t)
do (setq test t)
collect (list `(let ((it ,it))
,test)
result)))) e.g. FizzBuzz: (cl-loop for n from 1 to 100
collect (--cond-> (= 0 (% n it))
(15 (list n "FizzBuzz"))
(3 (list n "Fizz"))
(5 (list n "Buzz"))
(t n)))
;; => (1 2 (3 "Fizz") 4 (5 "Buzz") (6 "Fizz") 7 8 (9 "Fizz") (10 "Buzz") 11 (12 "Fizz") 13 14 (15 "FizzBuzz") ...) |
@alphapapa That's not how Clojure's cond-> and cond->> work, and doing something else will probably be confusing for most people. In Clojure, cond->> takes something and test form pairs, like (cond->> something test1 form1 test2 form2 ...) and it puts something last in form1 if test1 is true, then that in form2 if test2 is true, etc. What you have described is more akind to Clojure's condp. Clojure's cond->> https://clojuredocs.org/clojure.core/cond-%3E%3E |
Does #349 satisfy this feature request? |
@basil-conto Not exactly; the examples I showed don't work on that PR's code. However, @oskarkv's comment may explain why. Maybe the macro I've shown here should be named differently so Of course, naming things is hard, but I'll change the title of this issue to...something else... |
Imagine the difficulty for someone reading Elisp code with all these many similar but subtly different "magic" macros. ;) |
Yes, that's probably why not many of them have been implemented in a package. ;) |
Note: It's no longer proposed that these macros be named
-cond->>
and--cond->
. See this comment.Hey, it's me again, another idea. :)
Used like:
Which expands to:
Saves a bit of repetition. Could also have a
-cond->
form that uses each cond test as the second argument in the test form rather than the last, like the difference between->>
and->
.The text was updated successfully, but these errors were encountered: