What is the equivalent of a python dictionary like {'a':1, 'b':2}
in elisp?
And again, does elisp have any map-reduce api?
Association lists are the most commonly used associative containers in elisp. It is just a list of key-value cons cells like this ((key . value))
. You can use the assoc
function to get a value corresponding to a key and rassoc
to get a key with the required value.
Elisp comes with the built-in function mapcar
which does map, but AFAIK there is no good fold
facility. You could emulate it using any of the looping facilities provided. However, the better solution is to use cl-lib
and slip into CommonLisp land. In particular, it supplies cl-mapcar
and cl-reduce
.
assoc
is what makes sense because that compares the keys with equal
. On the other hand, assq
compares with eq
which requires that they be the exact same object in memory. –
Durstin Besides association lists,(whose algorithmic complexity is OK for small tables but not for large ones), there are hash tables, you can construct with make-hash-table
and puthash
, or if you prefer immediate values, you can write them as #s(hash-table data a 1 b 2)
.
Association lists are the most commonly used associative containers in elisp. It is just a list of key-value cons cells like this ((key . value))
. You can use the assoc
function to get a value corresponding to a key and rassoc
to get a key with the required value.
Elisp comes with the built-in function mapcar
which does map, but AFAIK there is no good fold
facility. You could emulate it using any of the looping facilities provided. However, the better solution is to use cl-lib
and slip into CommonLisp land. In particular, it supplies cl-mapcar
and cl-reduce
.
assoc
is what makes sense because that compares the keys with equal
. On the other hand, assq
compares with eq
which requires that they be the exact same object in memory. –
Durstin © 2022 - 2024 — McMap. All rights reserved.