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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
use std::fmt::Display;

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Category {
    General,
    PowerLog,
    Trigonometry,
}

impl Display for Category {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Category::General => write!(f, "General"),
            Category::PowerLog => write!(f, "Power and Logarithms"),
            Category::Trigonometry => write!(f, "Trigonometry"),
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub enum ArgCount {
    One,
    Two,
    Atleast(u32),
}

impl ArgCount {
    pub fn check(&self, count: usize) -> bool {
        match self {
            ArgCount::One => count == 1,
            ArgCount::Two => count == 2,
            ArgCount::Atleast(n) => count >= *n as usize,
        }
    }
}

impl Display for ArgCount {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            // printed in error message
            ArgCount::One => write!(f, "one argument"),
            ArgCount::Two => write!(f, "two arguments"),
            ArgCount::Atleast(n) => {
                write!(f, "at least {n} argument{}", if *n > 1 { "s" } else { "" })
            }
        }
    }
}

#[derive(Debug, Clone)]
pub enum Args {
    One(f64),
    Two(f64, f64),
    Dyn(Vec<f64>),
}

impl Args {
    pub fn first(&self) -> f64 {
        match self {
            Args::One(v) => *v,
            Args::Two(v, _) => *v,
            Args::Dyn(v) => v[0],
        }
    }

    pub fn second(&self) -> f64 {
        match self {
            Args::One(..) => unreachable!("Not enough arguments"),
            Args::Two(_, v) => *v,
            Args::Dyn(v) => v[1],
        }
    }

    pub fn all(&self) -> &[f64] {
        match self {
            Args::One(..) => unreachable!("No dynamic arguments"),
            Args::Two(..) => unreachable!("No dynamic arguments"),
            Args::Dyn(v) => v.as_slice(),
        }
    }
}

#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct Func {
    pub category: Category,
    pub name: String,
    pub help: String,
    pub arg_count: ArgCount,
    pub eval: fn(Args) -> f64,
}

pub fn all_funcs() -> Vec<Func> {
    vec![
        Func {
            category: Category::General,
            name: "floor".to_string(),
            help: "floor value to lower integer".to_string(),
            arg_count: ArgCount::One,
            eval: |args| args.first().floor(),
        },
        Func {
            category: Category::General,
            name: "round".to_string(),
            help: "round value to nearest integer".to_string(),
            arg_count: ArgCount::One,
            eval: |args| args.first().round(),
        },
        Func {
            category: Category::General,
            name: "ceil".to_string(),
            help: "ceil value to upper integer".to_string(),
            arg_count: ArgCount::One,
            eval: |args| args.first().ceil(),
        },
        Func {
            category: Category::General,
            name: "trunc".to_string(),
            help: "truncate value to its integer part (round towards zero)".to_string(),
            arg_count: ArgCount::One,
            eval: |args| args.first().trunc(),
        },
        Func {
            category: Category::General,
            name: "fract".to_string(),
            help: "truncate value to its fractional part".to_string(),
            arg_count: ArgCount::One,
            eval: |args| args.first().fract(),
        },
        Func {
            category: Category::General,
            name: "abs".to_string(),
            help: "compute absolute value".to_string(),
            arg_count: ArgCount::One,
            eval: |args| args.first().abs(),
        },
        Func {
            category: Category::General,
            name: "sign".to_string(),
            help: "value sign (1 or -1)".to_string(),
            arg_count: ArgCount::One,
            eval: |args| args.first().signum(),
        },
        Func {
            category: Category::General,
            name: "min".to_string(),
            help: "minimum of all arguments".to_string(),
            arg_count: ArgCount::Atleast(1),
            eval: |args| {
                let args = args.all();
                let mut min = args[0];
                for arg in args.iter().skip(1) {
                    min = min.min(*arg);
                }
                min
            },
        },
        Func {
            category: Category::General,
            name: "max".to_string(),
            help: "maximum of all arguments".to_string(),
            arg_count: ArgCount::Atleast(1),
            eval: |args| {
                let args = args.all();
                let mut max = args[0];
                for arg in args.iter().skip(1) {
                    max = max.max(*arg);
                }
                max
            },
        },
        Func {
            category: Category::PowerLog,
            name: "pow".to_string(),
            arg_count: ArgCount::Two,
            help: "first argument raised to the power the second argument".to_string(),
            eval: |args| args.first().powf(args.second()),
        },
        Func {
            category: Category::PowerLog,
            name: "sqrt".to_string(),
            help: "square root".to_string(),
            arg_count: ArgCount::One,
            eval: |args| args.first().sqrt(),
        },
        Func {
            category: Category::PowerLog,
            name: "cbrt".to_string(),
            help: "cubic root".to_string(),
            arg_count: ArgCount::One,
            eval: |args| args.first().cbrt(),
        },
        Func {
            category: Category::PowerLog,
            name: "exp".to_string(),
            help: "exponential function (exp(x) = pow(e, x))".to_string(),
            arg_count: ArgCount::One,
            eval: |args| args.first().exp(),
        },
        Func {
            category: Category::PowerLog,
            name: "log".to_string(),
            help: "log(b, x) is base b logarithm of x".to_string(),
            arg_count: ArgCount::Two,
            eval: |args| args.first().log(args.second()),
        },
        Func {
            category: Category::PowerLog,
            name: "ln".to_string(),
            help: "natural logarithm".to_string(),
            arg_count: ArgCount::One,
            eval: |args| args.first().ln(),
        },
        Func {
            category: Category::PowerLog,
            name: "log2".to_string(),
            help: "base 2 logarithm".to_string(),
            arg_count: ArgCount::One,
            eval: |args| args.first().log2(),
        },
        Func {
            category: Category::PowerLog,
            name: "log10".to_string(),
            help: "base 10 logarithm".to_string(),
            arg_count: ArgCount::One,
            eval: |args| args.first().log10(),
        },
        Func {
            category: Category::Trigonometry,
            name: "sin".to_string(),
            help: "sine function".to_string(),
            arg_count: ArgCount::One,
            eval: |args| args.first().sin(),
        },
        Func {
            category: Category::Trigonometry,
            name: "cos".to_string(),
            help: "cosine function".to_string(),
            arg_count: ArgCount::One,
            eval: |args| args.first().cos(),
        },
        Func {
            category: Category::Trigonometry,
            name: "tan".to_string(),
            help: "tangent function".to_string(),
            arg_count: ArgCount::One,
            eval: |args| args.first().tan(),
        },
        Func {
            category: Category::Trigonometry,
            name: "csc".to_string(),
            help: "cosecante function (inverse of sine)".to_string(),
            arg_count: ArgCount::One,
            eval: |args| 1.0 / args.first().sin(),
        },
        Func {
            category: Category::Trigonometry,
            name: "sec".to_string(),
            help: "secante function (inverse of cosine)".to_string(),
            arg_count: ArgCount::One,
            eval: |args| 1.0 / args.first().cos(),
        },
        Func {
            category: Category::Trigonometry,
            name: "cot".to_string(),
            help: "cotangent function (inverse of tangent)".to_string(),
            arg_count: ArgCount::One,
            eval: |args| 1.0 / args.first().tan(),
        },
        Func {
            category: Category::Trigonometry,
            name: "asin".to_string(),
            help: "arc sine function".to_string(),
            arg_count: ArgCount::One,
            eval: |args| args.first().asin(),
        },
        Func {
            category: Category::Trigonometry,
            name: "acos".to_string(),
            help: "arc cosine function".to_string(),
            arg_count: ArgCount::One,
            eval: |args| args.first().acos(),
        },
        Func {
            category: Category::Trigonometry,
            name: "atan".to_string(),
            help: "arc tangent function".to_string(),
            arg_count: ArgCount::One,
            eval: |args| args.first().atan(),
        },
        Func {
            category: Category::Trigonometry,
            name: "sinh".to_string(),
            help: "hyperbolic sine function".to_string(),
            arg_count: ArgCount::One,
            eval: |args| args.first().sinh(),
        },
        Func {
            category: Category::Trigonometry,
            name: "cosh".to_string(),
            help: "hyperbolic cosine function".to_string(),
            arg_count: ArgCount::One,
            eval: |args| args.first().cosh(),
        },
        Func {
            category: Category::Trigonometry,
            name: "tanh".to_string(),
            help: "hyperbolic tangent function".to_string(),
            arg_count: ArgCount::One,
            eval: |args| args.first().tanh(),
        },
        Func {
            category: Category::Trigonometry,
            name: "asinh".to_string(),
            help: "hyperbolic arc sine function".to_string(),
            arg_count: ArgCount::One,
            eval: |args| args.first().asinh(),
        },
        Func {
            category: Category::Trigonometry,
            name: "acosh".to_string(),
            help: "hyperbolic arc cosine function".to_string(),
            arg_count: ArgCount::One,
            eval: |args| args.first().acosh(),
        },
        Func {
            category: Category::Trigonometry,
            name: "atanh".to_string(),
            help: "hyperbolic arc tangent function".to_string(),
            arg_count: ArgCount::One,
            eval: |args| args.first().atanh(),
        },
        Func {
            category: Category::Trigonometry,
            name: "degs".to_string(),
            help: "convert radians to degrees".to_string(),
            arg_count: ArgCount::One,
            eval: |args| args.first().to_degrees(),
        },
        Func {
            category: Category::Trigonometry,
            name: "rads".to_string(),
            help: "convert degrees to radians".to_string(),
            arg_count: ArgCount::One,
            eval: |args| args.first().to_radians(),
        },
    ]
}