summaryrefslogtreecommitdiff
path: root/lib/std/extremum.myr
diff options
context:
space:
mode:
Diffstat (limited to 'lib/std/extremum.myr')
-rw-r--r--lib/std/extremum.myr40
1 files changed, 40 insertions, 0 deletions
diff --git a/lib/std/extremum.myr b/lib/std/extremum.myr
new file mode 100644
index 0000000..9675915
--- /dev/null
+++ b/lib/std/extremum.myr
@@ -0,0 +1,40 @@
+pkg std =
+ generic min : (a : @a::numeric, b : @a::numeric -> @a::numeric)
+ generic max : (a : @a::numeric, b : @a::numeric -> @a::numeric)
+ generic clamp : (a : @a::numeric, min : @a::numeric, max : @a::numeric -> @a::numeric)
+ generic abs : (a : @a::numeric -> @a::numeric)
+;;
+
+generic min = {a, b
+ if a < b
+ -> a
+ else
+ -> b
+ ;;
+}
+
+generic max = {a, b
+ if a > b
+ -> a
+ else
+ -> b
+ ;;
+}
+
+generic clamp = {a, min, max
+ if a < min
+ -> min
+ elif a > max
+ -> max
+ else
+ -> a
+ ;;
+}
+
+generic abs = {a : @a::numeric
+ if a < (0 castto(@a::numeric))
+ -> -a
+ else
+ -> a
+ ;;
+}