Python dictionary or map in elisp
Asked Answered
D

2

8

What is the equivalent of a python dictionary like {'a':1, 'b':2} in elisp? And again, does elisp have any map-reduce api?

Debug answered 27/12, 2014 at 4:12 Comment(0)
D
7

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.

Durstin answered 27/12, 2014 at 4:43 Comment(2)
pradhan, to get value from a map, can one use assq (or) assoc?Debug
Usually, 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
K
9

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).

Kramlich answered 27/12, 2014 at 15:40 Comment(0)
D
7

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.

Durstin answered 27/12, 2014 at 4:43 Comment(2)
pradhan, to get value from a map, can one use assq (or) assoc?Debug
Usually, 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.