BuddhistCatholicContemplativeEvangelicalGeneral ChristianHinduJewishLatter-day SaintMuslimNew VisionsProgressive Christian
Beliefs
- Buddhist
- Catholic
- Contemplative
- Evangelical
- General Christian
- Hindu
- Jewish
- Latter-day Saint
- More Voices
- Muslim
- New Visions
- Pagan
- Progressive Christian
Topics
- Religion News
- Food and Religion
- Sports and Religion
- Religious Art
- Religious Music and Entertainment
- Religious Events
- History and Religion
- Style and Religion
- Politics and Religion
- Women in Religion
- Recreation Outdoors and Religion
- Business Finance and Religion
- Careers Workplace and Religion
- Education and Religion
- Family Parenting and Religion
- Health Fitness and Religion
- Home and Religion
- Relationships and Religion
- Technology and Religion
- Travel and Religion
Columnists
- Buddhist Columnists
- Business Finance and Religion Columnists
- Careers Workplace and Religion Columnists
- Catholic Columnists
- Contemplative Columnists
- Education and Religion Columnists
- Evangelists Columnists
- Family Parenting and Religion Columnists
- General Christian Columnists
- Health Fitness and Religion Columnists
- Hindu Columnists
- History and Religion Columnists
- Home and Religion Columnists
- Jewish Columnists
- More Voices Columnists
- Latter Day Columnists
- Muslim Columnists
- New Visions Columnists
- Nonreligious Columnists
- Pagan Columnists
- Politics and Religion Columnists
- Progressive Columnists
- Recreation Outdoors and Religion Columnists
- Relationships and Religion Columnists
- Religion News Columnists
- Religious Art Columnists
- Religious Events Columnists
- Religious Music and Entertainment Columnists
- Sports and Religion Columnists
- Style and Religion Columnists
- Technology and Religion Columnists
- Travel and Religion Columnists
- Women in Religion Columnists
Resource Library
- 2026 Calendar
- Library of World Religions
- The 100 Most Holy Places on Earth
- Patheos Answers
- Podcasts
- Videos
- Faith Leaders Database
- Anglican Episcopalian
- Bahai
- Baptist
- Buddhism
- Christianity
- Confucianism
- Eastern Orthodoxy
- Hinduism
- Holiness and Pentecostal
- ISKCON
- Islam
- Judaism
- Lutheran
- Methodist
- The Church of Jesus Christ of Latter-day Saints
- New Age
- Paganism
- Presbytarian and Reformed
- Protestantism
- Religion Behind the Scenes
- Roman Catholic
- Scientology
- Shia Islam
- Sikhism
- Sufism
- Sunni Islam
- Taoism
- Zen
- See all religions
Research Tools
Ad-Free & Subscriber Log-In Subscription Support
Ad-Free & Subscriber Log-In Subscription Support
- Trending:
- Advent
- |
- Forgiveness
- |
- Thanksgiving
- |
- Election
- |
- Seasonal Holidays
- |
- Marriage
Cry 'Woof'
-
Series
- Home
- Catholic
- Adventures in Clojure: Nearly Constant Data
Adventures in Clojure: Nearly Constant Data
Last updated on: July 26, 2014 at 3:15 am by willduquette
Adventures in Clojure: Nearly Constant Data 2014-07-23T19:16:55-05:00 willduquette
Patheos Explore the world's faith through different perspectives on religion and spirituality! Patheos has the views of the prevalent religions and spiritualities of the world.
As I noted last week, the Clojure way is to keep immutable (unchanging) data separate from the mutable data. Thus, you keep the network of rooms distinct from the data structure that indicates where the player and other things are at the present time. The latter changes while the game runs, and the former doesn’t.
At least, that’s the theory. As I showed a couple of weeks ago, you can define the world map as a single, immutable data structure, and never change it thereafter. But there are two problems with this.
First, of course, is that the game map really does change over time. When you get a key, you can unlock a door, opening up a new area in the map. After you blow up Flood Control Dam #3, it washes away the bridge you used to be able to cross to get to the Friendly Forest. However, there are number of ways to handle this without actually changing the map data at run-time. I’ll talk about this in future posts.
The more basic problem is that you might not want to define the entire world map as a single data structure. It will need to be one, ultimately, but you might not want to define it that way. For example, you want might to break it up into a number of files, instead of putting it all in one. Contrast these two implementations:
(def world {
:home {:name "Home Base"
:description "Your home..."
:links {:n :street :s :bedroom}
:initial-contents {:couch :soccer-ball :house-key}}
:street {:name "The Street"
:description "The mean street outside your front door."
:links {:s :home :n :tavern ...}
:initial-contents {:garbage}}
;; This would continue with all of the room definitions,
;; which might get quite large.
...})
On the other hand, you might write code like this:
(define-room :home "Home Base" {:n street :s :bedroom}
"Your home, which has seen better days."
:initial-contents {:couch :soccer-ball :house-key})
(define-room :street "The Street" {:s :home :n :tavern ...}
"The mean street outside your front door."
:initial-contents {:garbage})
This second form is more concise, and makes it easier to see what’s important. Every room has a unique keyword, a brief name, a detailed description, and a set of links to other rooms. Some rooms will have other attributes, such as an “initial contents”. So let’s use positional arguments for the things that all rooms have, and named arguments for the optional things.
However, we’re no longer building an immutable data structure. Instead, each define-room function is adding a room to some kind of “world” map piece-by-piece. And doing that in Clojure takes a little extra work.
The idiomatic approach, I gather (and any experienced Clojure programmers are urged to chime in) is to create an “atom”, and let define-room update the atom. And an atom is an entity that contains some Clojure value in such a way that any updates to the value are “atomic”, that is, if two threads are using the same atom, neither thread will ever see the atom in an intermediate state.
First, you define an atom containing an empty map:
(def world (atom {}))
You can retrieve the map by using the “@” character. For example, to print the contents of the world map, you could say this:
(print @world)
Remember that a Clojure map is a mapping from keys to values. In this case, the key is a room keyword, and the value is the map containing the room’s attributes. Consequently, define-room‘s job is to build the attribute map and insert it into the atom’s map of rooms.
The code to insert an entry into the atom’s map is complicated, and it’s worth looking at it in detail. Let’s suppose we’ve already added some rooms to world, and now we want to add :home. If all we wanted was a new map value with :home added, we could simply use the assoc function, which adds a new key and value to a map, and returns the new map. (The old map is always unchanged.)
(def new-world (assoc @world :home {:name "Home Base" ...})
The variable new-world now contains all of the entries that were in @world, plus the entry for :home. So how do we update a world map in place? It looks like this:
(swap! world assoc :home {:name "Home Base" ...})
The swap! function is updating the atom called world. It is using the assoc function to do it; and it is passing the map stored in the world atom to
assoc as its first argument. In short, this swap! is doing something like this:
let @world = (assoc @world :home {:name "Home Base" ...})
though of course, that’s not valid Clojure code.
Passing functions to other functions is an extremely common paradigm in Clojure, and it takes some getting used to.
Previous Post

July 25, 2014
My, My, This Here Anakin Guy...
Next Post

July 27, 2014 Lumen Fidei: Claiming the Community
Browse Our Archives
Select a Category Admin App o' the Morning Ask the Dogmat Believing Thomas Bits of Fiction Blasts from the Past Bootstrapping the Interior Life Catholic Mom Christian Living Deep Thoughts from the Doghouse Dominicana Evangelii Gaudium Interviews It Was A Dark and Stormy Night Links to This and That Lumen Fidei Meme Watch On Writing Operation Gratitude Pedant Errant Photography Quill Random Barkings S'Mary's World Shakers of Salt Software Development The Dog Whistler The Dogmat Sez Third Orders Thomas Aquinas Uncategorized Vikings at Dinos Woof Guide to Cinema Woof Guide to History Woof Guide to Literature Woof Guide to Obscure Catholic Music Woof Guide to Philosophy Woof Guide to Popular Music Woof Guide to Technology Woof Guide to Theology Words I Wish I'd Written Works For Me
Select a Month September 2016 August 2016 June 2016 May 2016 April 2016 March 2016 February 2016 January 2016 December 2015 November 2015 October 2015 September 2015 August 2015 July 2015 June 2015 May 2015 April 2015 March 2015 February 2015 January 2015 December 2014 November 2014 October 2014 September 2014 August 2014 July 2014 June 2014 May 2014 April 2014 March 2014 February 2014 January 2014 December 2013 November 2013 October 2013
Follow Us!
TAKE THE
Religious Wisdom Quiz
What Christian tradition is noted for its belief in nonresistance and simple living?
Select your answer to see how you score.
Anglican Episcopalian Methodist Amish
Related posts from Cry 'Woof'
Second Look: Amazon Echo after One Week
First Look: Amazon Echo, or Alexa Tells You...
Book Reader Notes: Kindle vs. iBooks vs. Google...
TRENDING AT PATHEOS Catholic
Choosing Virtue Over Luxury And Self-Interest
St. Basil the Great understood two different ways we can find our...
Why AI Needs Limits: Lessons From The Sorcerer’s...
The story of the Sorcerer’s Apprentice is famous in the modern age...
Faith, Reason, and Transubstantiation
“It would be easier for the world to survive without the sun...
If Baptism Washes Away Sin, Why Do Catholics...
The seven sacraments stand as the foundation of Catholic ecclesial life. The...
The Power of Setting a Good Example
Remember last August when the Chicago White Sox hosted a "Pope Hat...
How Grandma Tourism Helps Grandparents Live Their Vocation
Grandma, The New Travel Influencer What started as a nostalgia-inspired social media...
view all Catholic blogs
Ad-Free & Subscriber Log-In Subscription Support
- About Us
- 2026 Calendar
- Videos
- Podcasts
- Library of World Religions
- Faith In Media Tracker
- Advertise With Us
- Write for Us
- Privacy Policy
- Terms of Service
- Do Not Sell My Data
- Contact Us
- Radiant Digital
- Manage Newsletter Subscriptions
- Unsubscribe From Notifications
- Sitemap
Copyright 2008-2026, Patheos, part of the Radiant Digital network of media brands. All rights reserved.
Subscription Support Ad-Free & Subscriber Log-In









