From: Daniel Shahaf <d.s@daniel.shahaf.name>
To: Zsh Users <zsh-users@zsh.org>
Subject: min() max() math functions (was: Re: Feature request (@M):# with context matches)
Date: Sun, 07 Feb 2016 00:22:12 +0000 [thread overview]
Message-ID: <20160207002212.GC24068@tarsus.local2> (raw)
In-Reply-To: <160130085456.ZM9730__49922.0612728552$1454172936$gmane$org@torch.brasslantern.com>
Bart Schaefer wrote on Sat, Jan 30, 2016 at 08:54:56 -0800:
> So you want something like
>
> while (( ( hit = $list[(i)*$search_pattern*] ) < $#list ))
> do
> print -r -- $list[hit-3,hit+3]
> shift $hit list
> done
>
> with some appropriate checking that (hit-3) doesn't become negative and
> wrap around to the other end of the array, [...]
I've wished a number of times for a built-in max() math function, which
would allow:
$list[max(hit-3, 0), hit+3]
There are a number of places in the distribution that could use this:
% git grep '[(][(].*[?].*[:]'
Completion/Unix/Command/_git: (( n = $#file > $#prefix ? $#file : $#prefix ))
Completion/Zsh/Type/_ps1234: (( cols = cols > 255 ? 255 : cols ))
Functions/Misc/zargs: shift $((end > ARGC ? ARGC : end))
Functions/Zle/modify-current-argument: (( CURSOR = wordoff + (poschar > repmax ? repmax : poschar) - 1 ))
I also needed this a number of times in my zshrc, too, such as for
.
MANWIDTH=$(( min(COLUMNS, 80) ))
I imagine that function would be variadic and take parameters of any
numeric type... Something along these lines:
_mathfunc_min _mathfunc_max() {
local result=$1 ; shift
while (( $# )) ; do
case $0 in
(max) (( $1 > result )) && result=$1;;
(min) (( $1 < result )) && result=$1;;
esac
shift
done
(( result )) # return
}
functions -M max 1 -1 _mathfunc_max # at least one argument
functions -M min 1 -1 _mathfunc_min # at least one argument
And while at it:
_mathfunc_sum() {
local sum
while (( $# )) ; do
(( sum += $1 ))
shift
done
(( sum ))
}
functions -M sum 0 -1 _mathfunc_sum
Cheers,
Daniel
next prev parent reply other threads:[~2016-02-07 0:28 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-01-29 10:15 Feature request (@M):# with context matches Sebastian Gniazdowski
2016-01-30 12:13 ` Sebastian Gniazdowski
2016-01-30 14:38 ` Christian Heinrich
2016-01-30 15:34 ` Sebastian Gniazdowski
2016-01-30 16:54 ` Bart Schaefer
2016-01-30 20:42 ` Bart Schaefer
[not found] ` <160130085456.ZM9730__49922.0612728552$1454172936$gmane$org@torch.brasslantern.com>
2016-02-07 0:22 ` Daniel Shahaf [this message]
2016-02-07 1:00 ` min() max() math functions (was: Re: Feature request (@M):# with context matches) Bart Schaefer
[not found] ` <160206170040.ZM1927__13399.3204137825$1454806909$gmane$org@torch.brasslantern.com>
2016-02-09 3:23 ` Daniel Shahaf
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=20160207002212.GC24068@tarsus.local2 \
--to=d.s@daniel.shahaf.name \
--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).