From 6e8979336d879df0fa0c5883716f94371aa8d340 Mon Sep 17 00:00:00 2001 From: primo-ppcg Date: Mon, 21 Aug 2023 12:50:53 +0700 Subject: [PATCH] speed up `min`, `max` --- src/boot/boot.janet | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/boot/boot.janet b/src/boot/boot.janet index 2a27ea08..7e877e21 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -702,30 +702,38 @@ [f] (fn [x] (not (f x)))) +(defmacro- do-extreme + [order args] + ~(do + (def ds ,args) + (var k (next ds nil)) + (var ret (get ds k)) + (while (,not= nil (set k (next ds k))) + (def x (in ds k)) + (if (,order x ret) (set ret x))) + ret)) + (defn extreme ``Returns the most extreme value in `args` based on the function `order`. `order` should take two values and return true or false (a comparison). Returns nil if `args` is empty.`` - [order args] - (var [ret] args) - (each x args (if (order x ret) (set ret x))) - ret) + [order args] (do-extreme order args)) (defn max "Returns the numeric maximum of the arguments." - [& args] (extreme > args)) + [& args] (do-extreme > args)) (defn min "Returns the numeric minimum of the arguments." - [& args] (extreme < args)) + [& args] (do-extreme < args)) (defn max-of "Returns the numeric maximum of the argument sequence." - [args] (extreme > args)) + [args] (do-extreme > args)) (defn min-of "Returns the numeric minimum of the argument sequence." - [args] (extreme < args)) + [args] (do-extreme < args)) (defn first "Get the first element from an indexed data structure."