Units

Overview

This Clojure library allows you to define unit conversions without repeating yourself and includes some common ones predefined. It does some cool things thanks to a few features in Clojure.

Derive Inverse Relationships

One is that if you define the equation for fahrenheit to celsius, it will derive celsius to fahrenheit for you. This uses symbolic algebra and it is accomplished easily thanks to lisp's prefix syntax and the fact that code is a normal data structure.

See the function invert, which can take (/ (- x 1) 2) and return (+ (* x 2) 1). That's like solving y=(x-1)/2 for x.

Derive New Relationships

It also figures out which conversions haven't been defined but can be derived from the conversions that have been defined. So if you define inches-to-feet and feet-to-meters it will derive meters-to-inches and inches-to-meters.

Square and Cubed Units

Also, if you tell it that a unit can be squared or cubed it will define those automatically.

Example

So for inches, feet, yards, meters, centimeters, millimeters, miles, and kilometers, if you specify 7 equations this library will define 168 conversion functions for you (56 each for length, area, and volume).

Code

Date: 2010-01-14