Skip to content

Instantly share code, notes, and snippets.

@ShingoFukuyama
Created February 7, 2014 05:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShingoFukuyama/8857574 to your computer and use it in GitHub Desktop.
Save ShingoFukuyama/8857574 to your computer and use it in GitHub Desktop.
Samples of ht.el Emacs Lisp hash table library.
;; Wilfred / ht.el
;; https://github.com/Wilfred/ht.el
(setq $h1 (ht (1 8888)
(2 "ABCDE")
(3 "Emacs Lisp")
(4 "Emacs")))
;; => #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data
;; (1 8888 2 "ABCDE" 3 "Emacs Lisp" 4 "Emacs"))
;; Accessing the hash table ----------------------------------------------------
(ht-get $h1 1) ; => 8888
(ht-keys $h1) ; => (4 3 2 1)
(ht-values $h1)
;; => ("Emacs" "Emacs Lisp" "ABCDE" 8888)
(ht-size $h1) ; => 4
(ht-items $h1)
;; => ((4 "Emacs")
;; (3 "Emacs Lisp")
;; (2 "ABCDE")
;; (1 8888))
(ht-find (lambda ($key _val)
(or (eq $key 2)
(eq $key 1)))
$h1) ; => (1 8888)
(ht-find (lambda (ignored $val)
(if (stringp $val)
(string-match "\\`Emacs" $val)))
$h1) ; => (3 "Emacs Lisp")
;; Return a hash table ---------------------------------------------------------
(setq $h4 (ht-create 'eq))
;; => #s(hash-table size 65 test eq rehash-size 1.5 rehash-threshold 0.8 data
;; ())
(setq $h5 (ht-copy $h1))
;; => #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data
;; (1 8888 2 "ABCDE" 3 "Emacs Lisp" 4 "Emacs"))
(ht-select (lambda ($key _val)
(or (eq $key 3)
(eq $key 4)))
$h1)
;; => #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data
;; (3 "Emacs Lisp" 4 "Emacs"))
(ht-reject (lambda ($key _val)
(or (eq $key 3)
(eq $key 4)))
$h1)
;; => #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data
;; (1 8888 2 "ABCDE"))
(ht-set $h4 0 "hash4")
(ht-set $h5 2 "hash5")
(ht-merge $h1 $h4 $h5)
;; => #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data
;; (1 8888 2 "hash5" 3 "Emacs Lisp" 4 "Emacs" 0 "hash4"))
;; Mutating the hash table -----------------------------------------------------
(ht-set! $h1 3 "sashisuseso") ; => nil
$h1
;; => #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data
;; (1 8888 2 "ABCDE" 3 "sashisuseso" 4 "Emacs"))
(setq $h2 (ht (2 99) (3 22)))
;; => #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data
;; (2 99 3 22))
(ht-update! $h1 $h2) ; => nil
$h1
;; => #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data
;; (1 8888 2 99 3 22 4 "Emacs"))
(ht-remove! $h1 2) ; => nil
$h1
;; => #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data
;; (1 8888 3 22 4 "Emacs"))
(ht-reject! (lambda ($key $val)
(and (numberp $key)
(stringp $val)))
$h1) ; => nil
$h1
;; => #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data
;; (1 8888 3 22))
(ht-clear! $h2) ; => nil
$h2
;; => #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data
;; ())
;;Iterating over the hash table -----------------------------------------------
$h1
;; => #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data
;; (1 8888 3 22))
(ht-map (lambda ($key $val)
(* $key $val))
$h1) ; => (66 8888)
(setq $results nil)
(ht-each (lambda (ignored $val)
(push $val $results))
$h1) ; => nil
$results ; => (22 8888)
;; Predicates ------------------------------------------------------------------
$h1
;; => #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data
;; (1 8888 3 22))
$h2
;; => #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data
;; ())
(ht? $h1) ; => t
(ht? $results) ; => nil
(ht-contains? $h1 1) ; => t
(ht-contains? $h1 2) ; => nil
(ht-empty? $h1) ; => nil
(ht-empty? $h2) ; => t
;; Converting from a hash table ------------------------------------------------
(ht->alist $h1) ; => ((3 . 22) (1 . 8888))
(ht->plist $h1) ; => (3 22 1 8888)
;; Converting to a hash table --------------------------------------------------
(ht<-alist '(("a" . "AAA") ("z" . "ZZZ")))
;; => #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data
;; ("z" "ZZZ" "a" "AAA"))
(ht<-plist '("a" "b" "x" "y"))
;; => #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data
;; ("a" "b" "x" "y"))
;; Iterating over the hash table (anaphoric) -----------------------------------
(ht-amap (cons key value) $h1) ; => ((3 . 22) (1 . 8888))
(ht-amap (list key value) $h1) ; => ((3 22) (1 8888))
(ht-amap (+ key value) $h1) ; => (25 8889)
(ht-amap (* value value) $h1) ; => (484 78996544)
(setq $results2 nil)
(ht-aeach (push (cons key value) $results2) $h1) ; => nil
$results2 ; => ((3 . 22) (1 . 8888))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment