zsh-workers
 help / color / mirror / code / Atom feed
From: Daniel Shahaf <d.s@daniel.shahaf.name>
To: Clinton Bunch <cdb_zsh@zentaur.org>
Cc: zsh-workers@zsh.org
Subject: Re: [PATCH] zsh/random module [UPDATED]
Date: Wed, 23 Nov 2022 20:33:29 +0000	[thread overview]
Message-ID: <20221123203329.GP27622@tarpaulin.shahaf.local2> (raw)
In-Reply-To: <0a07afaf-1194-6752-8133-8aa6b689724d@zentaur.org>

Clinton Bunch wrote on Wed, Nov 23, 2022 at 07:41:40 -0600:
> I don't think we're going to come to an agreement on inclusive vs exclusive,
> and I know you'd hate the idea of both interfaces, so others will have to
> weigh in.

Some considerations:

1. What existing API, within or without zsh, should the new
builtin/mathfunc be consistent with?

2. Is it the case that choice A can be implemented in shell code in
terms of choice B, but not vice versa?  [For instance, if the argument
to -U were a uint64_t and were exclusive, there'd be no way to specify
UINT64_MAX as the upper bound.]

3. Inclusive is necessarily the rule when sampling from a given set of
elements (e.g., in zsh terms, "here's an array name, gimme a random
element from it"; when the array contains, say, {January..December}).
In this case there's no need to involve integers at all.

4. Which approach generalizes better?

---

[Please consider these questions for a second before reading on; my
proposed answer(s) are below.]

---

For #1, we might want to be consistent with the subscript slice syntax.
$foo[4,5] selects $foo[4] and $foo[5] both.  Similarly {4..5} includes
both endpoints.

Also, I could be wrong here, but isn't it the case that exclusiveness
goes along with the lower bound being hard-coded as zero?  That's how it
is in modular arithmetic, in arc4random_uniform(), and in Perl, and even
in C's idiom for iterating a dynamically-allocated array («malloc(42 * …)»,
followed by «for (i = 0; i < 42; …)»).  Python has both random.randint(),
which is inclusive, and random.randrange(), which is exclusive — but the
latter mirrors Python's array slice syntax, and that syntax has
a special-case form for "All elements from the Nth through the last one,
inclusive".

I think I'm leaning towards inclusive if -L and -U can both be specified
and towards exclusive if -L is hard-coded as 0, but I'm happy to be
convinced otherwise.

HTH,

Daniel


  reply	other threads:[~2022-11-23 20:34 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-02 17:13 [PATCH] zsh/random module Clinton Bunch
2022-11-03 17:50 ` Bart Schaefer
2022-11-04  3:17 ` dana
2022-11-04  6:22   ` Clinton Bunch
2022-11-04  7:27     ` dana
2022-11-04 12:57       ` Clinton Bunch
2022-11-08  0:18         ` [PATCH] zsh/random module [UPDATED] Clinton Bunch
2022-11-18 14:30           ` Clinton Bunch
2022-11-19  6:42             ` Lawrence Velázquez
2022-11-18 16:23           ` Stephane Chazelas
2022-11-18 17:08             ` Clinton Bunch
2022-11-18 18:12               ` Stephane Chazelas
2022-11-18 18:38                 ` Clinton Bunch
2022-11-23 19:52                   ` Daniel Shahaf
2022-11-24 16:19                     ` Stephane Chazelas
2022-11-24 16:30                       ` Roman Perepelitsa
2022-11-24 22:39                         ` Clinton Bunch
2022-11-25  8:53                           ` Stephane Chazelas
2022-11-25  9:40                             ` Stephane Chazelas
2022-11-28 16:37                               ` further discussion of zsh/random (was [PATCH] zsh/random module [UPDATED]) Clinton Bunch
2022-11-21  1:07           ` [PATCH] zsh/random module [UPDATED] Matthew Martin
2022-11-21  1:59             ` Clinton Bunch
2022-11-21  2:21               ` Matthew Martin
2022-11-21  2:57                 ` Clinton Bunch
2022-11-21  3:14                   ` Lawrence Velázquez
2022-11-21  4:17                     ` Bart Schaefer
2022-11-21  5:05                       ` Clinton Bunch
2022-11-22 13:42                         ` dana
2022-11-23 19:49                         ` Daniel Shahaf
2022-11-22 17:44                       ` Oliver Kiddle
2022-11-22 19:48                         ` Clinton Bunch
2022-11-23  1:23                   ` Matthew Martin
2022-11-23  2:58                     ` Clinton Bunch
2022-11-23  4:14                       ` Matthew Martin
2022-11-23 13:41                         ` Clinton Bunch
2022-11-23 20:33                           ` Daniel Shahaf [this message]
2022-11-23 21:42                             ` dana
2022-11-23 23:54                               ` Daniel Shahaf
2022-11-24  0:17                                 ` Daniel Shahaf
2022-11-24  1:05                                 ` dana
2022-11-24 13:52                               ` Clinton Bunch
2022-11-23 19:46           ` Daniel Shahaf
2022-11-24  2:58             ` Clinton Bunch
2022-11-24 10:07               ` nimaje+zml
2022-11-24 13:19                 ` Clinton Bunch
2022-11-24 14:33             ` Clinton Bunch

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221123203329.GP27622@tarpaulin.shahaf.local2 \
    --to=d.s@daniel.shahaf.name \
    --cc=cdb_zsh@zentaur.org \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).