Eric Normand Newsletter 466: You need two abstractions to model

Reflections ๐Ÿค”

You need two abstractions to model

When you model a domain in your software, you create two abstractions. Let's see an example. We'll use the domain of days of the week, as we saw last time. We'll model them as the numbers 0-6.

The first abstraction is mapping the days of the week to the numbers.

Concrete domain => Abstract domain
Monday     => 0
Tuesday    => 1
Wednesday   => 2
Thursday    => 3
Friday     => 4
Saturday    => 5
Sunday     => 6

Abstraction is a mapping from one representation to another, known as the concrete and abstract domains. This one maps the days of the week (already an abstract concept, but we call that the concrete domain since it's what we're mapping from) to the numbers 0-6 (numbers are also an abstract concept, and we call it the abstract domain since it's what we're mapping to).

That's the first abstraction. The second abstraction goes from bits in our computer to numbers.

Concrete domain => Abstract domain
0000      => 0
0001      => 1
0010      => 2
0011      => 3
0100      => 4
0101      => 5
0110      => 6

There's an interpretation of bits as binary representations of numbers. Luckily, our machines come with that built-in, and our languages have arithmetic operations to take advantage of them. In essence, we get this particular mapping for free. It lets us pretend we're dealing with the abstract domain when manipulating bits. And we can live the fiction until our numbers get too big (integer overflow).

Here are our two mappings:

Days of the week => Integers 0-6
Bits             => Integers 0-6

Because our two mappings share an abstract domain, we know we can represent days of the week in bits.

We build two abstractions when we program, but few of us are aware of what we are doing---including me until very recently. But I think it's the key to teaching people to build better domain models. There are some open questions:

  1. How do we understand the abstractions our language gives us?
  2. How do we systematically construct abstractions that map to the abstract domains of our languages' abstractions?
  3. How do we prove the correctness of our models?
  4. On what criteria do we choose between two correct models?

Well, those are just some of the questions I'm exploring now.

New podcast episode๐ŸŽ™

On my podcast, I explore whether abstract stuff is at the bottom or the top of the stack.

Clojure 1.11.0 Released ๐Ÿš€

New features! Check out the post.

ClojureDart Released ๐Ÿš€

Oh, man! Christophe Grand and Baptiste Dupuch have created a new compiler target for Clojure. This one targets the Dart language, which lets Clojure compile to native binaries across a range of devices and operating systems. It also gives us access to Flutter, which is a UI toolkit from Google. Check out the GitHub repo.

Programming Media ๐Ÿฟ

I've been on a Knuth lecture kick this week. Check out Let's Not Dumb Down the History of Computer Science where Knuth explains why we should be learning more technical history of our field.

Clojure Challenge ๐Ÿค”

Last challenge

Issue 465 - Single letter swaps - Submissions

This week's challenge

Custom sort order

The sort function sorts a collection based on its "natural ordering." sort-by allows you to sort a collection based on the natural ordering of the result of some function applied to the elements. Your task is to write a function sort-with which takes a sequence of elements that define the sort order.


clojure ; define ordering ; collection to sort (sort-with [:breakfast :lunch :dinner] #{:lunch :breakfast :dinner}) ;=> (:breakfast :lunch :dinner) (sort-with [2 3 4 :jack :queen :king :ace] [4 2 4 :king 2]) ;=> [2 2 4 4 :king]

Thanks to this site for the problem idea, where it is rated Expert in PHP. The problem has been modified.

Please submit your solutions as comments on this gist.

Rock on!
Eric Normand