zsh-users
 help / color / mirror / code / Atom feed
From: Roman Neuhauser <neuhauser@sigpipe.cz>
To: Daniel Shahaf <d.s@daniel.shahaf.name>
Cc: Bart Schaefer <schaefer@brasslantern.com>, zsh-users@zsh.org
Subject: Re: globbing in conditional expressions
Date: Wed, 14 May 2014 23:09:23 +0200	[thread overview]
Message-ID: <20140514210923.GF1629@isis.sigpipe.cz> (raw)
In-Reply-To: <20140514041908.GF2471@tarsus.local2>

# d.s@daniel.shahaf.name / 2014-05-14 04:19:08 +0000:
> Bart Schaefer wrote on Tue, May 13, 2014 at 08:41:17 -0700:
> > On May 8, 10:19pm, Roman Neuhauser wrote:
> > }
> > } maybe a crazy idea...  how about something like [[ -m pattern ]] which
> > } would succeed iff pattern matched at least one path?  this could be
> > } somewhat more amenable to shortcircuiting.
> > 
> > Returning to this after a bit of a detour through [[ ... ]] expression
> > parsing:
> > 
> > You could define (via zmodload) an operator that applies filename
> > generation to its argument, but changes to the internals of globbing
> > would be needed to make a short-circuit happen.  Then those changes
> > would have to be exposed somehow so that the operator could use them.
> > 
> 
> I've taken a shot at making those changes, see attached.

wonderful, thank you!

> > As I mentioned before, in the case of the match failing this would be
> > exactly as expensive as not short-circuiting.
> 
> Right, so short-circuiting would only be useful for callers that want to
> know whether a pattern matches a large directory, but don't care about
> having a list of matching filenames.  Does anyone have such a use-case?

s/a large directory// and you have exactly my use case: i only care
about simple and succint code.  (lack of) improved efficiency was Bart's
concern.  my original use case was along these lines:

  if ! is-mount $dst; then
    if ! is-empty $dst; then
      printf "%s: garbage in %s\n" $0 $dst
      return 1
    fi
    run sudo mount --bind $src $dst
  fi

where is-empty is now forced to

  function is-empty # {{{
  {   
    local -a d
    d=(${1:?}(N/F))
    (( $#d == 0 ))
  } # }}}

whereas i expected to be able to write sth like

  if ! is-mount $dst; then
    if [[ -m $dst/*(D) ]]; then

or

  if ! is-mount $dst; then
    if [[ -m $dst(/F) ]]; then

this piece of code deals with directories typically under 10 entries
so i don't care about the performance impact of shortcircuting.

if i cared about the matched filenames, the code would be structured
differently and proably wouldn't call for a condexpr at all.

i'm trying to think of a situation where [[ -m $pat ]] && mangle $REPLY
would be useful for something other than foot-shooting... any ideas?

-- 
roman


  parent reply	other threads:[~2014-05-14 21:09 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-07 12:41 Roman Neuhauser
2014-05-07 14:44 ` Peter Stephenson
2014-05-08 10:55   ` Daniel Shahaf
2014-05-08 11:20     ` Peter Stephenson
2014-05-08 15:34       ` Bart Schaefer
2014-05-08 20:19         ` Roman Neuhauser
2014-05-13 15:41           ` Bart Schaefer
2014-05-14  4:19             ` Daniel Shahaf
2014-05-14  7:18               ` Bart Schaefer
2014-05-15  4:55                 ` Bart Schaefer
2014-05-15  9:35                   ` Daniel Shahaf
2014-05-15 14:33                     ` Bart Schaefer
2014-05-15  9:29                 ` Daniel Shahaf
2014-05-15 14:50                   ` Bart Schaefer
2014-05-26 23:52                     ` Daniel Shahaf
2014-05-30  3:59                       ` Bart Schaefer
2014-05-30  8:47                         ` Peter Stephenson
2014-05-30 15:55                           ` Bart Schaefer
2014-05-31  4:43                             ` Bart Schaefer
2014-05-31 17:34                               ` Daniel Shahaf
2014-05-31 17:59                                 ` Bart Schaefer
2014-06-02  9:40                                   ` Peter Stephenson
2014-06-02 12:57                                     ` Daniel Shahaf
2014-06-02 14:15                                       ` Peter Stephenson
2014-05-30 14:45                         ` Daniel Shahaf
2014-05-14  7:42               ` Christoph (Stucki) von Stuckrad
2014-05-14 21:09               ` Roman Neuhauser [this message]
2014-05-15  9:14                 ` Daniel Shahaf
2014-05-15 10:05                   ` Roman Neuhauser
2014-05-07 14:57 ` Mikael Magnusson
2014-05-07 15:03 ` 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=20140514210923.GF1629@isis.sigpipe.cz \
    --to=neuhauser@sigpipe.cz \
    --cc=d.s@daniel.shahaf.name \
    --cc=schaefer@brasslantern.com \
    --cc=zsh-users@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).