[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#30237: Generalizing ‘and=>’
bug#30237: Generalizing ‘and=>’
Wed, 31 Jan 2018 15:23:43 +0100
Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux)
Mark H Weaver <address@hidden> writes:
> This generalization will inevitably slow it down, and it's not clear
> that this is useful in practice. It's also not particularly natural,
> because these return value(s) somehow need to be interpreted as a
> boolean. There are multiple ways to do that, and it's not clear which
> is the best way.
> I think we should resist the temptation to make simple things more
> complicated without good reason. Making things more complicated always
> has a cost. I'm a big believer in keeping things simple, especially the
> widely used primitive operations.
Generalizing the arity of things tends to make them more “simple” for
example thinking of ‘+’ as an arbitrary arity function instead of a
binary operator is simpler.
I am realizing that In this case, the semantics are indeed unclear for
multiple values. For example if multiple values were to be handled then
‘(and=> (values 1 2) list)’ should work in the first place. Moreover as
you pointed what should be done regarding the truthiness of multiple
values is not clear either. As a consequence I withdraw my proposition
to generalize ‘and=>’ to multiple values.
Having said that I think the implementation you previously proposed
(without the arity 0 case) keep things simple:
((val proc) (and val (proc val)))
((val . procs)
(let loop ((val val) (procs procs))
(if (null? procs)
(and val (loop ((car procs) val)
> Finally, there still some question in my mind whether this
> generalization would be useful in practice. Have you found a
> real-world use case where this generalized 'and=>' makes life easier?
I took some time to think about a pseudo-realistic use case. Consider
some configuration variables that are set from the process environment.
We want to check and transform what the user provides with some slight
variations for each variable:
;;; Higher-order utilities.
(define (ensure pred)
(and (pred val) val)))
(define (check pred msg)
(unless (pred val)
;;; Process and check variables (unrealistic but give an idea).
(define (split-path str)
(string-split str #\:))
(define (no-empty-strings? lst)
(not (any (lambda (str) (string=? "" str)) lst)))
(define (keep-existing-files lst)
(filter file-exists? lst))
(define (warn-deprecated-path lst)
(when (any (lambda (string-suffix? "/old-bar")))
(display "Please don't refer to \"old-bar\" for XYZ reason")))
;;; Global variables.
;; Apply procedures in a pipeline fashion...
(and=> (and=> (and=> (and=> (getenv "FOO") split-path)
;; ...or with more idiomatic procedure calls.
(let ((lst (split-path (or (getenv "BAR")
(when (no-empty-strings? lst)
Handling arbitrary arities would allow rewriting those variables in a
more elegant way:
(and=> (getenv "FOO")
(define (no-deprecated-dirs? lst)
(not (any (lambda (str) (string-suffix? "/old-bar" str)) lst)))
(and=> (or (getenv "BAR") "/etc/bar:/usr/share/bar")
"Please don't refer to \"old-bar\" for XYZ reason")))
‘and-let*’ would be a reasonable alternative but the benefit of this
form is that it favours the use of higher-order procedures in place of
GPG: F2A3 8D7E EB2B 6640 5761 070D 0ADE E100 9460 4D37