GitHub - Wilfred/ht.el: The missing hash table library for Emacs
source link: https://github.com/Wilfred/ht.el
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
ht.el
The missing hash table library for Emacs.
Table of Contents
Functions
Return a hash table
ht-create
(test?)
ht-merge
(&rest tables)
ht-copy
(table)
ht-select
(function table)
ht-reject
(function table)
ht-select-keys
(table keys)
Accessing the hash table
ht-get
(table key default?)
ht-get*
(table &rest keys)
ht-keys
(table)
ht-values
(table)
ht-items
(table)
ht-find
(function table)
ht-size
(table)
Mutating the hash table
ht-set!
(table key value)
ht-update!
(table table)
ht-remove!
(table key)
ht-clear!
(table)
ht-reject!
(function table)
Iterating over the hash table
ht-map
(function table)
ht-each
(function table)
Predicates
ht?
(table-or-object)
ht-contains?
(table key)
ht-equal?
(table1 table2)
ht-empty?
(table)
Converting from a hash table
ht->alist
(table)
ht->plist
(table)
Converting to a hash table
ht<-alist
(alist test?)
ht<-plist
(plist test?)
Macros
Returning a hash table
ht
(&rest pairs)
Iterating over the hash table (anaphoric)
ht-amap
(form table)
ht-aeach
(form table)
Examples
Creating a hash table and accessing it:
(require 'ht) (defun say-hello (name) (let ((greetings (ht ("Bob" "Hey bob!") ("Chris" "Hi Chris!")))) (ht-get greetings name "Hello stranger!")))
This could be alternatively written as:
(require 'ht) (defun say-hello (name) (let ((greetings (ht-create))) (ht-set! greetings "Bob" "Hey Bob!") (ht-set! greetings "Chris" "Hi Chris!") (ht-get greetings name "Hello stranger!")))
Accessing nested hash tables:
(let ((alphabets (ht ("Greek" (ht (1 (ht ('letter "α") ('name "alpha"))) (2 (ht ('letter "β") ('name "beta"))))) ("English" (ht (1 (ht ('letter "a") ('name "A"))) (2 (ht ('letter "b") ('name "B")))))))) (ht-get* alphabets "Greek" 1 'letter)) ; => "α"
ht-get
and ht-get*
have gv-setters and so will work with setf
:
(let ((table (ht-create))) (ht-set! table 1 "A"))
is equivalent to
(let ((table (ht-create))) (setf (ht-get table 1) "A"))
(let ((table (ht (1 (ht (2 (ht (3 "three")))))))) (ht-set! (ht-get (ht-get table 1) 2) 3 :three))
is equivalent to
(let ((table (ht (1 (ht (2 (ht (3 "three")))))))) (setf (ht-get* table 1 2 3) :three))
Libraries like s.el (strings) and dash.el (lists) have shown how much nicer Emacs lisp programming can be with good libraries. ht.el aims to similarly simplify working with hash tables.
Common operations with hash tables (e.g. enumerate the keys) are too difficult in Emacs lisp.
ht.el offers:
- A consistent naming scheme (contrast
make-hash-table
withputhash
) - A more natural argument ordering
- Mutation functions always return
nil
- A more comprehensive range of hash table operations, including a
conventional map (
ht-map
returns a list, elisp'smaphash
returns nil).
Similar libraries
- kv.el (focuses more on alists)
- mon-hash-utils
Installation
ht.el is available on MELPA (recommended) and Marmalade.
Add MELPA to your .emacs.d/init.el:
(require 'package) (add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
then run M-x package-install <RET> ht <RET>
.
Changelog
ht.el uses semantic versioning, so an incompatible API change will result in the major version increasing. See CHANGELOG.md for a history of all changes.
Running tests
M-x ht-run-tests
What's an alist/plist?
An alist is an association list, which is a list of pairs. It looks like this:
((key1 . value1)
(key2 . value2)
(key3 . value3))
An alist can also look like this:
((key1 . value1)
(key2 . value2)
(key1 . oldvalue))
A plist is a property list, which is a flat list with an even number of items. It looks like this:
(key1 value1
key2 value2
key3 value3)
Both of these are slow. ht.el provides ht<-alist
and
ht<-plist
to help you convert to hash tables. If you need to
work with an alist or plist, use the functions ht->alist
and
ht->plist
to convert an hash table to those formats.
Recommend
-
171
Rust Emacs This project isn't maintained anymore. If you are looking for a rust based emacs fork, you...
-
117
Helpful Helpful is an alternative to the built-in Emacs help that provides much more contextual information.
-
137
ag.el Ag.el allows you to search using ag from inside Emacs. You can filter by file type, edit results inline, or find files. Ag.el tries very hard to be Do-What-I-Mean, and will make intelligent suggestions about what...
-
88
Imagine that you hold Control and type your name into Emacs. Can you describe what will happen? – The ‘Emacs Guru Test’ Emacs shortcuts (known as ‘key bindings’) can seem ridiculous to beginners. Some...
-
76
README.md elisp-def
-
82
README.md elisp-refs
-
45
README.md tco.el Tail call optimisation for Emacs lisp
-
9
It's Difftastic! Difftastic is an experimental structured diff tool that compares files based on their syntax. It is very much unfinished. It works reasonably on very parenthesised data (lisps, JSON), it works sometimes on oth...
-
14
It's Difftastic! Difftastic is an experimental structured diff tool that compares files based on their syntax. It is very much unfinished. It works reasonably on very parenthesised data (lisps, JSON), it works sometimes on oth...
-
3
pyimport Manage Python imports from Emacs! Installation: install from MELPA, or add pyimport.el to your load-path. Unused Imports Requires pyflakes to be installed. Run M-x py...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK