## Grid-Generators v0.4.20151016 (and List-Types v0.4.20151029) ReleasedOctober 29th, 2015 Patrick Stein

Two new Common Lisp libraries released: `GRID-GENERATORS` and `LIST-TYPES`.

### GRID-GENERATORS and GRID-ITERATE packages

I often find myself wanting to iterate over the points in a rectangular region of a grid in an arbitrary number of dimensions. If my grid is only one-dimensional or two-dimensional then I often just write nested loops to traverse the points of interest.

(loop :for y :from 0 :to 10 :by 1/2
:do (loop :for x :from 0 :to 10 :by 1/2
:do (something x y)))

However, when I want the code to be flexible in the number of dimensions, I always end up writing application-specific code to increment arbitrarily long lists of integers with given bounds. I finally got sick of repeating this code every time I needed it and created a library.

For the particular application that I have in mind though, I wanted more than just walking the points inside some rectangular hyper-prism. I wanted to traverse all of the points at a give taxicab distance from a starting point.

The `GRID-GENERATORS` package facilitates generating points in a rectangular hypergrid, generating points based on taxicab distance, and generating points based upon the number of steps in an arbitrary lattice (given the generators of the fundamental parallelpiped of the lattice). The `GRID-ITERATE` package provides `ITERATE` clauses for those generators.

For example, one could iterate over the same points in my nested example above like this:

(loop :with generator := (make-grid-generator '(10 10) :by '(1/2 1/2))
:for (x y) := (funcall generator)
:while x
:do (something x y))

Or, with iterate:

(iterate:iterate
(iterate:for point on-grid-to '(10 10) by '(1/2 1/2))
(destructuring-bind (x y) point
(something x y)))

### LIST-TYPES package

The `LIST-TYPES` package provides a way to generate `SATISFIES` type clauses that ensure that a list contains elements all of the given type. For example, if I wanted to ensure that my list was entirely rational numbers, I could use the declaration:

(check-type my-list (list-types:list-of rational))

## Braille chords Emacs minor modeFebruary 11th, 2015 Patrick Stein

I have been reading and writing a fair amount of Braille lately. Duxbury Systems has an excellent cheat sheet which outlines the different abbreviations used in Grade 2, English Braille. This is a great help to me when I am writing, but it is organized poorly for reading.

I started making myself an inverted version of that cheat sheet, but was quickly frustrated trying to type Braille on my QWERTY keyboard. What to do? I wrote some Emacs lisp code to allow me to type Braille characters into Emacs in the same, chorded manner that one would enter them on a Braille keyboard.

You can find the code on github: braille-chords.el. The documentation is comments at the top of the source file.

## Alternatives (v0.2.20141117) ReleasedNovember 17th, 2014 Patrick Stein

I have uploaded a new version of my Alternatives library. In addition to the `ALTERNATIVES` macro, there is an `ALTERNATIVES*` macro which allows one to specify a name for the set of choices. Then, one can check the `DOCUMENTATION` to see which alternative was last macroexpanded.

(defun random-letter ()
(alt:alternatives* random-letter-algorithm
***
(:constant-a
"Always pick the letter A."
#\A)

(:uniform
"Choose any letter with equal probability"
(random:random-elt "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))))

(documentation 'random-letter-algorithm 'alt:alternatives)
=> "CONSTANT-A
Always pick the letter A."

## Alternatives (v0.1.20141115) ReleasedNovember 16th, 2014 Patrick Stein

I have now released the code that I mentioned in my previous post Code That Tells You Why which lets one keep multiple implementations around in code and switch between them manually without much trouble.

A link to the source code is here: nklein.com/software/alternatives/.

## Track-Best Library ReleasedMay 9th, 2013 Patrick Stein

In doing a problem set from the Internet a few weeks ago, I found myself writing awkward constructions with `REDUCE` and `LOOP` to try to find the best one (or two or three) things in a big bag of things based on various criteria: similarity to English, hamming distance from their neighbor, etc.

I wrote a macro that encapsulated the pattern. I’ve reworked that macro into a library for public use.

### Using

There are a variety of examples in the README and the tests directory.

Here is one example to pique your interest. Suppose you have some data about the elevations of various cities in various states and you (being a Moxy Fruvous fan) want to know What Is the Lowest Highest Point? Here’s how you might tackle that with the `TRACK-BEST` library:

(let ((data '(("Alabama"  ("Birmingham" 664)
("Mobile" 218)
("Montegomery" 221))
("Fairbanks" 531))
("Arizona"  ("Grand Canyon" 6606)
("Phoenix" 1132)
("Tuscon"  2641)))))
(with-track-best (:order-by-fn #'<)
(dolist (state-info data)
(multiple-value-bind (city altitude)
(with-track-best ()
(dolist (city-info (rest state-info))
(track (first city-info) (second city-info))))
(track (list (first state-info) city) altitude)))))

With this limited dataset, the end result would be `(VALUES '("Alaska" "Fairbanks") 531)`. The inner `WITH-TRACK-BEST` finds the highest city in each state. The outer `WITH-TRACK-BEST` finds the lowest of these.