* Repeat argument N times?
@ 2014-07-30 3:12 Benjamin R. Haskell
2014-07-30 6:48 ` Bart Schaefer
0 siblings, 1 reply; 6+ messages in thread
From: Benjamin R. Haskell @ 2014-07-30 3:12 UTC (permalink / raw)
To: zsh-users
[-- Attachment #1: Type: text/plain, Size: 626 bytes --]
Is there a built-in way to repeat an argument N times?
Brace expansion is something I fall back on too much, so I've gotten in the
habit of using it for small values of N, e.g. with a script I use to test
uploading files to a web app:
upload-files file1{,,} file2{,}{,,,,} file3{,,,}
uploads 3 copies of file1, 10 copies of file2, and 4 copies of file3.
It'd be nice if I could write (something like):
upload-files file1(N3) file2(N10) file3(N4)
Today I found myself writing:
upload-files file1{,}{,,,,}{,}{,,,,}
And while it's nice to know I can factor 100, it'd be nicer to just write
it.
--
Best,
Ben
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Repeat argument N times?
2014-07-30 3:12 Repeat argument N times? Benjamin R. Haskell
@ 2014-07-30 6:48 ` Bart Schaefer
2014-07-30 8:56 ` Peter Stephenson
2014-07-31 15:59 ` Mikael Magnusson
0 siblings, 2 replies; 6+ messages in thread
From: Bart Schaefer @ 2014-07-30 6:48 UTC (permalink / raw)
To: zsh-users
On Jul 29, 11:12pm, Benjamin R. Haskell wrote:
}
} Is there a built-in way to repeat an argument N times?
Not really. You have to admit it's a rather unusual thing to do. It
can be done by abusing other expansions, as you've discovered.
} It'd be nice if I could write (something like):
}
} upload-files file1(N3) file2(N10) file3(N4)
You can create an array of N empty elements just by assigning to the
Nth position:
N[100]=()
And then:
upload-files file1$^N[1,3] file2$^N[1,10] file3$^N[1,4]
Works up to however much memory you're willing to waste on allocating
empty array slots.
There are other ways, e.g.
upload-files file2${^${:-{1..10}}/*/}
upload-files file2(e{'repeat 10 reply+=($REPLY)'})
though the latter requires "file2" to actually exist, which sometimes
might be a good thing.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Repeat argument N times?
2014-07-30 6:48 ` Bart Schaefer
@ 2014-07-30 8:56 ` Peter Stephenson
2014-07-30 14:10 ` Roman Neuhauser
2014-07-31 15:59 ` Mikael Magnusson
1 sibling, 1 reply; 6+ messages in thread
From: Peter Stephenson @ 2014-07-30 8:56 UTC (permalink / raw)
To: zsh-users
On Tue, 29 Jul 2014 23:48:30 -0700
Bart Schaefer <schaefer@brasslantern.com> wrote:
> There are other ways, e.g.
>
> upload-files file2(e{'repeat 10 reply+=($REPLY)'})
>
> though the latter requires "file2" to actually exist, which sometimes
> might be a good thing.
If you don't mind a limited repertoire of counts explicitly constructed,
you can make this method look close to what you want.
% define-x() { eval "x$1() { typeset -ga reply; repeat $1 reply+=(\$REPLY) }" }
% define-x 3
% print .zshrc(+x3)
.zshrc .zshrc .zshrc
pws
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Repeat argument N times?
2014-07-30 8:56 ` Peter Stephenson
@ 2014-07-30 14:10 ` Roman Neuhauser
2014-07-30 14:41 ` Roman Neuhauser
0 siblings, 1 reply; 6+ messages in thread
From: Roman Neuhauser @ 2014-07-30 14:10 UTC (permalink / raw)
To: Peter Stephenson; +Cc: zsh-users
# p.stephenson@samsung.com / 2014-07-30 09:56:57 +0100:
> On Tue, 29 Jul 2014 23:48:30 -0700
> Bart Schaefer <schaefer@brasslantern.com> wrote:
> > There are other ways, e.g.
> >
> > upload-files file2(e{'repeat 10 reply+=($REPLY)'})
> >
> > though the latter requires "file2" to actually exist, which sometimes
> > might be a good thing.
>
> If you don't mind a limited repertoire of counts explicitly constructed,
> you can make this method look close to what you want.
>
> % define-x() { eval "x$1() { typeset -ga reply; repeat $1 reply+=(\$REPLY) }" }
> % define-x 3
> % print .zshrc(+x3)
> .zshrc .zshrc .zshrc
it looks like the alternative syntax permits generalization:
function x
{
typeset -ga reply
repeat $1 do
reply+=($REPLY)
done
}
print :)(e:x 3:)
print :)(e:x 30:)
--
roman
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Repeat argument N times?
2014-07-30 14:10 ` Roman Neuhauser
@ 2014-07-30 14:41 ` Roman Neuhauser
0 siblings, 0 replies; 6+ messages in thread
From: Roman Neuhauser @ 2014-07-30 14:41 UTC (permalink / raw)
To: zsh-users
# neuhauser@sigpipe.cz / 2014-07-30 16:10:31 +0200:
> print :)(e:x 3:)
> print :)(e:x 30:)
s/:)/.zshrc/ ...
--
roman
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Repeat argument N times?
2014-07-30 6:48 ` Bart Schaefer
2014-07-30 8:56 ` Peter Stephenson
@ 2014-07-31 15:59 ` Mikael Magnusson
1 sibling, 0 replies; 6+ messages in thread
From: Mikael Magnusson @ 2014-07-31 15:59 UTC (permalink / raw)
To: Zsh Users
On 30 July 2014 08:48, Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Jul 29, 11:12pm, Benjamin R. Haskell wrote:
> }
> } Is there a built-in way to repeat an argument N times?
>
> Not really. You have to admit it's a rather unusual thing to do. It
> can be done by abusing other expansions, as you've discovered.
>
> } It'd be nice if I could write (something like):
> }
> } upload-files file1(N3) file2(N10) file3(N4)
>
> You can create an array of N empty elements just by assigning to the
> Nth position:
>
> N[100]=()
>
> And then:
>
> upload-files file1$^N[1,3] file2$^N[1,10] file3$^N[1,4]
>
> Works up to however much memory you're willing to waste on allocating
> empty array slots.
>
> There are other ways, e.g.
>
> upload-files file2${^${:-{1..10}}/*/}
>
> upload-files file2(e{'repeat 10 reply+=($REPLY)'})
>
> though the latter requires "file2" to actually exist, which sometimes
> might be a good thing.
Here's another minor variation on the first one,
upload-files file1${(s: :)^${(l:50:)}}
--
Mikael Magnusson
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2014-07-31 15:59 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-30 3:12 Repeat argument N times? Benjamin R. Haskell
2014-07-30 6:48 ` Bart Schaefer
2014-07-30 8:56 ` Peter Stephenson
2014-07-30 14:10 ` Roman Neuhauser
2014-07-30 14:41 ` Roman Neuhauser
2014-07-31 15:59 ` Mikael Magnusson
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).