File: zmathfunc

package info (click to toggle)
zsh 5.9-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,856 kB
  • sloc: ansic: 108,138; sh: 6,976; makefile: 722; perl: 687; awk: 291; sed: 16
file content (50 lines) | stat: -rw-r--r-- 967 bytes parent folder | download | duplicates (2)
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
43
44
45
46
47
48
49
50
#autoload

zsh_math_func_min() {
  emulate -L zsh
  local result=$1
  shift
  local arg
  for arg ; do
    (( arg < result ))
    case $? in
      (0) (( result = arg ));;
      (1) ;;
      (*) return $?;;
    esac
  done
  (( result ))
  true # Careful here: `return 0` evaluates an arithmetic expression
}
functions -M min 1 -1 zsh_math_func_min # at least one argument

zsh_math_func_max() {
  emulate -L zsh
  local result=$1
  shift
  local arg
  for arg ; do
    (( arg > result ))
    case $? in
      (0) (( result = arg ));;
      (1) ;;
      (*) return $?;;
    esac
  done
  (( result ))
  true # Careful here: `return 0` evaluates an arithmetic expression
}
functions -M max 1 -1 zsh_math_func_max # at least one argument

zsh_math_func_sum() {
  emulate -L zsh
  local sum
  local arg
  for arg ; do
    (( sum += arg ))
  done
  (( sum ))
  true # Careful here: `return 0` evaluates an arithmetic expression
}
functions -M sum 0 -1 zsh_math_func_sum