kip

kip

ex_cldr Core Team

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

Showing Posts 1 to 10

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 OP

ex_cldr Core Team

Awesome story, I’ll add that link to the README. And of course, as you knew I would:

Interactive Elixir (1.19.5) - press Ctrl+C to exit (type h() ENTER for help)
# Import the Gnu Units database (it has more units that Localize, but Localize can only
# localize what it knows. And we don't support non-linear conversions so not as many
# as Units. But its enough - Gnu Units knows about light seconds.
iex(1)> Unity.GnuUnitsImporter.import()
iex(2)> Unity.Repl.start()
Unity v0.1.0 — type "help" for commands, "quit" to exit

> let milli_light_second = 3 lightsecond / 1000
0.003 lightsecond
> milli_light_second to miles
558.847191 miles

That was fun, and gave me some more ideas for this lib.

kip

kip OP

ex_cldr Core Team

Wow, yeah, Numbat looks the real deal (but in Rust, not Elixir of course). Unity is nothing like the scope of Numbat - but I may take some inspiration from their work. Thanks for the pointer.

kip

kip OP

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.

mudasobwa

mudasobwa

Creator of Cure

BTW, you might check marcli for easy bold colored output. Just saying.

tcoopman

tcoopman

I get a 404 on the GitHub repo?

kip

kip OP

ex_cldr Core Team

…and now its public!

kip

kip OP

ex_cldr Core Team

BTW, you might check marcli for easy bold colored output. Just saying.

Ooooo, you know I’m a sucker for new fun things to try.

kip

kip OP

ex_cldr Core Team

Two cups of coffee and here is Unity version 0.6.0, Add (optional) marcli to your app and voila.

Example

Where Next? Top

Trending in Announcing Top

woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. https://github.com/woylie/doggo Features Unstyled Phoenix components....
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
anuaralfetahe
Hello Published a new library - ProcessHub! ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews