Last active
August 29, 2015 13:57
-
-
Save oubiwann/9701054 to your computer and use it in GitHub Desktop.
Multi-language Maps Comparison
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% create a new map | |
A = #{key1 => Val1, key2 => Val2, ...} | |
%% pattern match a map | |
#{key1 := Pattern1, key2 := Pattern2, ...} = VarContainingAMap | |
%% updating a map with new key/value pairs | |
NewX = X#{ key1 => Val1, ... KeyN => ValN, ...} | |
%% updating a map with new values for keys that already exist | |
NewX = X#{ key1 := Val1, ... KeyN := ValN, ...} | |
%% keys can be any ground term | |
Z = #{{age, fred} => 12, | |
{age, bill} => 97, | |
{color, red} => {rgb, 255, 0, 0}}. | |
%% extracing values via pattern matching | |
#{{color, red} := X1} = Z. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; create a new empty map | |
(defparameter *my-map* (make-hash-table)) | |
;; add values to map | |
(setf (gethash 'key-one *my-map*) "value one") | |
(setf (gethash 'key-two *my-map*) "value two") | |
;; get a value from a map | |
(gethash 'key-one *my-map*) | |
"value one" | |
;; update a map | |
(setf (gethash 'key-two *my-map*) "value 2") | |
;; iterating over key/values | |
(maphash #'(lambda (k v) (format t "~a => ~a~%" k v)) *my-map*) | |
KEY-ONE => value one | |
KEY-TWO => value two |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; create a new empty map | |
(define my-map (hash)) | |
;; add values to map | |
(define my-map (hash-set my-map 'key-one "value one")) | |
(define my-map (hash-set my-map 'key-two "value two")) | |
my-map | |
'#hash((key-one . "value one") (key-two . "value two")) | |
;; create a new map with default values | |
(define my-map (hash 'key-one "value one" 'key-two "value two")) | |
my-map | |
'#hash((key-one . "value one") (key-two . "value two")) | |
;; get a value from a map | |
(hash-ref my-map 'key-two) | |
"value two" | |
;; update a map | |
(hash-update my-map 'key-two (lambda (x) 2)) | |
'#hash((key-one . "value one") (key-two . 2)) | |
;; iterating over key/values | |
(hash-map my-map vector) | |
'(#(key-one "value one") #(key-two "value two")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; create a new empty map | |
(def my-map (hash-map)) | |
;; add values to map | |
(def my-map (assoc my-map :key-1 "value one")) | |
(def my-map (assoc my-map :key-2 "value two")) | |
;; or you could use merge | |
(def my-map (merge my-map {:key-1 "value one"})) | |
(def my-map (merge my-map {:key-2 "value two"})) | |
;; create a new map with default values | |
(assoc {} :key-1 "value one" :key-2 "value two") | |
{:key-2 "value two", :key-1 "value one"} | |
;; get a value from a map | |
(my-map :key-2) | |
"value two" | |
;; update a map | |
(def my-map (assoc my-map :key-2 "value 2")) | |
;; iterating over key/values | |
(map #(list %) my-map) | |
(([:key-2 "value 2"]) ([:key-1 "value one"])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; create a new property list with default values | |
(put my-map key-1 "value one") | |
(put my-map key-2 "value two") | |
;; get a value from a property list | |
(get my-map key-2) | |
"value two" | |
;; create a non-system (non-default) hash-table for property lists | |
;; we'll make it small for demo purposes | |
(set *hash-table* (vector 2)) | |
;; add key/values | |
(put my-map key-1 "value one" (value *hash-table*)) | |
(put my-map key-2 "value two" (value *hash-table*)) | |
;; get a stored value | |
(get my-map key-1 (value *hash-table*)) | |
"value one" | |
;; peek inside the hash table | |
(value *hash-table*) | |
<[[[my-map key-1] | "value one"] [[my-map key-2] | "value two"]] ...> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; create a new empty map | |
(defdynamic my-hash (make-hash-table)) | |
;; add values to map | |
(puthash 'key-1 my-hash "value one") | |
(puthash 'key-2 my-hash "value two") | |
;; get a value from a map | |
(gethash 'key-2 my-hash) | |
"value two" | |
;; iterating over key/values | |
(maphash #'(lambda (key val) (print "key: " key " and value: " val)) my-hash) | |
"key: "key-2" and value: ""value two" | |
"key: "key-1" and value: ""value one" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; create a new empty property list | |
(set my-map '()) | |
;; create a new property list with default values | |
(set my-map '(#(key-1 "value one") #(key-2 "value two"))) | |
;; get a value from a property list | |
(: proplists lookup 'key-2 my-map) | |
#(key-2 "value two") | |
;; or this | |
(: proplists get_value 'key-2 my-map) | |
"value two" | |
;; iterating over key/values | |
(: lists map (lambda (x) x) my-map) | |
(#(key-1 "value one") #(key-2 "value two")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; create a new ordered-list-based dictionary | |
(set my-map (: orddict new)) | |
;; add values to map | |
(set my-map (: orddict store 'key-1 '"value one" my-map)) | |
(set my-map (: orddict store 'key-2 '"value two" my-map)) | |
;; get a value from a map | |
(: orddict fetch 'key-2 my-map) | |
("value two") | |
;; iterating over key/values | |
(: lists map (lambda (x) x) my-map) | |
(#(key-1 ("value one")) #(key-2 ("value two"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment