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:
- Forgiveness
- |
- Marriage
- |
- Consciousness
- |
- Death Penalty
- |
- Election
- |
- Halloween
Cry 'Woof'
-
Series
- Home
- Catholic
- Adventures in Clojure: Saving the Day
Adventures in Clojure: Saving the Day
Last updated on: July 19, 2014 at 10:04 am by willduquette
Adventures in Clojure: Saving the Day 2014-07-19T10:04:32-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.
See the previous installment here.
It’s a big part of the philosophy of Clojure programming that you keep mutable state (data that can change) separate from immutable state (data that cannot change). There are a number of good reasons for doing this; the one that the Clojure books usually trot out is that it makes supporting concurrency easier because you don’t need to synchronize reads and writes to data that never changes.
Concurrency isn’t all that important to a single-user text adventure game; but keeping the mutable and immutable state separate is still a good idea. To see why, let’s enumerate what’s in each category:
Immutable state:
- The world map
- The descriptions of each room and each thing.
- The initial location of the player and the various other things.
Mutable state:
- The current location of the player and other things.
- What the player is currently carrying.
- Flags indicating which puzzles have been solved.
- A score, if any.
In short, the mutable state is all of the stuff you need to save when you save the game. The immutable state is everything else.
In a language like Java, all of these entities would be built up out of Java objects, each of which has its own internal state. And while it’s possible to keep the immutable state and the mutable state separate in a Java program, that’s not the usual way. And so when you want to save a game, you have to save a network of interconnected objects in such a way that you can rebuild that network later. This is doable—Java has support for it built-in to the language—but it’s tricky.
In Clojure, by comparison, it’s straightforward: put all of the mutable state in a handful of data structures (e.g., nested maps). On save, write them out; on restore, read them back in.
Consequently, my game breaks the mutable state up into three pieces:
- The
playermap, which contains the player’s location, health, and so forth. - The
inventorymap, which contains a (possibly empty) set of “things” for each container in the game. Containers include the player himself (because he can carry things), rooms (because they contain things) and also things like desks, cupboards, boxes, and so forth. - The
fact-base, which records facts about what has gone on in the game so far: rooms visited, puzzles solved, doors unlocked.
These data structures can grow arbitrarily complicated, and it will still be possible to write them out with one function call each, and to read them back in again. Saving and restoring the game is thus a simple affair.
My adventure game hasn’t progressed to the point where saving the game is necessary; but the capability for it needs to be built into the program architecture from the beginning, or you’re going to have problems when the time comes. I intend to be ready. In the meantime, I’ll have more to say about all of these things in future installments.
Previous Post

July 18, 2014
Bertie Fit the Battle of Jericho!
Next Post

July 20, 2014 Lumen Fidei: The Gift of Baptism
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 happened to Jezebel as prophesied by Elijah?
Select your answer to see how you score.
Struck by lightning Burned alive Eaten by dogs Drowned
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
Rejecting Doom: Christian Hope And Human Action
Christianity is a religion of hope. We are to have hope and...
It’s What Comes From Within Which Matters
One of the most difficult things for us to understand is why...
Why I Choose the Eucharist Over the Sermon
Choose the Eucharist, Not the Sermon What are we supposed to get...
5 Design Elements Found in Spiritually Rooted Homes
Putting Spirituality Into Home Decor Our homes are more than our living...
Credo: A Story of God Told with the...
About 10 years ago, I began to imagine a personal "Credo" that...
The Anatomy of Divine Revelation in Catholic Thought
The claim that God has made Himself known to human beings is...
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. All rights reserved.
Subscription Support Ad-Free & Subscriber Log-In









