1 | const __provide__ = :| identity max min abs |
|
2 |
|
3 | func identity(x) {
|
4 | ### The identity function. Returns its argument.
|
5 |
|
6 | return (x)
|
7 | }
|
8 |
|
9 | func __math_select(list, cmp) {
|
10 | ## Internal helper for `max` and `min`.
|
11 | ##
|
12 | ## NOTE: If `list` is empty, then an error is thrown.
|
13 |
|
14 | if (len(list) === 0) {
|
15 | error "Unexpected empty list" (code=3)
|
16 | }
|
17 |
|
18 | if (len(list) === 1) {
|
19 | return (list[0])
|
20 | }
|
21 |
|
22 | var match = list[0]
|
23 | for i in (1 .. len(list)) {
|
24 | setvar match = cmp(list[i], match)
|
25 | }
|
26 | return (match)
|
27 | }
|
28 |
|
29 | func max(...args) {
|
30 | ## Compute the maximum of 2 or more values.
|
31 | ##
|
32 | ## `max` takes two different signatures:
|
33 | ## - `max(a, b)` to return the maximum of `a`, `b`
|
34 | ## - `max(list)` to return the greatest item in the `list`
|
35 | ##
|
36 | ## So, for example:
|
37 | ##
|
38 | ## max(1, 2) # => 2
|
39 | ## max([1, 2, 3]) # => 3
|
40 |
|
41 | case (len(args)) {
|
42 | (1) { return (__math_select(args[0], max)) }
|
43 | (2) {
|
44 | if (args[0] > args[1]) {
|
45 | return (args[0])
|
46 | } else {
|
47 | return (args[1])
|
48 | }
|
49 | }
|
50 | (else) { error "max expects 1 or 2 args" (code=3) }
|
51 | }
|
52 | }
|
53 |
|
54 | func min(...args) {
|
55 | ## Compute the minimum of 2 or more values.
|
56 | ##
|
57 | ## `min` takes two different signatures:
|
58 | ## - `min(a, b)` to return the minimum of `a`, `b`
|
59 | ## - `min(list)` to return the least item in the `list`
|
60 | ##
|
61 | ## So, for example:
|
62 | ##
|
63 | ## min(2, 3) # => 2
|
64 | ## max([1, 2, 3]) # => 1
|
65 |
|
66 | case (len(args)) {
|
67 | (1) { return (__math_select(args[0], min)) }
|
68 | (2) {
|
69 | if (args[0] < args[1]) {
|
70 | return (args[0])
|
71 | } else {
|
72 | return (args[1])
|
73 | }
|
74 | }
|
75 | (else) { error "min expects 1 or 2 args" (code=3) }
|
76 | }
|
77 | }
|
78 |
|
79 | func abs(x) {
|
80 | ## Compute the absolute (positive) value of a number (float or int).
|
81 |
|
82 | if (x < 0) {
|
83 | return (-x)
|
84 | } else {
|
85 | return (x)
|
86 | }
|
87 | }
|