kip

kip

ex_cldr Core Team

Unity - an Elixir unit conversion calculator inspired by the Unix `units` utility

What else would you do on a weekend except build something no one asked for?

Have you ever used the Unix utility units? It’s one of those quiet, decades-old tools that’s amazing at doing unit math and conversion. You type in what you have, it tells you what you want. It looks something like this:

> 3 meters to feet
9.84252 feet

> 60 mph to km/h
96.56064 kilometers per hour

> 100 kg * 9.8 m/s^2
980 kilogram-meter-per-square-second

> 12 ft + 3 in to ft
12.25 feet

> 1 gallon to liters
3.785412 liters

> sqrt(9 m^2)
3 meters

> 1|3 cup to mL
78.862746 milliliters

It handles juxtaposition multiplication the way physicists expect (kg m / s^2 means (kg * m) / s^2), it does rational numbers with |, and it knows about over 150 unit types with all their SI-prefixed variants. You can search for units, ask what’s conformable with what, and pipe it into shell scripts.

But what you may not have guessed is that this Unity, a units clone written in Elixir.

It leverages the units-of-measure capabilities of Localize with the power of a NimbleParsec grammar and an expression interpreter. And it adds some things that GNU units never anticipated.

Like locale-aware output:

> locale de
Locale set to :de

> 1234.5 meter to kilometer
1,2345 Kilometer

> locale ja
Locale set to :ja

> 1 kilometer
1 キロメートル

Variable binding:

> let distance = 42.195 km
42.195 kilometers

> let time = 2 hours
2 hours

> distance / time
21.0975 kilometers per hour

> _ to mph
13.109 miles per hour

Measurement system conversion — just say where you want to go:

> 100 meter to US
0.062137 miles

> 100 fahrenheit to metric
37.777778 degrees Celsius

> 100 meter to preferred
0.062137 miles

> locale de
Locale set to :de

> 100 meter to preferred
0,1 Kilometer

And mixed-unit decomposition:

> 3.756 hours to h;min;s
3 hours, 45 minutes, 21.6 seconds

You never asked for this, and you’ll probably never need it. But when you do, you’ll find it at hex.pm/packages/unity.

Coda

I’m always looking for ways to test the boundaries of Localize. This fun project was intended just as a great way to exercise Localize.Unit and it did shine a light on two small bugs. And then somehow grew into something much more.

https://github.com/elixir-localize/unity

Most Liked

mudasobwa

mudasobwa

Creator of Cure

Amazing!

When anybody mentions units, I always recall brilliant The case of the 500-mile email. Reposting it here just in case anybody has missed it (or is too young to not have it missed :). Sorry for the offtopic, but I believe it’s worth it.

kip

kip

ex_cldr Core Team

I’m very unhappy with you @kevinschweikert. You sent me down a rabbit hole nearly all day. :slight_smile: But what a lot of fun it was too! (totaly said in jest as I hope you realise). Already on unity version 0.5.0.

It’s not Numbat for sure, but take a look at this - I think it’s a fair way towards the same scope, beyond the capabilities now of units. And surprisingly, I think it’s quite usable. Proper terminal handling, tab compeition, “did you mean” handling.

Here’s the Exploring Unity guide. See what you think. I’d welcome comments and suggestions.

kip

kip

ex_cldr Core Team

Ha, turns out it was a user error. When importing the Gnu Units database, gb is interpreted as a Gilbert.

Here we go with the correct units:

Unity v0.6.0 — type help for commands, quit to exit
> 32 gigabyte / 5 tbsp in bit-per-cubic-meter
3,462,555,924,668,722.5 bit-per-cubic-meter

Unity for the win!

Last Post!

mudasobwa

mudasobwa

Creator of Cure

I am truly sorry for that, but I could not resist sharing when I stumbled upon the above.

Yes, this is indeed fascinating. I once was honored to cook together with the professional chef, on his kitchen. I needed cardamom, and the guy’s got a giant spice cupboard in there. “Where exactly should I look?” I asked. He looked at me like I’m an idiot and replied, “In that drawer to the right of the pilaf pan.”

FWIW, it’s exactly how the CPU cache works.

Where Next?

Popular in Announcing Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
New
jakub-zawislak
Hi everyone, I’m coming from the Symfony (PHP) framework. I like Phoenix, but it has a one thing that was build much better in the Symfo...
New
danschultzer
In short Plug n’ play OAuth 2.0 provider library. Just set up a resource owner schema with Ecto (your user schema), install the dependen...
New
kip
ex_cldr provides localisation and internationalisation support based upon the data from the Unicode CLDR project. Unicode released CLDR ...
407 13256 120
New
sasajuric
I’d like to announce a small library called boundaries. This is an experimental project which explores the idea of enforcing boundaries ...
New
kevinlang
Hey all, We have made an Ecto3 Adapter for SQLite3, ecto_sqlite3! We have successfully on-boarded the full suite of integration tests (...
New
fuelen
Hey folks! Want to present a toolkit for writing command-line user interfaces. It provides a convenient interface for colorizing text...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44532 311
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement