zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Sebastian Gniazdowski <sgniazdowski@gmail.com>
Cc: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: [PATCH] Support the mksh's ${|func;} substitution
Date: Wed, 11 Sep 2019 22:35:03 -0700	[thread overview]
Message-ID: <CAH+w=7Yx6S2gC4f2iM2qf-M6uSc2RuQ+1BW5OesmAUxwWgsKpg@mail.gmail.com> (raw)
In-Reply-To: <CAKc7PVBusxo0K-iJMXfiM+H2Lm1x+mkSf4ECnK9OajXVJqoLEA@mail.gmail.com>

On Wed, Sep 11, 2019 at 7:07 PM Sebastian Gniazdowski
<sgniazdowski@gmail.com> wrote:
>
> On Thu, 12 Sep 2019 at 03:03, Bart Schaefer <schaefer@brasslantern.com> wrote:
> >
> > In an (e) glob, REPLY is set to each possible file name in turn, but
> > $reply can provide multiple names to the final result.
>
> array( "a" "b c" "d" )
> func() { reply=( ${=REPLY} ); }
> array=( ${(x^func^)array} )
>
> and it would result in FOUR array elements, not three. [...]
> I think that in order to
> allow contraction of the result, the (x) flag could use only reply,
> without REPLY as opposed to the (e), unless we do that reply being
> just set invalidates REPLY and allows the empty result.

Hm, perhaps you're still confused about something.  The value of
$REPLY means nothing after the code has run -- it's strictly an input.
$reply is the only output.  So if we were to do as I'm suggesting,

array( "a" "b c" "d" )
func() {
  if (( ${#${=REPLY}} > 1 ))
  then reply=()
  else reply=( $REPLY )
  fi
}
array=( ${(x^func^)array} )

would result in two elements ("a" "d").

Oh, I've forgotten an important bit -- the return value of the code
matters as well.  The full semantics is:
1. on entry, REPLY is set to one element and reply is unset
2. If the code returns nonzero (false, failure) then the result is the
empty array
   (so the corresponding element is removed from the input set)
3. else if reply has become set, that array is used (even if empty)
   (so the corresponding element may become zero, one, or more elements)
4. else if REPLY is set, the value of REPLY is used
   (so the corresponding element changes, possibly to an empty string)
5. else the original element is unchanged

> > Whether the syntax ${(x+func)...} would call "func" once for each
> > array element (again by analogy to glob (e+func))
>
> Why only one + in the examples? (I've tried this syntax with (e), it
> doesn't seem to support it).

Oops, I've typo'd.  as a glob flag, (+func) is (e:func:), you don't
use (e+).  I don't think we can get away with using a bare leading "+"
like that in parameter flags.

Here are examples globbing in my zsh source (gmail is probably going
to line wrap some of this, sorry):

% i=0; echo *(oNe:'REPLY=$((++i))':)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
% echo *(e:'() return 1':)
zsh: no matches found: *(e:() return 1:)
% func() { if (( $#REPLY > 4 )); then REPLY=; fi }
% echo *(oNP:,:+func)
, Misc ,  ,  ,  ,  , Test , Util ,  ,  ,  ,  ,  ,  ,  ,  , Etc ,  ,  ,
 ,  ,  ,  ,  ,  ,  , NEWS ,  ,  ,  ,  ,  ,  ,  ,  , Doc ,  ,  , zsh ,
,  ,  , Src
% func() { if (( $#REPLY > 4 )); then reply=(); fi }
% echo *(+func)
Doc Etc Misc NEWS Src Test Util zsh

  reply	other threads:[~2019-09-12  5:36 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-06  0:52 Sebastian Gniazdowski
2019-09-06  0:54 ` Sebastian Gniazdowski
2019-09-06 23:16 ` Sebastian Gniazdowski
2019-09-07 12:16   ` Daniel Shahaf
2019-09-07 15:07 ` Stephane Chazelas
2019-09-07 18:09   ` Sebastian Gniazdowski
2019-09-07 20:19     ` Stephane Chazelas
2019-09-07 21:19       ` Sebastian Gniazdowski
2019-09-10  2:20         ` Sebastian Gniazdowski
2019-09-10  5:29           ` Bart Schaefer
2019-09-10 18:21             ` Sebastian Gniazdowski
2019-09-10 19:38               ` Bart Schaefer
2019-09-12  0:08                 ` Sebastian Gniazdowski
2019-09-12  1:03                   ` Bart Schaefer
2019-09-12  2:06                     ` Sebastian Gniazdowski
2019-09-12  5:35                       ` Bart Schaefer [this message]
2019-09-12  6:00                         ` Sebastian Gniazdowski
2019-09-12  6:55                           ` Bart Schaefer
2019-09-13 20:28                             ` Sebastian Gniazdowski
2019-09-13 21:33                               ` Bart Schaefer
2019-09-13 21:36                                 ` Bart Schaefer
2019-09-14  0:41                                   ` Sebastian Gniazdowski
2019-09-14  0:44                                     ` Sebastian Gniazdowski
2023-05-03 14:46 Sebastian Gniazdowski
2023-07-04  1:55 ` Bart Schaefer
2023-07-04 18:53   ` Lawrence Velázquez
2023-07-05  5:32     ` Bart Schaefer
2023-07-05  6:30       ` Bart Schaefer
2023-07-06  1:39       ` Bart Schaefer
2023-07-06  2:27         ` Mikael Magnusson
2023-07-06  4:00           ` Bart Schaefer
2023-07-18  3:14             ` Bart Schaefer

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='CAH+w=7Yx6S2gC4f2iM2qf-M6uSc2RuQ+1BW5OesmAUxwWgsKpg@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=sgniazdowski@gmail.com \
    --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).