2. Data Types and Immutability
Activity 2.01: Creating a Simple In-Memory Database
Solution:
- First, create the helper functions. You can get the Hash Map by executing the
read-db
function with no arguments, and write to the database by executing thewrite-db
function with a Hash Map as an argument:user=> (def memory-db (atom {})) #'user/memory-db (defn read-db [] @memory-db) #'user/read-db user=> (defn write-db [new-db] (reset! memory-db new-db)) #'user/write-db
- Start by creating the
create-table
function. This function should take one parameter: the table name. It should add a new key (the table name) at the root of our Hash Map database, and the value should be another Hash Map containing two entries – an empty vector at thedata
key and an empty Hash Map at theindexes
key:user=> (defn create-table [table-name] (let [db (read-db)] (write-db (assoc db table-name {:data [] :indexes {}}))...