Self Wright

Tag: rutils

Hash table utilities

People have referred to the usage of hashtables in ANSI common lisp as archaic. This isn’t necessarily true, but if you do feel this way there’s no need to stick with it. Here’s an example of how “radical utils” can simplify your life.

> (setf myhash (hash-table-from-alist '((apple . 2) (berry . 5) (lemon . 1))))
#<HASH-TABLE :TEST EQL :COUNT 3 {10060EA0B3}>
> (let ((acc 0))
          (dotable (_ val myhash acc)
        (incf acc val)))
8
> (print-hash-table myhash)
#{
  APPLE 2
  BERRY 5
  LEMON 1
 } 
#<HASH-TABLE :TEST EQL :COUNT 3 {10060EA0B3}>

So, converting from an alist or a plist to a hash table is straightforward, as is iterating over it (notice the use of _ for the ignored “key” value).

(I’m slowly working my way through the (https://github.com/vseloved/rutils), so I hope it’s ok to post little snippets like these as I go along, even though this is a blog and I really should gather them up together. OTOH if I wait that long I’ll never get around to it)

Rutils ?

I was excited to try this out when I came across it … but it looks like (today!) the github repo is not in a compilable state, at least on Allegro CL.

CL-USER(44): (ql:quickload 'rutils)
To load "rutils":
  Load 1 ASDF system:
    rutils
; Loading "rutils"
[package editor-hints.named-readtables]...........
[package rutils.readtable]........................
[package rutils.core].............................
[package rutils.misc].............................
[package rutils.anaphora].........................
[package rutils.list].............................
[package rutils.string]...........................
[package rutils.hash-table].......................
[package rutils.sequence].........................
[package rutils.pair].............................
[package rutils.tree].............................
[package rutils.array]............................
[package rutils.abbr].............................
[package rutils]..................................
[package rutil].; While EVALUATING an unknown function in
                #P"/usr/local/google/home/agam/quicklisp/dists/quicklisp/software/rutils-20140425-git/core/readtable.lisp":
Error: There is no readtable named :RUTILS.READTABLE.RUTILS-READTABLE.
  [condition type: SIMPLE-ERROR]

Update:

It works fine on SBCL(!)

I think this is because Allegro CL (which, in its defense, is trying to be clever) supports a named readtable feature that is getting triggered by the text at the top of this file. Oh well, I guess I’ll have to stick with SBCL.

Anyway, coming back to rutils: read the announcement. I for one totally agree with this goal of collecting and consolidating utility code, and as a newbie, will do my best to learn as much as I can from it!

Powered by WordPress & Theme by Anders Norén