aboutsummaryrefslogtreecommitdiffhomepage
path: root/std/Generic/Number.bruijn
blob: a5fab12776516e5e458f2ca766e0d23a5e4f6af3 (plain) (blame)
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
# MIT License, Copyright (c) 2024 Marvin Borner
# generic number template, meant to be :input-ted
# assumes: zero?, even?, gt?, zero?, eq?, add, compare-case

# TODO: also include std/Number more std/Math etc.? Requires solution for recursive imports
# then: give numeral systems unary id, match with index-unary

:import std/Combinator .
:import std/Logic .

# returns true if number is not zero
not-zero? not! ∘ zero? ⧗ Generic → Boolean

≠?‣ not-zero?

# returns true if two numbers are not equal
not-eq? not! ∘∘ eq? ⧗ Generic → Generic → Boolean

…≠?… not-eq?

# prefix for comparing functions
?‣ &eq?

# returns true if number is less than other number
lt? \gt? ⧗ Generic → Generic → Boolean

…<?… lt?

# returns true if number is less than or equal to other number
le? not! ∘∘ gt? ⧗ Generic → Generic → Boolean

…≤?… le?

# returns true if number is greater than or equal to other number
ge? \le? ⧗ Generic → Generic → Boolean

…≥?… ge?

# returns 1 if a>b, -1 if a<b and 0 if a=b
# also: spaceship operator
compare compare-case (+0) (+1) (-1) ⧗ Generic → Generic → Generic

…<=>… compare

<=>‣ &compare

# returns true if comparison result is equal (EQ)
c-eq? eq? (+0) ⧗ Generic → Generic

# returns true if comparison result is less than (LT)
c-lt? eq? (-1) ⧗ Generic → Generic

# returns true if comparison result is greater than (GT)
c-gt? eq? (+1) ⧗ Generic → Generic

# returns max number of two
max [[(1 ≤? 0) 0 1]] ⧗ Generic → Generic → Generic

# returns min number of two
min [[(1 ≤? 0) 1 0]] ⧗ Generic → Generic → Generic

# clamps a number between two numbers
clamp [[[min 1 (max 0 2)]]] ⧗ Generic → Generic → Generic

# returns true if the number is odd (remainder mod 2 == 1)
odd? ¬‣ ∘ even? ⧗ Generic → Boolean