all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* `when' vs. `and' / `unless' vs `or'
@ 2018-10-16 21:05 Garreau, Alexandre
  2018-10-16 22:13 ` Drew Adams
  0 siblings, 1 reply; 11+ messages in thread
From: Garreau, Alexandre @ 2018-10-16 21:05 UTC (permalink / raw)
  To: help-gnu-emacs

Hi, TL;DR: I’m asking for opinions and individual practice,

I’ve heard `when' and `unless' are for side-effects, because if cond
fails they return nil (or maybe they don’t return in some lisps or
previous emacsen?).

However, there are situations where I could use respectively `and' or
`or' instead, where these works and are not used for side-effects (there
may be none) but only for return value.  Especially anywhere a boolean
is expected, as then nil has full meaning (the condition, or maybe the
body, failed).

Such as: (or stuff (when cond body) stuff) vs. (or stuff (and cond body)
stuff), or (and stuff (unless cond body) stuff) vs. (and stuff (unless
cond body) stuff), but also as a condition for an `if', or even another
`when' or `unless'. Then what should I do?

Should I ban these and only use `when' and `unless' for side-effects,
except when I’m feeling like I should use a progn inside the `and'/`or'
(so I don’t bloat code)… or maybe should I use that progn, to convey
some meaning (this one at least truly is for side-effects)?

Or should I use `when' and `unless' as much as possible, and use `or'
and `and' only when the first argument could be a meaningful non-nil
thing to return?

Maybe there are even other practices.  I’m interested in your opinions
and your practices, as both manual and docstrings are not verbose enough
about this issue.

PS:

Personally, I was always seduced by how primitive and basic or
short-circuiting logical constructs, and never understood why these are
implemented in terms of conditionals, rather than the opposite (probably
because in imperative programming short-circuiting is quite relevant and
not trivial and the important thing of the if?), since these are backed
down in primitive logic.

So I tend to like to use them when I can, but I fear this is `(or
cryptical pedantic "a too-smart approach")', as first time I saw them
used like that I bugged a bit (but I can imput that to how and where I
was wrongly teached to see these).

PPS: Sorry for verbosity, one day I need to find a technique to fix that
PPPS: I fear I already asked something among those line a long time ago
but lost both the backup and memory of what was said.



^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: `when' vs. `and' / `unless' vs `or'
  2018-10-16 21:05 `when' vs. `and' / `unless' vs `or' Garreau, Alexandre
@ 2018-10-16 22:13 ` Drew Adams
  2018-10-16 22:38   ` Garreau, Alexandre
  0 siblings, 1 reply; 11+ messages in thread
From: Drew Adams @ 2018-10-16 22:13 UTC (permalink / raw)
  To: Garreau, Alexandre, help-gnu-emacs

Do whatever you like. ;-) Different people use different styles.
There is no standard or even a convention. Common Lisp
suggests some conventions, but there too there is nothing
official.

I use `when' and `unless' when I want to show a human
reader (mainly me) that the code doesn't use or care about
the return value. I don't use them when the returned `nil'
could be important.

I use `and' or `or' when the return value is significant.
(With `not' as needed.)

I use `if' when the true part is a single sexp. If necessary,
to respect this I flip the true and false parts (negating the
condition).

I use `cond' when I have two or more sexps for each of
the true and false parts or I have more than two conditions.

I use `case' when the conditions would just test the
same symbol for equality against different values.

I don't use `pcase' much. Nothing against it, really.

Yes, the way I use `if' etc. can mean that I change which
form I use as the code evolves. I'd rather spend more
time fiddling with the code, in order to have the result
be easier to understand (for a human).

YMMV.



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: `when' vs. `and' / `unless' vs `or'
  2018-10-16 22:13 ` Drew Adams
@ 2018-10-16 22:38   ` Garreau, Alexandre
  2018-10-16 23:21     ` Drew Adams
  0 siblings, 1 reply; 11+ messages in thread
From: Garreau, Alexandre @ 2018-10-16 22:38 UTC (permalink / raw)
  To: Drew Adams; +Cc: help-gnu-emacs

Le 16/10/2018 à 15h13, Drew Adams a écrit :
> Do whatever you like. ;-) Different people use different styles.
> There is no standard or even a convention. Common Lisp
> suggests some conventions, but there too there is nothing
> official.

What is this? it does interest me.

> I use `when' and `unless' when I want to show a human
> reader (mainly me) that the code doesn't use or care about
> the return value. I don't use them when the returned `nil'
> could be important.

Okay, so what about when you care about the result, but absolutely not
about the return value of the condition?

(unless cond body) vs (or cond body) -> `or' might imply you might want
to return cond.

> I use `and' or `or' when the return value is significant.
> (With `not' as needed.)
>
> I use `if' when the true part is a single sexp. If necessary,
> to respect this I flip the true and false parts (negating the
> condition).

So unless it is simple, you always write your big-conditions if as an
imbrication of both `or' and `and'? isn’t this actually less easy to
understand (even if easier to read with indentation)?

> I use `case' when the conditions would just test the
> same symbol for equality against different values.

And then comes typecase and its unability to have several types per
clause, or when you want to use an arbitrary test function for case, the
more likely and particular case of when you want that test function to
be `equal', the case where in your bodies you always apply something to
the aforementioned expr (so it looks redundant)…

> I use `cond' when I have two or more sexps for each of
> the true and false parts or I have more than two conditions.

…or the case each condition clause is actually to apply something to the
same thing.

I’ve refactored all this with my own typecase, case*, case-equal,
case-apply, case-test… Where case-apply is based on case-equal which is
based on case*, and typecase on case-test.  I still need to refactor all
this into a big general-case function upon everything is based by being
a particular case of it.

> I don't use `pcase' much. Nothing against it, really.

YMMV indeed :)



^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: `when' vs. `and' / `unless' vs `or'
  2018-10-16 22:38   ` Garreau, Alexandre
@ 2018-10-16 23:21     ` Drew Adams
  2018-10-17  0:02       ` Garreau, Alexandre
  0 siblings, 1 reply; 11+ messages in thread
From: Drew Adams @ 2018-10-16 23:21 UTC (permalink / raw)
  To: Garreau, Alexandre; +Cc: help-gnu-emacs

> Okay, so what about when you care about the result, but absolutely not
> about the return value of the condition?
> 
> (unless cond body) vs (or cond body) -> `or' might imply you might want
> to return cond.

I don't understand the question. I use (or ...) when I want to return
the value of a disjunction, where that value, if non-nil, might be
any non-nil Lisp value. If (or cond body) returns cond then it's
because cond is non-nil? What's the question? (And there is no
"cond" as in condition versus "body" as in body. `or' just returns
the first non-nil arg, or nil if none is non-nil.)

> > I use `and' or `or' when the return value is significant.
> > (With `not' as needed.)
> >
> > I use `if' when the true part is a single sexp. If necessary,
> > to respect this I flip the true and false parts (negating the
> > condition).
> 
> So unless it is simple, you always write your big-conditions if as an
> imbrication of both `or' and `and'? isn’t this actually less easy to
> understand (even if easier to read with indentation)?

Dunno what you mean. Isn't what less easy to understand than
what? What's a "big-condition"?

I use `if' when the code cares about the return value, just like
`or' and `and'. If the code doesn't care about the return value,
and if there is only one condition, then I use `when' or `unless'.



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: `when' vs. `and' / `unless' vs `or'
  2018-10-16 23:21     ` Drew Adams
@ 2018-10-17  0:02       ` Garreau, Alexandre
  2018-10-17  0:16         ` Drew Adams
  0 siblings, 1 reply; 11+ messages in thread
From: Garreau, Alexandre @ 2018-10-17  0:02 UTC (permalink / raw)
  To: Drew Adams; +Cc: help-gnu-emacs

Le 16/10/2018 à 16h21, Drew Adams a écrit :
>> Okay, so what about when you care about the result, but absolutely not
>> about the return value of the condition?
>> 
>> (unless cond body) vs (or cond body) -> `or' might imply you might want
>> to return cond.
>
> I don't understand the question. I use (or ...) when I want to return
> the value of a disjunction, where that value, if non-nil, might be
> any non-nil Lisp value. If (or cond body) returns cond then it's
> because cond is non-nil? What's the question?

I was just saying in the first case it will return nil while in the
second it will return cond (the first arg), so if you don’t want to
return cond (because it’s not what you want) you’ll end up with (and
(not cond) body).

> (And there is no "cond" as in condition versus "body" as in body. `or'
> just returns the first non-nil arg, or nil if none is non-nil.)

It was just to use the same arg names, it doesn’t change anything as you say.

> What's a "big-condition"?

A condition of more than a single sexp, such as (and cond cond) instead
of just cond (which may be (= x 1), so that it would have instead (and
(= x 1) (= x 2)) for instance).

> I use `if' when the code cares about the return value, just like
> `or' and `and'. If the code doesn't care about the return value,
> and if there is only one condition, then I use `when' or `unless'.

> Dunno what you mean. Isn't what less easy to understand than
> what?

(if (or cond cond) then else) than, canonically (and (not (and (or cond
cond) then)) else) (not sure about a simplified version), which is the
way to write it without an `if', as you said you didn’t use it if the
condition was more than a single sexp.



^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: `when' vs. `and' / `unless' vs `or'
  2018-10-17  0:02       ` Garreau, Alexandre
@ 2018-10-17  0:16         ` Drew Adams
  2018-10-17  1:25           ` Garreau, Alexandre
  0 siblings, 1 reply; 11+ messages in thread
From: Drew Adams @ 2018-10-17  0:16 UTC (permalink / raw)
  To: Garreau, Alexandre; +Cc: help-gnu-emacs

> (if (or cond cond) then else) than, canonically (and (not (and (or cond
> cond) then)) else) (not sure about a simplified version), which is the
> way to write it without an `if', as you said you didn’t use it if the
> condition was more than a single sexp.

As I (think I) said, I use (if (or cond cond) then else), if THEN is a single sexp.

If THEN is not a single step I don't bother to use
(if (or cond cond) (progn THEN1 THEN2...)) else).

---

BTW, for transforming sexps with `not', `and', and `or', for readability
or for performance reasons, I sometimes use `notandor.el'.

https://www.emacswiki.org/emacs/NotAndOr

Also, package `el-search.el' has similar possibilities (and many other
good things).



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: `when' vs. `and' / `unless' vs `or'
  2018-10-17  0:16         ` Drew Adams
@ 2018-10-17  1:25           ` Garreau, Alexandre
  2018-10-17  1:58             ` Drew Adams
  2018-10-17 15:42             ` Michael Heerdegen
  0 siblings, 2 replies; 11+ messages in thread
From: Garreau, Alexandre @ 2018-10-17  1:25 UTC (permalink / raw)
  To: Drew Adams; +Cc: help-gnu-emacs

Le 16/10/2018 à 17h16, Drew Adams a écrit :
>> (if (or cond cond) then else) than, canonically (and (not (and (or cond
>> cond) then)) else) (not sure about a simplified version), which is the
>> way to write it without an `if', as you said you didn’t use it if the
>> condition was more than a single sexp.
>
> As I (think I) said, I use (if (or cond cond) then else), if THEN is a single sexp.

Oh sorry! I misunderstood in fact, I must be tired.

> BTW, for transforming sexps with `not', `and', and `or', for readability
> or for performance reasons, I sometimes use `notandor.el'.
>
> https://www.emacswiki.org/emacs/NotAndOr

Nice! will it be in elpa one day? ^^

Althought your Package Requirements is a list instead of a list of list
so that blocks automatical installation using package-install-file…

> Also, package `el-search.el' has similar possibilities (and many other
> good things).

Wow it seems cool, I’ll try it out when I’ve got time if I remember to.
I’ve dreamt of it I think.



^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: `when' vs. `and' / `unless' vs `or'
  2018-10-17  1:25           ` Garreau, Alexandre
@ 2018-10-17  1:58             ` Drew Adams
  2018-10-17  9:00               ` Garreau, Alexandre
       [not found]               ` <mailman.2299.1539766826.1284.help-gnu-emacs@gnu.org>
  2018-10-17 15:42             ` Michael Heerdegen
  1 sibling, 2 replies; 11+ messages in thread
From: Drew Adams @ 2018-10-17  1:58 UTC (permalink / raw)
  To: Garreau, Alexandre; +Cc: help-gnu-emacs

> > BTW, for transforming sexps with `not', `and', and `or', for readability
> > or for performance reasons, I sometimes use `notandor.el'.
> >
> > https://urldefense.proofpoint.com/v2/url?u=https-
> 3A__www.emacswiki.org_emacs_NotAndOr&d=DwIFaQ&c=RoP1YumCXCgaWH
> vlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=kI3P6ljGv6CTHIKju0jqInF6AOwMCYRDQU
> mqX22rJ98&m=__OVOB-
> tJoLihmI72sPPpAIReKeWG19lS1GOjTGgDGo&s=sXzSuHMBNHG6coBRhaVI6lraH
> 1aMD3bro_kwvYDmC14&e=
> 
> Nice! will it be in elpa one day?

No, no need.

> Althought your Package Requirements is a list instead of a list of list
> so that blocks automatical installation using package-install-file…

Corrected. Thx.

> > Also, package `el-search.el' has similar possibilities (and many other
> > good things).
> 
> Wow it seems cool, I’ll try it out when I’ve got time if I remember to.
> I’ve dreamt of it I think.

Remember to. ;-)



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: `when' vs. `and' / `unless' vs `or'
  2018-10-17  1:58             ` Drew Adams
@ 2018-10-17  9:00               ` Garreau, Alexandre
       [not found]               ` <mailman.2299.1539766826.1284.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 11+ messages in thread
From: Garreau, Alexandre @ 2018-10-17  9:00 UTC (permalink / raw)
  To: Drew Adams; +Cc: help-gnu-emacs

Le 16/10/2018 à 18h58, Drew Adams a écrit :
>> > BTW, for transforming sexps with `not', `and', and `or', for readability
>> > or for performance reasons, I sometimes use `notandor.el'.
>> >
>> > https://urldefense.proofpoint.com/v2/url?u=https-
>> 3A__www.emacswiki.org_emacs_NotAndOr&d=DwIFaQ&c=RoP1YumCXCgaWH
>> vlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=kI3P6ljGv6CTHIKju0jqInF6AOwMCYRDQU
>> mqX22rJ98&m=__OVOB-
>> tJoLihmI72sPPpAIReKeWG19lS1GOjTGgDGo&s=sXzSuHMBNHG6coBRhaVI6lraH
>> 1aMD3bro_kwvYDmC14&e=
>> 
>> Nice! will it be in elpa one day?
>
> No, no need.

I was asking because as I find it useful other might :P but I understand
as it’s experimental until it’s tested to be proved that much useful,
to, for instance, being used in another package, you prefer to delay ^^



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: `when' vs. `and' / `unless' vs `or'
       [not found]               ` <mailman.2299.1539766826.1284.help-gnu-emacs@gnu.org>
@ 2018-10-17  9:49                 ` Emanuel Berg
  0 siblings, 0 replies; 11+ messages in thread
From: Emanuel Berg @ 2018-10-17  9:49 UTC (permalink / raw)
  To: help-gnu-emacs

Garreau, Alexandre wrote:

> I was asking because as I find it useful
> other might :P but I understand as it’s
> experimental until it’s tested to be proved
> that much useful, to, for instance, being
> used in another package, you prefer to delay
> ^^

I'm sure you are aware of MELPA as well as the
EmacsWiki where people can put whatever they
like. When I came to Emacs I was unaware of
both so what happened was I just put all my
Elisp on my personal web pile. Now I've lost
touch with it just a bit so I don't have the
enthusiasm to go thru it all to decide if
anything is worth putting somewhere else.

-- 
underground experts united
http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: `when' vs. `and' / `unless' vs `or'
  2018-10-17  1:25           ` Garreau, Alexandre
  2018-10-17  1:58             ` Drew Adams
@ 2018-10-17 15:42             ` Michael Heerdegen
  1 sibling, 0 replies; 11+ messages in thread
From: Michael Heerdegen @ 2018-10-17 15:42 UTC (permalink / raw)
  To: Garreau, Alexandre; +Cc: help-gnu-emacs

"Garreau, Alexandre" <galex-713@galex-713.eu> writes:

> > Also, package `el-search.el' has similar possibilities (and many other
> > good things).
>
> Wow it seems cool, I’ll try it out when I’ve got time if I remember to.
> I’ve dreamt of it I think.

If you happen to try it, I'm curious for your feedback, especially if
you find bugs, something doesn't work as expected, the documentation
isn't helpful enough, etc.


Thanks,

Michael.



^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2018-10-17 15:42 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-10-16 21:05 `when' vs. `and' / `unless' vs `or' Garreau, Alexandre
2018-10-16 22:13 ` Drew Adams
2018-10-16 22:38   ` Garreau, Alexandre
2018-10-16 23:21     ` Drew Adams
2018-10-17  0:02       ` Garreau, Alexandre
2018-10-17  0:16         ` Drew Adams
2018-10-17  1:25           ` Garreau, Alexandre
2018-10-17  1:58             ` Drew Adams
2018-10-17  9:00               ` Garreau, Alexandre
     [not found]               ` <mailman.2299.1539766826.1284.help-gnu-emacs@gnu.org>
2018-10-17  9:49                 ` Emanuel Berg
2018-10-17 15:42             ` Michael Heerdegen

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.