]> git.earman.xyz Git - lisp/workenv.git/commitdiff
Add some conversions and a caller
authorrearman <rearman@r717.net>
Wed, 15 Oct 2025 19:55:07 +0000 (15:55 -0400)
committerrearman <rearman@r717.net>
Wed, 15 Oct 2025 19:55:07 +0000 (15:55 -0400)
Caller takes an amount as a number, and a from-unit and to-unit as quoted
symbols.  It converts the units to strings, sticks a "->" between them,
interns that string as a symbol, and calls that symbol with the amount as an
argument.

unit-conv.lisp

index e145a8282e73a69882ba463fe0165f09f0db303f..826581d2c243ae9d7c3bc04bcfd9c8e81df5d4d0 100644 (file)
@@ -4,13 +4,68 @@
 
 ;;; Length
 
-(defun mm->in (mm)
-  "Convert milimeters to inches."
-  (/ mm 25.4))
+(defun mm->cm (mm)
+  "Convert milimeters to centimeters."
+  (/ mm 10.0))
+
+(defun cm->mm (cm)
+  "Convert centimeters to milimeters."
+  (* cm 10.0))
+
+(defun cm->m (cm)
+  "Convert centimeters to meters."
+  (/ cm 100.0))
+
+(defun m->cm (m)
+  "Convert meters to centimeters."
+  (* m 100.0))
+
+(defun mm->in (mm) "Convert milimeters to inches." (/ mm 25.4))
 
 (defun in->mm (in)
   "Convert inches to milimeters."
   (* in 25.4))
+
+(defun in->ft (in)
+  "Convert inches to feet."
+  (/ in 12.0))
+
+(defun ft->in (ft)
+  "Convert feet to inches."
+  (* ft 12.0))
+
+(defun mm->ft (mm)
+  "Convert milimeters to feet."
+  (in->ft (mm->in mm)))
+
+(defun ft->mm (ft)
+  "Convert feet to milimeters."
+  (in->mm (ft->in ft)))
+
+(defun ft->yd (ft)
+  "Convert feet to yards."
+  (/ ft 3.0))
+
+(defun yd->ft (yd)
+  "Convert yards to feet."
+  (* yd 3.0))
+
+(defun in->yd (in)
+  "Convert inches to yards."
+  (/ in 36.0))
+
+(defun yd->in (yd)
+  "Convert yards to inches."
+  (* yd 36.0))
+
+(defun mm->yd (mm)
+  "Convert milimeters to yards."
+  (in->yd (mm->in mm)))
+
+(defun yd->mm (yd)
+  "Convert yards to milimeters."
+  (in->mm (yd->in yd)))
+
 \f
 ;;; Heat
 
 (defun lb->kg (pounds)
   "Convert pounds to kilograms."
   (/ pounds 2.2))
+\f
+;;; Volume
+
+(defun floz->c (floz)
+  "Convert fluid ounces to cups."
+  (/ floz 8.0))
+
+(defun c->floz (c)
+  "Convert fluid ounces to cups."
+  (* c 8.0))
+
+(defun convert (amt funits tunits)
+  "Convert an `amt' of `funits' to `tunits'.
+Assumes the function to convert is already defined, and called as
+'(funits->tunits amt)'."
+  (if (eq funits tunits)
+      amt
+      (funcall (intern (concatenate 'string
+                                   (symbol-name funits)
+                                   "->"
+                                   (symbol-name tunits)))
+              amt)))