<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Joe Z. on Medium]]></title>
        <description><![CDATA[Stories by Joe Z. on Medium]]></description>
        <link>https://medium.com/@joezeng?source=rss-3632604493df------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*tEw2Rql_rGa2q-QZ2w6qgw.png</url>
            <title>Stories by Joe Z. on Medium</title>
            <link>https://medium.com/@joezeng?source=rss-3632604493df------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 20:23:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@joezeng/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[I have no idea how immutable data works. Help me find out.]]></title>
            <link>https://medium.com/@joezeng/i-have-no-idea-how-immutable-data-works-help-me-find-out-3f2692815a47?source=rss-3632604493df------2</link>
            <guid isPermaLink="false">https://medium.com/p/3f2692815a47</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[functional-programming]]></category>
            <dc:creator><![CDATA[Joe Z.]]></dc:creator>
            <pubDate>Sat, 05 Mar 2016 18:00:26 GMT</pubDate>
            <atom:updated>2016-04-01T17:27:39.839Z</atom:updated>
            <content:encoded><![CDATA[<p>At a recent job interview, I was asked about the advantages of immutable data types<em> </em>in programming. Although I’d heard of the term and knew what it <em>meant</em>, I found that I had no idea what exactly the advantage was to use them, much less articulate them in an interview question.</p><p>An immutable object is an object that you can’t change once created; if you want to assign a new value to a variable, you have to create a new object and make your variable reference that instead. The concept of immutability is usually associated with <em>functional programming</em>, which is a programming paradigm that’s all about using functions to evaluate the results of data. It’s usually contrasted to <em>procedural programming</em>, which is all about using procedures and instructions to change a program’s state in order to evaluate data.</p><p>My default mode of thinking when I program applications is object-oriented procedural programming — making code that manages objects and invokes their member methods to set their state. None of the projects I’d programmed had ever required a functional programming approach, so I stuck to what I knew and never really pursued it any further.</p><p>That’s not to say that immutability is restricted to functional programs. I knew, for instance, that strings were immutable in most languages I programmed in like Python or Javascript, but not <em>why</em> they were that way. It was just something I ended up working around instead of really thinking about. I say “work around” because in Visual Basic 6, the first language I ever learned, strings were supremely mutable (to the point where you could set the <a href="https://msdn.microsoft.com/en-us/library/05e63829%28v=vs.90%29.aspx">Mid$</a> of a string to something else and it would <a href="http://www.vb6.us/tutorials/vb6-string-functions">replace it automatically</a>), and they were still somewhat mutable in C++, the next language I ended up learning.</p><p>I learned functional programming in Racket in my first year of university (thanks, University of Waterloo CS curriculum). It was fun to learn about and reason with, and some of the concepts like closures and passing functions as objects became supremely useful once I started programming in Javascript. But ultimately the <em>paradigm</em> of functional programming never made its way into my own projects because I couldn’t figure out how to make it useful without mutating things.</p><p>Then that interview happened and it finally hit home that the paradigm was more widespread than I thought it was. I debriefed with my friends about the immutable data type thing, and they revealed that they’d used the paradigm in their own programs for quite some time. I decided to look into the matter seriously because I could ignore it no longer.</p><p>The first thing I learned by looking up was how exactly purely functional programming languages like Haskell handled the parts that you can’t avoid involving mutation, like I/O. It turns out they use a world-transition function and just loop that every time they need to change state to react to some external change like advancing a frame or handling input.</p><p>Luckily enough, that fits perfectly with my existing paradigm for updating the state of a game in a game loop. The way I see it, I can make a game loop function by defining it as:</p><pre>world = gameLoop(input, world);</pre><p>where <em>gameLoop</em> is a pure function that just calculates the new state without changing any of the existing data in place.</p><p>The big problem that wandered into my mind after that was, <strong>isn’t creating a new giant world object every loop a really expensive operation?</strong></p><p>This question is still nagging me quite hard — I couldn’t find any answers that I could really understand given my current knowledge. And so in order to explore whether that really is a problem and how I’d deal with it if it were, I’m going to embark on a little project. I’m going to create a game of Tetris in which <strong>the entire game board is an immutable data type</strong> that uses a world transition function to update itself.</p><p>I’ll be posting a series of Medium articles about the project as I keep updating it, documenting the challenges I face and discoveries I make while programming this game. I hope this guide will be useful to other people, and I also hope to get people’s help along the way in case I make any mistakes that should be corrected.</p><p><a href="https://medium.com/@joezeng/functomino-part-1-the-game-board-69719bcb67ee#.agqvzd3vz">The first article is now up!</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3f2692815a47" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>