1

I would like to write a clojure program that can create a structure for a 'story'. For example, I would like to be able to 'add-story', which would create a new story with character and event fields, and 'add-char' would add new characters with attribute fields to a given story.

I would like to be able to create and fill in a 'story', and then be able to exit and return later to find the story in the same state as it was when I left it. Do I need to write a program that is running continuously? How can I store data such as stories/events/characters so that I can come back to them later?

2
  • Use e.g. maps to store story elements. Write to file. Read from file. Commented Aug 13, 2013 at 14:40
  • Or, research databases. Commented Aug 13, 2013 at 15:05

1 Answer 1

3

One way to get the advantages of the Clojure ecosystem on this project would be to represent your story as a map:

{:title "Allice's adventures in Clojure Land"
 :characters []}

then when it changes write it to a file, database, Amazon-s3-bucket, Dropbox, etc. basically anything that will remember bits will do becasue Clojure structures are both printable and readable so serialization and parsing are built in.

then adding a character would look something like:

(update-in story [:characters] conj {:name "Allice"})
{:title "Allice's adventures in Clojure Land", :characters [{:name "Allice"}]} 

then write that string to your data store for later retrieval. The idea behind this is to seperate the data from the storage, and update processes so each can be made as simple as possible.

the easiest (as in most familiar) way to save clojure's core data structures is:

core> (spit "save.edn" (pr-str {:title "Allice's adventures in Clojure Land", 
                                :characters [{:name "Allice"}]})) 
nil
core> (read-string (slurp "save.edn"))
{:title "Allice's adventures in Clojure Land", :characters [{:name "Allice"}]} 

Though this will only work in the most simple programs. In many cases you will need something more sophisticated, like a database or object store (S3). Clojure's data format is called Extensible Data Notation or EDN, hence the extension .edn

Sign up to request clarification or add additional context in comments.

2 Comments

Whats the best way to write something to a data store in Clojure that can easily be retrieved at a later date?
Added an example of using spit and pr-str

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.