tag:blogger.com,1999:blog-55584108609086543172024-02-07T03:46:50.021-08:00Fun CodeAll about having fun with Functional Programming.Mark Bastianhttp://www.blogger.com/profile/17120391255299566028noreply@blogger.comBlogger16125tag:blogger.com,1999:blog-5558410860908654317.post-56895024986271946102017-06-20T17:22:00.001-07:002017-06-20T17:22:44.910-07:00fn-code has movedDear readers,
<br />
<br />
I haven't been blogging for a while here at fn-code, but I've decided to restart the effort. However, one of the challenges of developing good content has been the blogger platform itself. Two particular issues I've seen have been the random "unposting" of some of my posts as well as the decision by Google to no longer host static assets on Drive. So, the new blog (including all of the old posts) is now <a href="http://markbastian.github.io/">http://markbastian.github.io/</a>. I've got plenty of ideas for posts and now that things have settled down for me in other areas of my life you should be seeing new content soon.<br />
<br />
Thanks,<br />
MarkMark Bastianhttp://www.blogger.com/profile/17120391255299566028noreply@blogger.com0tag:blogger.com,1999:blog-5558410860908654317.post-87967017393593757032016-04-07T09:00:00.000-07:002016-04-07T19:15:36.926-07:00Another Tetris Clone in Clojure<b>Introduction</b><br />
Recently I was looking at a problem involving extracting and transposing submatrices of data within a larger data grid. Somehow this got me thinking how easy it would be to rotate <a href="https://en.wikipedia.org/wiki/Tetromino">tetrominos</a> using Clojure. In case you are wondering, it is super easy:<br />
<div>
<pre class="brush: clj">(defn rotate-ccw [shape]
(apply mapv vector (map rseq shape)))
(defn rotate-cw [shape]
(apply mapv (comp vec rseq vector) shape))
</pre>
</div>
These two functions will rotate a tetromino counterclockwise and clockwise, respectively. No math required.<br />
<br />
After doing this trivial exercise I soon found myself asking myself "How fast can I code up Tetris in Clojure." It turns out that the answer is "pretty fast." Let me first say that this is not a new idea. It has been done <a href="http://timothypratley.blogspot.com/2015/07/you-should-be-using-figwheelreagent.html">here</a> and <a href="https://github.com/yogthos/clj-tetris">here</a> as well. I still thought it would be fun, so here is my solution along with some insights.<br />
<br />
<div>
Here's the game. Press the left and right arrow keys to translate, up and down arrows to rotate, and the space bar to make the the tetromino immediately fall into place.<br />
<br />
<div id="app">
</div>
<script src="http://markbastian.github.io/tetris/js/compiled/tetris.js" type="text/javascript"></script>
<br />
Read on to learn more about how the game was actually written.<br />
<b><br /></b>
<b>Data First Design</b><br />
As anyone who has seen my <a href="https://www.youtube.com/watch?v=Tb823aqgX_0">Clojure/conj video</a> or talked to me knows, I believe the best approach to coding a solution to a problem is to model the data literally rather than design an API to represent the data. Why model a domain when you can represent it directly instead? Clojure is made for this approach. This exercise is no different, as the first step is to model the shapes as maps of vector literals. Rather than making some sort of base "Tetromino" class and extending a bunch of methods and having some field that loads a bitmap or some such nonsense, I just did what you see here:<br />
<div>
<pre class="brush: clj">(def shapes
{:I [[0 0 0 0]
[1 1 1 1]
[0 0 0 0]
[0 0 0 0]]
:J [[0 0 0]
[1 1 1]
[0 0 1]]
:L [[0 0 0]
[1 1 1]
[1 0 0]]
:O [[1 1]
[1 1]]
:S [[0 0 0]
[0 1 1]
[1 1 0]]
:T [[0 0 0]
[1 1 1]
[0 1 0]]
:Z [[1 1 0]
[0 1 1]
[0 0 0]]})
</pre>
</div>
Pretty cool, eh? You can look right at the shape entries and see the shapes as they are encoded in the data structures as 1s in a field of 0s. Of course, the current shape will be translating and rotating during the game, so I must keep track of that. I also want to track a few other game values, such as the current score, high score, values to keep track of when a tetromino falls, as well as the cells that are currently locked into place.<br />
<br />
This can all be captured in a single function that generates an initial state representing a complete value for the game.<br />
<div>
<pre class="brush: clj">(defn initial-state []
{:frame 0
:speed 50
:score 0
:high-score 0
:locked #{}
:shape-pos [(rand-int 7) 0]
:shape ((rand-nth (keys shapes)) shapes)})
</pre>
</div>
The data structure above completely represents all of the values I need to keep track of for my entire game. The most interesting entries are locked, shape-pos, and shape. locked is a set of coordinates in the board that contain locked cells. shape-pos is the upper left hand coordinate of the current tetramino. shape is the current falling tetramino shape. Note that I do not keep track of rotation. I just rotate the piece in place.<br />
<br />
At this point, all that remains is filling in some functions and methods around this data for user interaction, game rules, and rendering. Here are some of the details.<br />
<b><br /></b>
<b>Implicit Board Representation</b><br />
Rather than taking the usual approach of creating an MxN grid, I implicitly represent the board by using rules for boundaries (nothing can be outside of the board dimensions) and a hash set to represent the blocks currently "locked in" to the board. This definition looks like this:<br />
<div>
<pre class="brush: clj">(defn valid? [{:keys [locked] :as state}]
(every? (fn [[x y :as c]]
(and ((complement locked) c)
(<= 0 x 9) (< y 22)))
(shape-coords state)))
</pre>
</div>
<br />
Basically, for every coordinate in the current falling shape (The shape-coords function) I check to see if it is within the game's x-y grid and if it is not contained in the locked set of cells.<br />
<br />
Speaking of computing the coordinates of the current shape, this is something I'll do a lot, so here's a handy function to do it:<br />
<div>
<pre class="brush: clj">(defn shape-coords [{:keys [shape-pos shape]}]
(let [d (count shape)]
(for [i (range d) j (range d)
:when (= 1 (get-in shape [i j]))]
(mapv + [i j] shape-pos))))
</pre>
</div>
This function destructures the current shape position and shape cells from the game and returns the coordinates of the 4 cells in the shape (the 1s).<br />
<b><br /></b>
<b>Moves</b><br />
Given the above representations, the game reduces down to making valid tetromino moves within the board. There are two scenarios for moving a tetromino:<br />
<ol>
<li>The user moves it (This does not include the user making the tetromino fall faster). A user can translate or rotate a tetromino. The only thing the game must do when a user performs a translation or rotation is see if the new piece is in a valid location. If it is you accept the new value, if not you just reject the move.</li>
<li>It is falling. When a tetromino falls (either over time or by a user forcing it to fall faster) there is only one thing to consider: Is the new position overlapping the set of locked blocks? If so, we do not allow the tetromino to fall, but instead add the former grid positions of the tetromino to the locked block set.</li>
</ol>
<div>
A key point here is that rather than allowing invalid operations to be performed and throwing exceptions or other similar mechanisms, this program simply rejects any invalid operations and always maintains a valid value for the current state.<br />
<br />
Here are the functions for piece translation and rotation:<br />
<div>
<pre class="brush: clj">(defn x-shift [state f]
(let [shifted (update-in state [:shape-pos 0] f)]
(if (valid? shifted) shifted state)))
(defn rotate [state f]
(let [shifted (update state :shape f)]
(if (valid? shifted) shifted state)))
</pre>
</div>
In the above code x-shift means "translate shift" and f is a function to be applied to the shape position (either inc or dec for right or left translation, respectively). rotate takes on of the two rotation functions I defined at the beginning of this post.<br />
<br />
For falling, the logic is a bit more complex:<br />
<div>
<pre class="brush: clj">(defn fall [state]
(let [shifted (update-in state [:shape-pos 1] inc)]
(if (valid? shifted)
shifted
(let [locked-coords (shape-coords state)]
(-> state
(update :locked into locked-coords)
(score 1)
(#(reduce clear-row % (map second locked-coords)))
(into { :shape ((rand-nth (keys shapes)) shapes)
:shape-pos [(rand-int 7) 0]}))))))
</pre>
</div>
Again, we first do a simple shift. If it is valid, we are done. If it isn't valid that means we've either moved off the bottom of the board or intersected with existing locked pieces. Either way, we lock the unshifted cells into the board and then check to see if rows need clearing. Finally, we add in a new random shape at the top of the board. I also give the player a point when a cell locks.<br />
<br />
The player can also execute a fast drop in which they have a piece prepositioned and want it to immediately lock so they can get the next piece. This is as simple as:<br />
<div>
<pre class="brush: clj">(defn fast-drop [{:keys [locked] :as state}]
(some #(when (not= locked (:locked %)) %)
(iterate fall state)))
</pre>
</div>
The function iterates over the current state with the fall function until the set of locked set of cells change. This will occur when the falling piece can no longer fall.<br />
<b><br /></b>
<b>Clearing Rows</b><br />
Clearing a row occurs when all every cell in a row is locked. When this occurs, state is threaded such that 10 points are awarded, the speed is decreased (more on this in the next section), and cells are kept, removed, or shifted depending on their relation to the row to be removed.<br />
<div>
<pre class="brush: clj">(defn clear-row [{:keys [locked] :as state} row]
(if (every? locked (for [i (range 10)] [i row]))
(-> state
(score 10)
(update :speed dec)
(assoc :locked
(set (for [[i j] locked :when (not= j row)]
(if (< j row) [i (inc j)] [i j])))))
state))
</pre>
</div>
<br />
<b>Bringing It All Together</b><br />
The final function needed is a game-step function that is called by the user interface layer for each game time step. Here's the function:<br />
<div>
<pre class="brush: clj">(defn game-step [{:keys [frame locked speed] :as state}]
(cond-> (update state :frame inc)
(zero? (mod frame (max speed 1)))
fall
(some zero? (map second locked))
(into (dissoc (initial-state) :high-score))))
</pre>
</div>
The function updates the frame at each time step then does some conditional threading at the new game step. First, if the frame modded with the current speed is zero, the tetromino falls. So, speed in this game is really a misnomer. Lower speed makes the tetromino fall more frequently. Finally, if any y coordinate in the locked cells is at 0 (I am using screen coordinates, so 0 is at the top of the screen going down) we reset the game but maintain the current high score.<br />
<br />
<b>A Complete Rule Set and a UI</b><br />
All of the above code in a single namespace completely defines the game engine. All that is needed is a user interface to display the current value and take user input. I won't go into the details of the UI in this post, but might later. However, here are a couple of high level details about the UI:<br />
<ul>
<li>I actually did 2 interfaces - one using Quil and one using Reagent. The one embedded here is the Reagent version.</li>
<li>The Quil UI compiles to both Java and JavaScript where the Reagent version just compiles to JavaScript.</li>
<li>The rules are written as cljc files so cross compile without issue.</li>
<li>Either UI is only about 50-70 lines of code, so are pretty minimal.</li>
</ul>
<b><br /></b>
<b>Conclusion</b><br />
Once again, Clojure's ability to let me model a domain directly as data and then write functions to manipulate those domain values has yielded a simple and concise solution to a problem. I find it pretty amazing that I can write an entire game, including UI, in about 150 lines of code. Not only is the solution brief, but I think the simple function names and full-state representation of values makes the code pretty readable. Despite being a simple case in this post, I've found this technique to be quite useful in domains of any size or complexity. However, a key requirement for this approach is a language that has full support for modeling the domain as data. Clojure is an excellent fit as it was designed to be data first from inception.<br />
<br />
The complete source for this project can be cloned <a href="https://github.com/markbastian/tetris">here</a>.</div>
</div>
<div>
<br /></div>
Mark Bastianhttp://www.blogger.com/profile/17120391255299566028noreply@blogger.com2United States37.09024 -95.712891000000013-36.4186355 99.052733999999987 90 69.521483999999987tag:blogger.com,1999:blog-5558410860908654317.post-55566421527235379522015-12-01T07:00:00.000-08:002015-12-01T07:00:03.765-08:00Clojure: Getting Started<b>Introduction</b><br />
Often people will ask me how to get started in Clojure. Most often they have a Java background, but sometimes they are coming from C++, Python, or some other popular language. Either way, I rehash the same story and spent a bunch of time finding and consolidating the same links as the last time someone asked me this question.<br />
<br />
This post is meant to be my simple, consolidated answer to "How do I get started with Clojure?"<br />
<br />
Here's how...<br />
<br />
<b>Pick a Project</b><br />
Before you do anything else, you really need to pick some simple project you will do in Clojure. In my experience, attempting to learn something without some concurrent hands-on experience isn't particularly effective. My first non-trivial (i.e. <i>(prn "Hello World)</i>) application was an implementation of the board game <a href="https://boardgamegeek.com/boardgame/826/cartagena">Cartagena</a> that I talked about at Clojure/conj. You can watch it right here:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<iframe allowfullscreen="" class="YOUTUBE-iframe-video" data-thumbnail-src="https://i.ytimg.com/vi/Tb823aqgX_0/0.jpg" frameborder="0" height="266" src="https://www.youtube.com/embed/Tb823aqgX_0?feature=player_embedded" width="320"></iframe></div>
I suggest an application that has some state, whether it be a desktop application or single page web app (using ClojureScript). That way you can use atoms and understand how Clojure separates the concerns of state (atoms, agents, or refs), value (data), and transition (functions). I think board games make excellent projects, but then again I really like board games. You could do just about anything.<br />
<br />
<b>Watch & Read</b><br />
Here are a few links I recommend to everyone, not in any particular order. They are all excellent.<br />
<ul>
<li><a href="http://www.braveclojure.com/">Clojure for the Brave and True</a>: Daniel Higginbotham's truly awesome book/online learning series on learning Clojure from the ground up. <a href="http://www.amazon.com/Clojure-Brave-True-Ultimate-Programmer/dp/1593275919/ref=as_li_ss_tl?ie=UTF8&linkCode=sl1&tag=braveclojure-20&linkId=e3c6527befc02cce112deb5b8fbc3774">Buy it here</a>. This is my #1 recommended getting started resource.</li>
<li><a href="http://clojure.org/evaluation">Clojure Evaluation</a>: The single most important documentation page on Clojure in the entire universe. Once you understand this page you pretty much know the entire language (not the APIs). Yes, Clojure is this simple.</li>
<li><a href="http://java.ociweb.com/mark/clojure/article.html">Clojure - Functional Programming for the JVM</a>: Mark Volkmann's extremely direct and to the point single web page description of Clojure.</li>
<li><a href="https://changelog.com/rich-hickeys-greatest-hits/">Rich Hickey's Greatest Hits</a>: Most of these are more philosophical than tutorial in nature, but they give you great insights into the genius behind Clojure. Key concepts such as value, state, identity, etc. are described in these videos. My favorites:</li>
<ul>
<li>Are We There Yet?</li>
<li>Simple Made Easy</li>
<li>The Value of Values</li>
</ul>
</ul>
<div>
<div>
<b>In Conclusion</b></div>
<div>
Hopefully the above tips and links will give you a little help if you are considering Clojure and want to know where to start. Either way, I'll have something to point to next time someone asks me.</div>
<div>
<div>
<br /></div>
</div>
</div>
Mark Bastianhttp://www.blogger.com/profile/17120391255299566028noreply@blogger.com0tag:blogger.com,1999:blog-5558410860908654317.post-4933800477520142732015-10-16T06:00:00.000-07:002015-12-16T10:37:13.442-08:00My Concern with Concerns<b>Introduction</b><br />
In the Computer Science world we often talk about the value of Separation of Concerns (SoC). <a href="https://en.wikipedia.org/wiki/Separation_of_concerns">This Wikipedia article</a> on the subject says that well-separated code is more modular, maintainable, and reusable. The basic idea is that we separate our code into components by their roles so that those pieces can be used and developed independently as well as assembled into a greater whole.<br />
<br />
<div>
However, I am concerned with the traditional treatment of concerns. We often look at concerns as a breakdown of our application into various interconnected pieces, or objects. For example, you might have a game program with a rendering system, an input system, a physics system, and so on. A spreadsheet application might have concerns regarding computations, persistence, and input. At a high level, and borrowing from the Wikipedia article, you might have concerns for things like business logic, persistence, data access, and presentation.<br />
<br />
Two major flaws with the typical treatment of SoC as described above stand out to me:<br />
<ol>
<li>In many of these systems the various parts require knowing about each other to the degree that if you use one, you must also use the others. This leads to complected, interconnected systems. Rich Hickey talks all about it <a href="http://www.infoq.com/presentations/Simple-Made-Easy">here</a>.</li>
<li>We have jumped past some core fundamental concerns of how we think about computing and moved straight to high level constructs (usually objects, layers, or systems). This is what I want to talk about.</li>
</ol>
</div>
<div>
These are the three low level concerns I am concerned about:</div>
<div>
<br /></div>
<div>
<b>1. Representation: How do you describe the world?</b><br />
The first concern to consider is how you represent your world. In a traditional object oriented approach, the simple solution is objects with their corresponding fields. Even in many functional or mostly functional languages like Scala you will use value types to represent your world. Consider the following three ways to represent a person:<br />
<br />
<b>Java
</b><br />
<div>
<pre class="brush: java">package concerns;
public class Person {
private String name;
private int age;
private double weight;
public Person() {
}
public Person(String name, int age, double weight) {
this.name = name;
this.age = age;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
}</pre>
</div>
<b>Scala
</b><br />
<div>
<pre class="brush: scala">case class Person(name : String, age : Int, weight : Double)</pre>
</div>
<b>Clojure
</b><br />
<div>
<pre class="brush: clj">{ :name "Zaphod" :age 21 :weight 160.0 }</pre>
</div>
Clojure is unique in that practitioners generally represents the world as simple data structures. Other languages would have you define a class for your data (Often in a painfully verbose way, I might add), but Clojure simply views everything as data. You can use gen-class or defrecord, but you generally don't unless you are shooting for Java interop or feel the need for a defined structural type.<br />
<br />
Clojure is relatively unique in that it allows you to flexibly represent anything using a small number of primitives and heterogeneous, nested data structures. You have a single, uniform approach to representing anything as data, whether it be simple or extremely complicated.<br />
<br />
Classes, on the other hand, require a new class for any level of modification of representation. A person with height would need to extend a base Person (e.g. PersonWithHeight extends Person). This leads to elaborate type hierarchies and an explosion of representations. Classes also fail disastrously when dealing with temporal changes. Suppose you want to transition from a Caterpillar to a Butterfly or from an Employee to a Manger (You got a promotion. Congratulations!). How do you handle these? Do you pass roles around? Does that work in the caterpillar to butterfly case? Is there a GoF anti-pattern for that?<br />
<br />
This is not to say classes do not have their place. If your representation is well-known at design time a class can be an excellent fit for your problem. For example, a 3D mathematical vector is very well defined with fields along 3 axes. However, using classes to represent things with a wide variety of potential fields or that transition over time can be a disaster.<br />
<br />
Clojure's simple approach to the concern of representation is wonderful, but we are here to discuss the separation of concerns, so let's talk about another concern that we need to keep separate...<br />
<br />
<b>2. Behavior: How do you modify or use your representation of the world?</b><br />
The next concern is how you modify the representation of your world, or how you transition from one representation to another. Tied to this is how you do things with your objects, even if you don't modify them. In OOP this is done by class methods that operate on themselves or objects they are familiar with. This seems natural, as most verbs tend to operate on some noun. For example, a rename method clearly must rename something. What is that thing? For an object, it logically is the thing rename is attached to.<br />
<br />
However, this complects the concerns of representation and behavior.<br />
<br />
Consider the trivial situation of managing named objects. What if I want to rename a person, car, dog, or file? In OOP, I need to add a method to every object (e.g. setName or rename) that is nameable. You might argue for a common base class (e.g. AbstractRenameable), but now everything must extend this class, and you don't want to waste your one-shot inheritance on something so trivial. Instead, maybe you implement IName and proxy a DefaultName. In any event, you are complected up the wazoo for something so simple as the ability to rename an object. And you have to do this every time, for every field on every object.<br />
<br />
Wouldn't it be better to have some independent function that renames every structurally similar piece of data? I can do this trivially in Clojure like this:<br />
<div>
<pre class="brush: clj">(defn rename[o name](assoc o :name name))
(pp/pprint
(rename
{ :name "Zaphod Beeblebrox"
:age 21
:weight 160.0 }
"Ford Prefect"))
(pp/pprint
(rename
{ :type :spaceship
:name "Heart of Gold"
:color :red }
"Millenium Falcon"))</pre>
</div>
This produces the following output:<br />
<div>
<pre class="brush: clj">{:name "Ford Prefect", :age 21, :weight 160.0}
{:type :spaceship, :name "Millenium Falcon", :color :red}</pre>
</div>
One function that works on any structurally similar object. Compare this to putting a rename method on every named object in existence. This is what I call a separation of concerns.<br />
<br />
Let's look at a slightly less trivial example. Suppose you are writing a sprite-based game and have a variety of scene elements that you want to render. Each element must have information for its location and the image to be drawn. Let's look at how you might render a sprite-based image in our three languages using Java 2D:<br />
<br />
<b>Java</b><br />
Here is what you would add to the above class:<br />
<div>
<pre class="brush: java">public void render(Graphics2D g){
g.drawImage(getImage(), getX(), getY(), null);
}</pre>
</div>
This, of course, assumes you have also added fields and methods to manage the image and the item's location. You've now fundamentally intertwined your class with the Java 2D library, some AWT classes for handing images, and whatever else you've done to make this work.<br />
<br />
<b>Scala</b><br />
Scala suffers from the same problem as Java because it still complects the concerns of representation and behavior. In Scala's defense, it does have duck typing, structural typing, and traits that allow you to do a variety of things to repeat yourself a lot less. It's a better situation, but it does add complexity to your solution and you have to go to a lot of effort to separate your behavior from your representation.<br />
<br />
<b>Clojure
</b><br />
Clojure completely separates the concerns of representation and behavior by using pure data structures to represent data and functions to represent behavior. The only required interface is correct structural inputs to functions.<br />
<div>
<pre class="brush: clj">(defn render [g {:keys [image x y]}]
(.drawImage g image x y nil))</pre>
</div>
In this example, any data conforming to the required interface (Having entries for the :image, :x, and :y keys) can be rendered. The data has no knowledge of Graphics2D and is only loosely coupled to AWT by having a field entry referencing an image. This data can be removed from the map entirely if not needed as opposed to an object which would have a null field that still references a foreign API. </div>
<br />
We're now double-complected in OOP-land and have complete separation with Clojure. Let's throw another concern into the mix...<br />
<br />
<b>3. Management: How do you keep track of the current state of the world?</b><br />
Separate from the idea of how you represent and act in your world is how you keep track of your current (and perhaps historical) view of the world. This includes not just a handle to the current value of the world, but a mechanism for watching for changes and responding accordingly.<br />
<br />
In Java this is accomplished via a fully implemented Java Bean, with getters, setters, property change support, property change listeners, and so on. Your bean class will have a mechanism to wire up things that listen for changes and changes are fired when change occurs. Again, we've further tied another concern to the object. We're now complected x3.<br />
<br />
Scala doesn't address this concern directly, but you've got options. You can create Java Beans in Scala, but beans are an ugly, complicated mess that should be avoided if possible. You can use the Akka library, but in my experience Akka is too heavyweight and complicated for most problems.<br />
<br />
Clojure, on the other hand, has atoms, agents, and refs. These concurrency primitives are designed exactly for the concern of state management. These 3 items each hold a value as their current state and have methods for performing safe modification synchronously, asynchronously, uncoordinated, or coordinated, depending on which primitive you need. All have the same API for dereferencing, watching for changes, and validating state. These primitives completely separate the concern of state management from the other concerns described in this post. You can read more about these primitives <a href="http://fn-code.blogspot.com/2015/09/clojure-state-management-by-example.html">here</a>.<br />
<br />
One really great thing about Clojure concurrency primitives is that they are easy to use from any other JVM language, so you can leverage them in your Java or Scala projects to separate this concern if desired.<br />
<br />
<b>Summary and Conclusions</b><br />
The following table summarizes what I have discussed in this post.<br />
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg .tg-yw4l{vertical-align:top}
</style>
<br />
<table align="center" class="tg" style="text-align: center;"><thead>
<tr><th class="tg-yw4l">Concern</th><th class="tg-yw4l">OOP</th><th class="tg-yw4l">FP</th></tr>
</thead><tbody>
<tr><td>State</td><td>Object Fields</td><td>Values/Data</td></tr>
<tr><td>Behavior</td><td>Object Methods</td><td>Functions</td></tr>
<tr><td>Management</td><td>Object References</td><td>Concurrency Primitives</td></tr>
<tr><td>Separation Level</td><td>Complected</td><td>Separated</td></tr>
</tbody></table>
<div style="text-align: center;">
<br /></div>
The key takeaway is that objects fundamentally complect all concerns by their very nature. Clojure's separation of data, functions, and state management allow for a clean separation of these concerns as part of the language design. So, rather than beginning to design the world as objects, layers, and systems start with data, functions, and state. The former solution automatically puts you on the road to complexity while the latter allows you to keep your concerns separated all the way down.<br />
<div>
<br /></div>
Mark Bastianhttp://www.blogger.com/profile/17120391255299566028noreply@blogger.com0tag:blogger.com,1999:blog-5558410860908654317.post-78137493687865141402015-09-24T07:00:00.000-07:002015-09-24T07:00:00.388-07:00Clojure State Management by Example<b>Introduction</b><br />
One of my favorite features of Clojure is the way changes in application state are handled. In Clojure we separate the concerns of our data (which is stored as values) and the management of how that data might change over time. Contrast this to most other languages that have mutable state tied to their object model at some level.<br />
<br />
There are four state management primitives. Here's a very simple example of each and how they behave.<br />
<div>
<br /></div>
<div>
<b>Vars</b></div>
<div>
Vars are what you get when you use def. def simply defines a value. If you declare the def'd value as dynamic, you can rebind it on a per-thread basis. Meaning, within your current scope you can rebind the value of the previously def'd value. Here's the example:</div>
<div>
<div>
<pre class="brush: clj">(def ^:dynamic *a* 0) ;Note the *earmuff*
(def ^:dynamic *b* 1) ;Clojure uses them for things mean to be rebound
(prn (str "(original) a, b = " *a* "," *b*))
(future
(binding [*a* 1 *b* 0]
(prn (str "(rebinding) a, b = " *a* "," *b*))
(binding [*a* 11 *b* 45]
(prn (str "(another binding) a, b = " *a* "," *b*)))
(prn (str "(exiting scope) a, b = " *a* "," *b*)))
(prn (str "(exiting scope) a, b = " *a* "," *b*)))
(prn (str "(original ns value) a, b = " *a* "," *b*))</pre>
</div>
The above produces this output:<br />
<div>
<pre class="brush: clj">"(original) a, b = 0,1"
"(rebinding) a, b = 1,0"
"(original ns value) a, b = 0,1"
"(another binding) a, b = 11,45"
"(exiting scope) a, b = 1,0"
"(exiting scope) a, b = 0,1"
</pre>
</div>
As you can see, every time I rebind a and b in a new form, the old value are replaced within that scope. As soon as the form is exited, we are back to the previous binding. Finally, you can see that the last prn statement that prints the original binding value is unaffected by the future since the future is in a different thread. I don't find vars particularly useful or interesting, but for completeness's sake, there you have it. Our next three concurrency primitives are much more interesting.<br />
<br /></div>
<div>
<b>Atoms</b></div>
<div>
Atoms provide synchronous, uncoordinated state management. These are the workhorse of Clojure state management. Here's how it works using a simple example that updates two atoms and dumps out their results. A built-in delay is added to each update for illustration's sake.</div>
<div>
<div>
<pre class="brush: clj">(def a (atom 0))
(def b (atom 1))
(defn slow [f] (Thread/sleep 300) f)
(defn slower [f] (Thread/sleep 400) f)
(future
(do
(swap! a (comp slow inc))
(swap! b (comp slower dec))))
(future
(loop [i 10]
(when (pos? i)
(do
(prn (str "a, b = " @a "," @b))
(Thread/sleep 100)
(recur (dec i))))))
</pre>
</div>
Output:<br />
<div>
<pre class="brush: clj">"a, b = 0,1"
"a, b = 0,1"
"a, b = 0,1"
"a, b = 1,1"
"a, b = 1,1"
"a, b = 1,1"
"a, b = 1,1"
"a, b = 1,0"
"a, b = 1,0"
"a, b = 1,0"
</pre>
</div>
<br /></div>
<div>
The above output illustrates that atoms are synchronous since it took 300ms for <i>slow</i> to execute on a and an additional 400ms for <i>slower</i> to execute on b. Also, the functions are uncoordinated - There is a time when <i>slow</i> has completed its work and <i>slower</i> has not. There is no connection between the two swap! operations.<br />
<br />
<b>Refs</b></div>
<div>
Refs provide synchronous, coordinated state management. Use a ref when you need a transaction to be performed correctly. For example, you could use Refs to track funds in a bank account. To transfer funds from one Ref'd account to another, put the transaction in a synchronized ref block. The following example is identical to the above, except that now we are altering a and b in a synchronized code block.</div>
<div>
<div>
<pre class="brush: clj">(def a (ref 0))
(def b (ref 1))
(defn slow [f] (Thread/sleep 300) f)
(defn slower [f] (Thread/sleep 400) f)
(future
(dosync
(alter a (comp slow inc))
(alter b (comp slower dec))))
(future
(loop [i 10]
(when (pos? i)
(do
(prn (str "a, b = " @a "," @b))
(Thread/sleep 100)
(recur (dec i))))))
</pre>
</div>
Output:<br />
<div>
<pre class="brush: clj">"a, b = 0,1"
"a, b = 0,1"
"a, b = 0,1"
"a, b = 0,1"
"a, b = 0,1"
"a, b = 0,1"
"a, b = 0,1"
"a, b = 1,0"
"a, b = 1,0"
"a, b = 1,0"
</pre>
</div>
<br /></div>
<div>
Unlike the previous example, no change occurred in a or b until both the <i>slow</i> and <i>slower</i> functions were applied to a and b. Since the operations were synchronous, it took the entire compute time of both functions to pass before both a and be were concurrently updated.<br />
<b><br /></b>
<b>Agents</b></div>
<div>
Agents provide asynchronous, uncoordinated state management. If you want reactive behavior, use agents. As before, we are using the same example to illustrate agent behavior.</div>
<div>
<pre class="brush: clj">(def a (agent 0))
(def b (agent 1))
(defn slow [f] (Thread/sleep 300) f)
(defn slower [f] (Thread/sleep 400) f)
(future
(do
(send a (comp slow inc))
(send b (comp slower dec))))
(future
(loop [i 10]
(when (pos? i)
(do
(prn (str "a, b = " @a "," @b))
(Thread/sleep 100)
(recur (dec i))))))
</pre>
</div>
Output:<br />
<div>
<pre class="brush: clj">"a, b = 0,1"
"a, b = 0,1"
"a, b = 0,1"
"a, b = 1,1"
"a, b = 1,0"
"a, b = 1,0"
"a, b = 1,0"
"a, b = 1,0"
"a, b = 1,0"
"a, b = 1,0"
</pre>
</div>
<br />
In this case, we see that both <i>slow</i> and <i>slower</i> executed concurrently, since a was updated after 300ms and b was updated only 100ms later.<br />
<br />
<b>Summary</b><br />
Clojure's concurrency primitives are very easy to use and make it simple to manage program state. However, it is important to know when to use which. Hopefully this simple set of examples will give you a clear idea as to the behaviors of each so that you'll know when to use them.<br />
<br />Mark Bastianhttp://www.blogger.com/profile/17120391255299566028noreply@blogger.com0tag:blogger.com,1999:blog-5558410860908654317.post-2620112369228496242015-08-21T06:30:00.000-07:002015-08-21T06:41:59.958-07:00A Clojure Snake Game<b>Introduction</b><br />
I recently decided to write a Snake game in Clojure as a small project. In the game you have a snake that grows as it consumes food. The goal of the game is to make the snake as long as possible without self-intersecting. I got the idea to do it on a Saturday morning and by that night I was done. Most of the coding was done in the evening since I was doing family activities all day. All told, this was probably a 3 hour project. The entire program is 75 lines of code. That's awesome! This includes both the JVM and JavaScript targets via Clojure and ClojureScript.<br />
<ul>
</ul>
Here's the game:<br />
<br />
<div class="container">
<canvas height="400" id="snake-canvas" style="border: 1px solid #000000;" width="400"></canvas>
</div>
<script src="http://markbastian.github.io/snake/js/snake.js"></script>
<script type="text/javascript">snake.core.launch_app(document.getElementById("snake-canvas"), 400, 400);</script>
<br />
Here's how you play:<br />
<ul>
<li>Use your arrow keys or w, a, s, d to change the snake's direction. Note that you probably will need to click on the canvas first to gain focus.</li>
<li>When the snake hits a green "food" pill it grows one unit longer.</li>
<li>When the snake intersects itself it resets to its original length.</li>
<li>Your score is the length of your snake.</li>
</ul>
<b>The Program</b><br />
Here's the program. Read past it for my observations and comments.<br />
<br />
<small>
<div>
<pre class="brush: clj">(ns snake.core
(:require [quil.core :as q #?@(:cljs [:include-macros true])]
[quil.middleware :as m]))
(def world { :width 100 :height 100 :food-amount 1000 })
(defn gen-food [] [(rand-int (world :width)) (rand-int (world :width))])
(defn replenish-food [initial amount]
(loop [food initial] (if (>= (count food) amount) food (recur (conj food (gen-food))))))
(defn wrap [i m]
(loop [x i] (cond (< x 0) (recur (+ x m)) (>= x m) (recur (- x m)) :else x)))
(defn grow-snake [{:keys [snake velocity] :as state}]
(let [[px py] (map + (peek snake) velocity)]
(assoc state :snake (conj snake [(wrap px (world :width)) (wrap py (world :height))]))))
(defn eat [{:keys [snake food] :as state}]
(if-let [pellet (food (peek snake))]
(-> state (update :food disj pellet))
(-> state (update :snake subvec 1))))
(defn reset? [{:keys [snake] :as state}]
(if (apply distinct? snake)
state
(assoc state :snake [(peek snake)])))
(defn setup []
(do (q/smooth)
(q/frame-rate 30)
{:snake [[50 50]] :velocity [1 0] :food (replenish-food #{} (world :food-amount))}))
(defn draw [{ :keys [snake food] }]
(let [w (/ (q/width) (world :width))
h (/ (q/height) (world :height))]
(do
(q/smooth)
(q/stroke-cap :round)
(q/stroke-join :round)
(q/background 0 0 0)
(q/fill 0 255 0)
(q/stroke 0 255 0)
(doseq [[x y] food](q/rect (* w x) (* h y) w h))
(q/fill 255 0 0)
(q/stroke 255 0 0)
(doseq [[x y] snake](q/rect (* w x) (* h y) w h))
(q/fill 0 255 255)
(q/text (str "Score: " (count snake)) 10 15))))
(defn launch-sketch [{:keys[width height host]}]
(q/sketch
:title "Snake"
:setup setup
:update #(-> % grow-snake eat (update :food replenish-food (world :food-amount)) reset?)
:draw draw
:key-pressed
(fn [{ :keys [velocity] :as state} { :keys [key key-code] }]
(case key
(:a :left) (if (not= [1 0] velocity) (assoc state :velocity [-1 0]) state)
(:d :right) (if (not= [-1 0] velocity) (assoc state :velocity [1 0]) state)
(:w :up) (if (not= [0 1] velocity) (assoc state :velocity [0 -1]) state)
(:s :down) (if (not= [0 -1] velocity) (assoc state :velocity [0 1]) state)
state))
:middleware [m/fun-mode]
:size [width height]
#?@(:cljs [:host host])))
;#?(:clj (launch-sketch { :width 400 :height 400 }))
#?(:cljs (defn ^:export launch-app[host width height]
(launch-sketch { :width width :height height :host host})))
</pre>
</div>
</small>
<br />
<b>Observations and Comments</b><br />
The state in this program is represented by a simple data structure containing a vector describing the snake, a vector representing the snake's velocity, and a set of coordinates representing the locations of food. You can see where I create this at the end of the setup function.There are also some constants in the world def.<br />
<br />
To update the state, this program makes use of the common Clojure pattern of "threading state." Basically, you write your functions so that program state is passed in as an argument and a modified, updated version of the program state is returned. Functions with a single action or concern are written in this manner and then chained to form more complicated behaviors. It makes your program very easy to reason about. In this program you can see where this is done in the update method:<br />
<br />
<small>
<div>
<pre class="brush: clj">#(-> % grow-snake eat (update :food replenish-food (world :food-amount)) reset?)</pre>
</div>
</small>
<br />
For those unfamiliar with Clojure, I am using the "thread first" macro (The arrow). The # creates an anonymous function with the % as the passed in argument. The arrow takes the argument and feeds it through each function in succession (grow-snake, then eat, then updating food, and so on.).<br />
<br />
At the program level, the Quil library handles passing state to each relevant function for processing. In "fun-mode" (functional mode), Quil functions hand you the initial state for modification in methods for program setup, update, and input. For drawing, state is passed in and there is no function output since you will draw your state to the screen. In other applications you can follow this same pattern of state management using Clojure's amazing concurrency primitives (atoms, agents, and refs). In fact Quil is just using an atom under the hood.<br />
<br />
Other minor details:<br />
<ul>
<li>There's a commented out form (;#?(:clj (launch-sketch { :width 400 :height 400 }))) towards the end. Uncomment this if you want to launch the file from a REPL. I leave it commented so that it doesn't launch when I do a cljsbuild.</li>
<li>For some reason, the (:gen-class) directive doesn't seem to have any effect in the cljc file, so I have a separate launcher.clj that defines a main method for uberjar builds. Clone the project if you want to see what I mean.</li>
</ul>
You can clone the project <a href="https://github.com/markbastian/snake">here</a>.
<br />
<br />
<b>Conclusion</b><br />
Clojure continues to amaze me by repeatedly enabling me to do so much in such a small amount to time and code. Simple ideas, such as representing your domain as data structures vs. classes and the ability to thread your state throughout your program via functions make development in Clojure rapid and productive. Nowadays, whenever I get an itch to try out a new program or concept, I just sit down and model the domain as data and then start writing functions to manipulate the data. Before I know it, I end up with a complete program. It's a very powerful and fun way to write applications.
<br />
<script type="text/javascript">
window.addEventListener("keydown", function(e) {
// space and arrow keys
if([32, 37, 38, 39, 40].indexOf(e.keyCode) > -1) {
e.preventDefault();
}
}, false);</script>Mark Bastianhttp://www.blogger.com/profile/17120391255299566028noreply@blogger.com0tag:blogger.com,1999:blog-5558410860908654317.post-17521820895308135392015-08-06T07:00:00.000-07:002015-08-06T07:00:00.083-07:00Clojure is Homoiconic, Java is Not<b><span style="font-family: Arial, Helvetica, sans-serif;">Introduction</span></b><br />
<span style="font-family: Arial, Helvetica, sans-serif;">Recently I was reading this <a href="http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html">article</a> regarding what is (or was) new in Java 8 and took an interest in the following section on Lambdas:</span><br />
<blockquote class="tr_bq">
<span style="font-family: Arial, Helvetica, sans-serif; line-height: 16px;">Lambda Expressions, a new language feature, has been introduced in this release. They enable you to treat functionality as a method argument, or code as data. Lambda expressions let you express instances of single-method interfaces (referred to as functional interfaces) more compactly.</span></blockquote>
<span style="font-family: Arial, Helvetica, sans-serif;">I did a quick <a href="https://www.google.com/search?q=java%20lambdas">Google search</a> on "java lambdas" and <a href="https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html">this tutorial was the first hit</a>. Once again, the same type of statement is made:</span><br />
<blockquote class="tr_bq">
<span style="font-family: Arial, Helvetica, sans-serif; line-height: 19px;">One issue with anonymous classes is that if the implementation of your anonymous class is very simple, such as an interface that contains only one method, then the syntax of anonymous classes may seem unwieldy and unclear. In these cases, you're usually trying to pass functionality as an argument to another method, such as what action should be taken when someone clicks a button. Lambda expressions enable you to do this, to treat functionality as method argument, or code as data.</span></blockquote>
<span style="font-family: Arial, Helvetica, sans-serif;">The thing that struck me about these articles is the consistent statement that that Java Lambdas (a.k.a. anonymous instances of single method interfaces) are "code as data" because the function can be passed as an argument. I guess if you define data as "instances of classes or functions" then this description is fine, but when I read these articles this is what comes to mind:</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibtlp7uUHl8-prizbR5ZqXGW4RrEZI3Olef8B154-pCbKIFRCN0zUt4d7UDfYuZNXsdyovMs-3kPrrw_0iVKdHkD8HMgotrdXDDxoIFwZGrr1z8rxU38hqvurwC3XnEBKu4cUchu5ZFuo/s1600/codeasdata.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: Arial, Helvetica, sans-serif;"><img border="0" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibtlp7uUHl8-prizbR5ZqXGW4RrEZI3Olef8B154-pCbKIFRCN0zUt4d7UDfYuZNXsdyovMs-3kPrrw_0iVKdHkD8HMgotrdXDDxoIFwZGrr1z8rxU38hqvurwC3XnEBKu4cUchu5ZFuo/s320/codeasdata.jpg" width="320" /></span></a></div>
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">Technically, the ability to assign a function to a variable or pass a function as an argument to another function means that your functions are "first class" which is a good thing. However, I would not call this "code as data." Another related term is "higher order functions." These are functions that take functions as their arguments or return functions as their results. Again, this is a very powerful language feature that Java now sort of does, but this is not "code as data."</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">What do I think of when I think of "code as data?" I think of "Homoiconicity." Google seems to agree, since when I type <a href="https://www.google.com/search?q=code%20as%20data">"code as data" into a search box</a> the first thing that comes up is <a href="https://en.wikipedia.org/wiki/Homoiconicity">this Wikipedia article on Homoiconicity</a>. Let's explore the concept in more detail.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<b><span style="font-family: Arial, Helvetica, sans-serif;">What is Homoiconicity?</span></b><br />
<span style="font-family: Arial, Helvetica, sans-serif;">Often homoiconicity is defined one the following ways:</span><br />
<ul>
<li><span style="font-family: Arial, Helvetica, sans-serif;">The code is written in the data structures of the language.</span></li>
<li><span style="font-family: Arial, Helvetica, sans-serif;">Code as data.</span></li>
</ul>
<div>
<span style="font-family: Arial, Helvetica, sans-serif;">Those more familiar with the concept seem to use "Code as data" more, but I think the first definition is a bit more clear if you are getting started with the idea. Either way, I am going to illustrate the concept by showing how Clojure code is actually written as Clojure data structures. To contrast, I'll also show what "homoiconic Java" would look like. Finally, I'll show you a simple Clojure macro in which we rearrange our code/data.</span></div>
<div>
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<b><span style="font-family: Arial, Helvetica, sans-serif;">Homoiconic Clojure</span></b></div>
<div>
<span style="font-family: Arial, Helvetica, sans-serif;">To start, consider the following core Clojure data structures:</span></div>
<div>
<ul>
<li><span style="font-family: Arial, Helvetica, sans-serif;">() is an empty List.</span></li>
<li><span style="font-family: Arial, Helvetica, sans-serif;">{} is an empty Map.</span></li>
<li><span style="font-family: Arial, Helvetica, sans-serif;">[] is an empty Vector.</span></li>
<li><span style="font-family: Arial, Helvetica, sans-serif;">#{} is an empty Set.</span></li>
</ul>
<span style="font-family: Arial, Helvetica, sans-serif;">When we write code in Clojure, it is expressed in terms of the above data structures. Here is a very simple example that applies the str function (it's kind of like .toString) to the number 42:</span></div>
<div>
<pre class="brush: clj">user=> (str 42)
"42"</pre>
</div>
<span style="font-family: Arial, Helvetica, sans-serif;">You might look at this and think this is the same as the following Java code:</span><br />
<div>
<pre class="brush: java">public static String str(Integer i){
return i.toString();
}
str(42);
</pre>
</div>
<span style="font-family: Arial, Helvetica, sans-serif;">The only difference is the location of the parenthesis, right? Wrong! (str 42) not actually the function str with the argument of 42, but is a list containing two elements, the str symbol and the value 42. When the Clojure evaluator sees a list it attempts to resolve the first symbol (str in this case) and calls that function with the remaining elements of the list as arguments. While this may seem like splitting hairs at the moment, this is very important when you get to macros. It is also crucial to the point that Clojure code is data.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">Here's another one:</span><br />
<div>
<pre class="brush: clj">(defn add [a b](+ a b))</pre>
</div>
<span style="font-family: Arial, Helvetica, sans-serif;">Again, you might think this is the same thing as this Java function:</span><br />
<div>
<pre class="brush: java">public static int add(int a, int b){
return a + b;
}</pre>
</div>
<span style="font-family: Arial, Helvetica, sans-serif;">As before, they are not the same. The above Clojure add function is actually built up with two lists (one nested) and a vector of arguments. The inner list contains three symbols (+, a, and b) and the outer list contains the defn symbol, a symbol naming your function ("add"), a vector of arguments, and our inner list.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">Here's another example:</span><br />
<div>
<pre class="brush: clj">(merge-with + { :x 1 :y 2 :z 3 } { :x 9 :y 2 :z 4 })
</pre>
</div>
<span style="font-family: Arial, Helvetica, sans-serif;">By this point you should see that we have a list, two maps, and a bunch of symbols and values as opposed to a function that adds two maps. Yes, the evaluator will merge the maps, but the code itself is data (as in the native data structures of the language).</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">You can do this type of structural breakdown with any Clojure code. Clojure programs are literal data structures consisting of nested literal data structures, values, and symbols. For more details, read <a href="http://clojure.org/evaluation">this</a>. The key takeaway is that we are indeed writing our code as a bunch of data (literal data structures containing nested data structures, values, and symbols).</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<b><span style="font-family: Arial, Helvetica, sans-serif;">Homoiconic Java</span></b><br />
<span style="font-family: Arial, Helvetica, sans-serif;">What if you wanted to write Java in its own data structures?</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">Here are our Java collection interfaces that correspond to the Clojure ones above:</span><br />
<div>
<ul>
<li><span style="font-family: Arial, Helvetica, sans-serif;">java.util.List</span></li>
<li><span style="font-family: Arial, Helvetica, sans-serif;">java.util.Map</span></li>
<li><span style="font-family: Arial, Helvetica, sans-serif;">java.util.Vector</span></li>
<li><span style="font-family: Arial, Helvetica, sans-serif;">java.util.Set</span></li>
</ul>
</div>
<span style="font-family: Arial, Helvetica, sans-serif;">Java, for some inexplicable reason, does not yet have collection literals, so this will be a very verbose exercise. My apologies up front.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3VmI7AIpacsxdqRmYovzGVbujvv-U6nh-jVSkRfmNdfdvPCTn6JTSONnMIkowPcpUkAkQj18aVFuQVDILGWlIk_LdgHX23Qe-ihiwGbikjEIwezthKtjEF9asaDFitaL50VrLTZwQats/s1600/CollectionLiterals.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: Arial, Helvetica, sans-serif;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3VmI7AIpacsxdqRmYovzGVbujvv-U6nh-jVSkRfmNdfdvPCTn6JTSONnMIkowPcpUkAkQj18aVFuQVDILGWlIk_LdgHX23Qe-ihiwGbikjEIwezthKtjEF9asaDFitaL50VrLTZwQats/s320/CollectionLiterals.jpg" width="240" /></span></a></div>
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">Ok, now let's write some Java code in the data structures of the language:</span><br />
<div>
<pre class="brush: java"> List add = new LinkedList();
add.add("+");
add.add(1);
add.add(1);
</pre>
</div>
<span style="font-family: Arial, Helvetica, sans-serif;">Sadly, I have no way to evaluate this code, but, hey, it's data. I chose to use a String to represent the + operation because Java doesn't have symbols, either.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">Here's another attempt at how I might construct some code as data in Java. I realize that this will be an utter failure, but you're just going to have to follow along as I write a ton of code to make my point.</span><br />
<div>
<pre class="brush: java"> Map a = new HashMap();
a.put("x", 1);
a.put("y", 2);
a.put("z", 3);
Map b = new HashMap();
b.put("x", 9);
b.put("y", 2);
b.put("z", 4);
List mergemaps = new LinkedList();
add.add("merge-with");
add.add("+");
add.add(a);
add.add(b);
</pre>
</div>
<span style="font-family: Arial, Helvetica, sans-serif;">Again, this can't be evaluated, but it's about as close as you can get to homoiconicity in Java.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<b><span style="font-family: Arial, Helvetica, sans-serif;">Why Homoiconicity? - Macros</span></b><br />
<span style="font-family: Arial, Helvetica, sans-serif;">Consider this question: "What can you do with data?" Think about an Excel spreadsheet, a simple comma separated value file, or some JSON data. These are things you can easily sort, manipulate, and transform.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">In the same way, Clojure code can be manipulated and transformed as data. In Clojure there is a facility for this called a macro. When Clojure evaluates macros, it does not evaluate the arguments as with a regular function. Rather, it passes the unevaluated arguments into the macro to be manipulated. Once this is done, the result is returned and evaluated.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">Here's an example:</span><br />
<div>
<pre class="brush: clj">(defmacro bizzaro-math
"Do everything the opposite of normal"
[[op & rest]]
(conj rest (case op
+ -
- +
* /
/ *
op)))
</pre>
</div>
<span style="font-family: Arial, Helvetica, sans-serif;">This macro takes its arguments and inverts the first argument if it is a basic arithmetic operator. Note that this would not be possible if the arguments were evaluated rather than treated as data. Here are some examples of it in use:</span><br />
<div>
<pre class="brush: clj">(bizzaro-math (+ 2 3))
=> -1
(bizzaro-math (- 2 3))
=> 5
(bizzaro-math (* 2 3))
=> 2/3
(bizzaro-math (/ 2 3))
=> 6
(bizzaro-math (rem 2 3))
=> 2
</pre>
</div>
<span style="font-family: Arial, Helvetica, sans-serif;">The most important thing to note here is that the inner forms (e.g. (+ 2 3)) are not evaluated, but are manipulated by the macro, then evaluated. This is the ultimate demonstration that we do, indeed, have code as data.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">A more in-depth discussion of macros is beyond the scope of this post, but the main takeaway is that macros allow you to manipulate code. This is a feature unique to homoiconic languages.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<b><span style="font-family: Arial, Helvetica, sans-serif;">Parting Thoughts</span></b><br />
<span style="font-family: Arial, Helvetica, sans-serif;">Code as data, a.k.a. homoiconicity, is the ability to write your code in the data structures of your language. This goes way beyond the simple ability to pass a function as an argument. If that's all you are looking for, Java might be all you need. If you want full on FP on the JVM, you've got better options. Scala is a great bridge language that has first class functions, higher order functions, partial evaluation, composition, and all the other things you'd expect from a functional language. Clojure, however, is King if want it all. Not only is it functional, it is truly homoiconic.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">Further Reading:</span><br />
<a href="http://www.paulgraham.com/avg.html"><span style="font-family: Arial, Helvetica, sans-serif;">Beating the Averages</span></a><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><a href="http://www.braveclojure.com/read-and-eval/#5__Macros">Clojure For the Brave and True</a></span><br />
<a href="http://clojure.org/macros"><span style="font-family: Arial, Helvetica, sans-serif;">Clojure Macros</span></a><br />
<br />Mark Bastianhttp://www.blogger.com/profile/17120391255299566028noreply@blogger.com2tag:blogger.com,1999:blog-5558410860908654317.post-62562796391999056922015-07-22T09:02:00.000-07:002015-07-23T15:25:39.032-07:00Three Reasons You May Not Want to Learn Clojure<b>Introduction</b><br />
I've been coding in Clojure for over a year now and not everything is unicorns and rainbows. Whatever language you choose will affect how you think and work and Clojure is no different. Read on to see what some of these effects are and why you might want to reconsider learning Clojure.<br />
<br />
<b>1. You will write at least 10X less code than your Java counterparts</b><br />
Often, the measure of a library or project is its size. Programmers like to compare the size of their projects, just like muscle car people like to compare the size of their engines. Here are some situations where this might happen:<br />
<ul>
<li>Lunch with buddies</li>
<li>Job interview</li>
<li>Internal performance evaluation</li>
</ul>
Here's a typical conversation:<br />
<br />
You: "Yeah, I just finished delivering the Awesome 2000."<br />
Them: "Oh, yeah? Tell me about it."<br />
You: "Well, it is this software product that simultaneously creates world peace, solves world hunger, and saves the environment. Zero carbon footprint, too."<br />
Them: "Cool, how many lines of code was that?"<br />
You: "Oh, like 2 million."<br />
Them: "Wow, awesome."<br />
<br />
You will now get a high five, a job offer, or a big raise depending on who you are talking with. However, if you stated that the number of lines of code in your product was 200 then the response might be more along the lines of "Oh, a toy program written in a toy language." For some reason, probably because it is easy, many people like to equate lines of code with programming ability rather than some other metric like, oh, I don't know, the actual features of the programs you've developed. Clojure, sadly, excels at implementing features but is very poor at being verbose about it.<br />
<br />
If you want to demonstrate your ability to code by writing lots of it, Clojure is a terrible, horrible, no-good language. I can't think of another language that is worse in this respect. You will never be able to brag again, in an circumstance, about how big your code base is compared to someone else's.<br />
<br />
<b>2. You won't be marketable for certain jobs</b><br />
True Story: Once upon a time I was at a job interview and the interview panel asked me to go up to the board and code up a Fibonacci sequence generator. I asked what language they wanted it in and they said they didn't care, but they were a C++ shop. Since they didn't care, I wrote a very short tail-recursive Scala version which I am sure they found incomprehensible. I used to program in C++ and at one time I think I was quite good at it. However, I am just not that interested in C++ any more. It was a great job interview because I didn't get the job. I am sure that the story would have ended the same had I done the exercise in Clojure, except for the parenthesis would have been in the right place.<br />
<br />
If you want to be a professional Clojure programmer, there will be a smaller number of job openings for someone of your skills and interests than for the standard Java or C# developer. If you are interested in being part of a clone army doing widely-available boring work, Clojure is not for you.<br />
<br />
<b>3. You will get things done faster</b><br />
Clojure makes you productive, really productive. So, if you can get a product to market 3-10 times faster than you once could, what does your employer do with all that extra time you just bought them? Fire you and pocket the savings, that's what! Just kidding. Mostly.<br />
<br />
While it is true that a company might take advantage of their newfound savings in cost and time by eliminating the software team that just bought them the savings, the more likely scenario is that 1) they will reapply you to something else; or 2) if they are trimming fat, you aren't part of it. Often you will become the new go-to person for getting things done because you've demonstrated an ability to get things done. This does lead to problem 3a: You have too much work to do because you are a productivity magnet.<br />
<br />
While you can't predict what your employer will do in a given situation, one constant you can be sure of in the current software job market is that you must be agile. If you demonstrate value you can be sure that will have the opportunity to continue to demonstrate value. If, on the other hand, you resist change you will eventually be phased out by your own employer or by another company that displaces yours entirely.<br />
<br />
<b>Conclusion</b><br />
Yes, Clojure is an enabling and empowering language, but this is not without its down sides. I didn't even get into some of the other issues, like not having the opportunity to rewrite your Java code in JavaScript because Clojure compiles both ways. If these are the kinds of problems you like to deal with, you just might want to give Clojure a try.<br />
<br />
<b>Shameless Plug</b><br />
Like this blog? Please share, subscribe, or follow it (just enter your email at the top right).Mark Bastianhttp://www.blogger.com/profile/17120391255299566028noreply@blogger.com0tag:blogger.com,1999:blog-5558410860908654317.post-37284737659170901112015-07-14T06:00:00.000-07:002015-07-14T16:22:07.975-07:00Quil, Clojure, and WORA<b>Introduction</b><br />
Since my last post, I've been playing with the excellent <a href="http://quil.info/">Quil library</a>, a "Clojure/ClojureScript library for creating interactive drawings and animations." Prior to this, I was writing demonstrations using the following scheme:<br />
<ol>
<li>Develop all logic using cljc files (cljx before Clojure 1.7). In general, application logic is pretty platform-independent and can be wholly or nearly-wholly written in whatever language you are working in (Clojure in our case). Any slight variances (e.g. having to create a java.util.Date or a JavaScript Date) can easily be handled by <a href="http://dev.clojure.org/display/design/Reader+Conditionals">Clojure's new Reader Conditionals</a>.</li>
<li>Decide on a UI technology (likely Java Swing with Java2D or HTML Canvas depending on what type of demo I wanted) and implement a front end using that particular choice.</li>
<li>Optionally implement the "other" solution from #2 so that I now have a Java and JavaScript solution.</li>
</ol>
<div>
This strategy works pretty well, but I still have duplication of effort when it comes to the user experience. The awesome thing about Quil is that it allows you to launch your UI as either a Clojure or ClojureScript application targeting the JVM or JavaScript-enabled browser, respectively. Now, I can pretty much write everything as cljc files with a few reader conditionals and easily produce targets that run on the JVM or in a browser.</div>
<div>
<br /></div>
<div>
To get my feet wet with Quil, I re-implemented the renderer for my <a href="http://fn-code.blogspot.com/2015/06/a-lunar-lander-game-in-clojure.html">Lunar Lander project</a> in Quil. I was so happy with the results that I removed the Canvas/ClojureScript renderer completely and now just use a single renderer for both the JVM and JS versions of the project.</div>
<div>
<br /></div>
<div>
<b>Results</b></div>
<div>
By the time I was done with my new rendering code and refactoring of everything else, all of the cljs files were gone. I now have a single clj file that is nothing more than a (:gen-class) macro and a main function calling the application entry point in the cljc code. Everything else is written as cljc files. Only a small number of reader conditionals were used to make any Clojure or ClojureScript specific changes as required. Rather than go over every example of how I used reader conditionals, take a look at <a href="https://github.com/markbastian/lander/blob/master/src/cljc/lander/game_launcher.cljc">this file</a> that demonstrates how to create a single Quil "Sketch" that works with both host platforms. Aside from the above linked file, the only other conditionals required were a couple java.util.Date vs. JavaScript Date conditionals used in the simulation namespace. The vast majority of the code was identical for all hosts.</div>
<div>
<br /></div>
<div>
<b>Conclusion</b></div>
<div>
I've been developing code for the JVM for over 10 years and have always liked the "Write once, run anywhere" (WORA) aspect of JVM languages, and have found that WORA works for the most part. However, you can't always rely on your host machine having a modern JVM, especially in the era of mobile devices. The browser is really the most ubiquitous host platform, and the ability to write a single application that can be compiled to run as a JVM app or a JavaScript app with very little effort is a huge advantage for Clojure and Clojurists.</div>
<div>
<br /></div>
<div>
<b>Afterthoughts</b></div>
<div>
The complete application described in this blog can be found <a href="https://github.com/markbastian/lander">here</a>. Note that you will need to build and install my <a href="https://github.com/markbastian/numerics">numerics</a> project as well. Once numerics is installed, cd over to lander and type <i>lein run</i> for the Clojure version of the game. You can play the web version right here (Note that you will need a keyboard or some way to emulate the 'f' key and left and right arrow keys.).</div>
<div>
<br /></div>
<div class="container">
<canvas height="400" id="quil-lander-canvas" style="border: 1px solid #000000;" width="400"></canvas>
</div>
<script src="http://markbastian.github.io/quil-lander/js/lander.js"></script>
<script type="text/javascript">lander.game_launcher.launch_app(document.getElementById("quil-lander-canvas"), 400, 400);</script>Mark Bastianhttp://www.blogger.com/profile/17120391255299566028noreply@blogger.com0tag:blogger.com,1999:blog-5558410860908654317.post-15669878086031182222015-06-24T07:00:00.000-07:002015-06-24T07:00:00.526-07:00A Lunar Lander Game in Clojure<b>Introduction</b><br />
In a <a href="http://fn-code.blogspot.com/2015/04/predator-prey-modeling-in-clojure.html">prior post</a>, I spent a great deal of time talking about Predator-Prey systems, ordinary differential equations, and solving these equations in Clojure. In this post, I describe a recent project I did that uses the same ODE solver to do something much more fun - implement a physics-based lunar lander game.<br />
<br />
Here are the rules and operations for the game:<br />
<ul>
<li>To win, you must land the lander on the terrain with a rotation of 0 and an absolute vertical velocity less than 10 m/s.</li>
<li>To engage the lander's thruster, press the 'f' key or space bar.</li>
<li>To turn the lander, press the left and right arrow keys.</li>
<li>If you go off the edge of the screen, land too fast, or land at an angle, you lose.</li>
<li>Press enter to start/restart the game.</li>
</ul>
<div>
The simplest solution is to just press the space bar when you get pretty close to the ground and carefully tap it so that you land very slowly. To give yourself a little challenge, turn the lander and give yourself some horizontal thrust. Then try to land it. I may later enhance the game with landing zones so you can't just land anywhere, but for now you can land anywhere you want.<br />
<br /></div>
Without further ado, here's the game:<br />
<div class="container">
<canvas height="400" id="canvas" style="border: 1px solid #000000;" width="400"></canvas>
</div>
<br />
If you are reading this blog via an aggregator that doesn't pick up the .js content, this is what the game looks like:<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOOUJ0zYwBfAt9bk0rjqbNguYW5X1wHVreWW6-MnKAYAl0h60uvGLw9kE5eeKdb_qsasLEGkWDzmNl5Zfr8eadv4SzMlTUfhRuFXGaSBTaw3bu8IGpxbnD_JwssI9u6PCAaT5VcWySRX0/s1600/lander.tiff" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOOUJ0zYwBfAt9bk0rjqbNguYW5X1wHVreWW6-MnKAYAl0h60uvGLw9kE5eeKdb_qsasLEGkWDzmNl5Zfr8eadv4SzMlTUfhRuFXGaSBTaw3bu8IGpxbnD_JwssI9u6PCAaT5VcWySRX0/s400/lander.tiff" width="397" /></a></div>
<br />
<b>Implementation/Clojure Details</b><br />
Now that you've had some fun trying the game out, here are a few high points. I won't go into a lot of detail in most of these sections since each could be a post in and of itself, but I will spend a good amount of time on multimethods, which were a big win in this application. If you want to build it, the entire project is available at <a href="https://github.com/markbastian/lander.">https://github.com/markbastian/lander.</a> Note that you will need to clone <a href="https://github.com/markbastian/numerics">my numerics library</a> for the Runge-Kutta solver and <i>lein install</i> it.<br />
<br />
<i>Platform Compatibility</i><br />
Clojure 1.7 (Currently RC2) has the new cljc extension, which makes cross-compiling Clojure and ClojureScript a breeze. It is much easier and faster than the old cljx solution. I didn't make a JVM-based solution in parallel this time, but it would be pretty easy.<br />
<br />
<i>Game Physics</i><br />
This was one of the easiest parts of the game since I just modeled the lander using differential equations and stepped forward using a Runge-Kutta integrator at each simulation step. The differential equations are:<br />
\({dp\over dt} = v\)<br />
\({dv\over dt} = -9.81 + thrust\)<br />
<br />
Thrust is only applied when the user turns it on and the actual solution breaks the position (p) and velocity (v) vectors up into their x and y components.<br />
<br />
And here's the code:<br />
<div>
<pre class="brush: clj">(defmethod sim :live [state-ref]
(let [{:keys [theta thrust time state]} @state-ref
t (.getTime (js/Date.))
dt (* (- t time) 1E-3)
dvx #(-> theta (* Math/PI) (/ -180) Math/sin (* thrust))
dvy #(+ -9.81 (-> theta (* Math/PI) (/ -180) Math/cos (* thrust)))
new-states (rk/rk-step [#(% 3) #(% 4) dvx dvy] state dt tableaus/classic-fourth-order)]
(swap! state-ref into { :state new-states :time t })))
</pre>
</div>
<br />
<i>Terrain</i><br />
Terrain is generated using the midpoint displacement method (<a href="https://github.com/markbastian/lander/blob/master/src/cljc/lander/terrain.cljc">source</a>). Read about it <a href="http://www.gameprogrammer.com/fractal.html">here</a>. Every new game procedurally generates a new terrain profile. There's a good chance I'll do a full on 3D terrain generation algorithm for a future blog post.<br />
<br />
<i>State</i><br />
State is held in a single atom. As usual, when writing Clojure apps, I just modeled my problem as a data structure held by a Clojure concurrency primitive and everything else fell into place. The values tracked were game state (discussed later in the multimethods section), physical simulation state, time, theta (the lander's rotation), thrust (toggled on and off by user input), and terrain. All of this was easily managed in a single atom. One of the most beautiful things about Clojure is the simplicity of modeling your domain. When you stop thinking in objects and start thinking in terms of data and functions on data, life becomes much simpler.<br />
<br />
<i>Project Size</i><br />
The project is ~200 LOC. Not too shabby for a complete game. There are certainly areas where I could have further reduced the amount of code, such as in the rendering section, but there comes a point where everything works fine and there's no need to mess with it further. Although I don't believe in brevity for brevity's sake, I do believe less code is easier to maintain and navigate than more code, so I always appreciate a concise solution.<br />
<br />
<div>
<b>Multimethods - The Big Win</b></div>
<div>
In a computer game, you will have different stages of play, or game states (not to be confused with the physics state being maintained in the sim loop). For example, when you start a game there is a setup stage. Once everyone has joined there is a main game phase in which most play occurs. Finally, when someone wins there is usually some sort of congratulatory display or an option to play again. Each stage or game state entails a different set of rules regarding what input is received, what is rendered, and so on. However, each state has common functions such as receiving input and rendering. This is where Clojure multimethods come into play - they allow you to call the same overall game logic, but dispatch different functions based on some custom function.<br />
<br />
In this game, the game is in one of four states:</div>
<div>
<ol>
<li>:before - The state of being before any game has been played.</li>
<li>:live - A game is currently being played.</li>
<li>:win - A game has been played and you won.</li>
<li>:lose - A game has been played and you lost.</li>
</ol>
<div>
Depending on the state of the lander game, different logic is executed for the input, rendering, and simulation functions. For example, in :before, :win, and :lose state the game only allows you to start/restart a game, where in :live state the game accepts input to control the lander. In :live state the game renders the lander, terrain, and stats. In :before state the game only renders its directions.</div>
</div>
<div>
<br />
The cool thing about all of this is that in Clojure I only have one main game loop (shown below). It repeatedly calls game-state, state, and render. At the same time, input is taken via the handle-keydown and handle-keyup methods. What happens when these methods are invoked depends on the current game state, with the right logic being dispatched via multimethods.</div>
<div>
<pre class="brush: clj">(defn ^:export init[canvas]
(set!
(.-onload js/window)
(let [state (atom { :game-state :before })]
(do
(js/setInterval #(do
(gs/game-state state)
(sim/sim state)
(render/render state canvas)) 1)
(set! (.-onkeydown js/document) (fn [e] (in/handle-keydown state e)))
(set! (.-onkeyup js/document) (fn [e] (in/handle-keyup state e)))))))
</pre>
</div>
<br />
Here is how it is done. I have 5 methods that I am invoking polymorphically based on the current game state (Note that they are shown together here, but are in separate files in the project):<br />
<div>
<pre class="brush: clj">;Watch the actual game state and transition to a new state based on the current physical state.
(defmulti game-state (fn [state] (@state :game-state)))
;Simulate the physics of the game based on the game state.
(defmulti sim (fn [state] (@state :game-state)))
;Draw to the screen based on the game state.
(defmulti render (fn [state _] (@state :game-state)))
;Handle input based on the game state.
(defmulti handle-keydown (fn [state _] (@state :game-state)))
(defmulti handle-keyup (fn [state _] (@state :game-state)))
</pre>
</div>
<br />
The actual function dispatched when a defmulti is called depends on the dispatch function. In this case the dispatch function is (fn [state] (@state :game-state)). Now that I've defined the multimethods and their dispatch functions, I need to create methods that dispatch based on the right game state. Here's how it works for the render multimethod:<br />
<div>
<pre class="brush: clj">(defmulti render (fn [state _] (@state :game-state)))
(defmethod render :before [_ canvas] (intro-screen canvas))
(defmethod render :win [state canvas]
(do
(draw canvas @state)
(win-screen canvas)))
(defmethod render :lose [state canvas]
(do
(draw canvas @state)
(lose-screen canvas)))
(defmethod render :live [state canvas] (draw canvas @state))
</pre>
</div>
<br />
So, given the state of the game, different render methods are called based on the state of the game. The same is true for all of the other multimethods defined above. For example, the :live method for the sim multi method was shown earlier. When the game is not in a live state, it simply calls the method show below, which conveniently makes the lander stop wherever it was when the game ended.<br />
<div>
<pre class="brush: clj">(defmethod sim :default [state-ref] ())
</pre>
</div>
I won't go through all of the methods I defined, but you should get the idea from what I've posted. Multimethods provide a very effective and powerful way to dynamically dispatch different behaviors based on a custom defined function.<br />
<br />
Finally, I could have defined my multimethods in one common namespace and provided implementations in another. Had I chose to provide both HTML Canvas and Swing/Graphics2D versions of this application, I could have implemented a single game loop that performs all logic in terms of multimethods and defined implementations in separate namespaces. Depending on which version of the application I wanted to run I would require the corresponding namespaces.<br />
<br />
<b>Conclusion</b><br />
In this post, I presented a lunar lander game written in Clojure along with some observations about the code. As with my other Clojure projects, I am impressed by how I can make a non-trivial application with such a small amount of code. Various aspects of Clojure contributed to this, but the thing that most impressed me with this particular application was the use of multimethods to easily transition between game states and manage which functions should be dispatched under what conditions.
<script src="https://markbastian.github.io/lander/js/lander.js"></script>
<script type="text/javascript">lander.lunarlander.init(document.getElementById("canvas"));</script>Mark Bastianhttp://www.blogger.com/profile/17120391255299566028noreply@blogger.com0tag:blogger.com,1999:blog-5558410860908654317.post-35117718482647243112015-06-11T08:00:00.000-07:002015-06-11T08:00:00.883-07:00Differences between Null, Nil, nil, Some, and None<div>
<b>Introduction</b><br />
During a recent code review, a coworker who had the good fortune of jumping straight from Java to Clojure was asking me a few questions about some Scala code she'd been asked to edit. The particular problem involved recursive accumulation of some list data and our conversation drifted towards some questions regarding the different-yet-similar-sounding terms in these JVM languages. Java, Scala, and Clojure together have Null, Nil, nil, Some, and None. Most of these are related in their respective languages to how exceptional behaviors are handled with respect to references and/or collections. Here I explain each of these from the perspective of their host languages and my opinion of the effectiveness of each solution.<br />
<br />
Before diving in, let me be clear that null exists in all JVM languages, including Scala and Clojure. However, these and other more modern languages provide additional facilities for dealing with situations where Java would default to using null and/or throw NPEs.</div>
<div>
<br /></div>
<b>Java</b><br />
<div>
In Java, you may encounter null when using an uninitialized reference (the default value for all references is null), when attempting to get a collection element that doesn't exist, or when someone decides to return null from their function to indicate a bad result. Java has no special handling for null. If you try doing anything with a null value, you get a NullPointerException and all is lost.<br />
<br />
Not only is trying to do something with null exceptionally bad, Java (pre 8) has no built-in mechanisms to help you out. You just do lots of null checking.<br />
<br />
Here are some methods for dealing with null in Java:<br />
<ul>
<li>Always check your references. Liberally spread if(foo != null){ /*Do stuff with foo here*/ } everywhere you don't have absolute control of your references and aren't absolutely sure foo is initialized.</li>
<li>Always initialize your references at declaration or (preferably) make them final. Effective Java Item #15 says "Minimize Mutability." I find it very hard to be effective when writing Java, but this is definitely a good tip. Whenever possible, make everything final with a meaningful (i.e. non-null) value.</li>
<li>Don't write your code in Java. There are some perfectly good alternatives (See below).</li>
<li>Use the new Optional class from Java 8. This is the similar to Scala's Option type so I won't say much here aside from saying that Scala had it first. There's a trend there.</li>
</ul>
</div>
<div>
<b>Scala</b></div>
<div>
Recognizing the evils of null and NullPointerExceptions, Scala designed a better way - the <i>Option</i> type. Rather than returning null when something goes bad, the Scala way is to return either <i>Some</i> value if the computation succeeded or <i>None</i> if it failed. Here is an example of how you might use Option types to model a random food grab into your refrigerator:<br />
<div>
<pre class="brush: scala"> /**
* A really lame function that might return a random food.
*/
def randomFood = math.random match {
case x if x > 0.75 => Some("Ham")
case x if x > 0.5 => Some("Cheese")
case x if x > 0.25 => Some("Bacon")
case _ => None
}
</pre>
</div>
If you made a call to randomFood you could then call <i>isDefined</i> or <i>isEmpty</i> on the result to see if a meaningful result was returned. If the result is meaningful you can then call <i>get</i> to retrieve the value stored in the Some. In reality, nobody ever does this because it isn't any better than performing a null check. The only time you might do this is when calling Scala from Java because Java doesn't understand Scala functions.<br />
<br />
What people really do with Options is use the fact that Scala implicity converts them to Iterables to perform functional operations on them. You can use functions on Options like map, foreach, flatten, reduce, and so on. Here's an example:<br />
<div>
<pre class="brush: scala">val sandwich = Some("Bread") ::
((0 until 10) map { _ => randomFood }).toList :::
Some("Bread") :: Nil
//Prints something like List(Some(Bread), Some(Ham), Some(Ham), None, Some(Bacon), Some(Bacon), Some(Ham), None, Some(Ham), Some(Bacon), None, Some(Bread))
println(sandwich)
//Prints something like List(Bread, Ham, Ham, Bacon, Bacon, Ham, Ham, Bacon, Bread)
println(sandwich.flatten)
</pre>
</div>
Scala also has Nil, which is the empty list. Nil is the odd man out in this post as it is a specific instance of an empty collection rather than a construct for handling exceptional behavior, especially with respect to null. Due to Scala's powerful type system and inferencing, the Scala compiler will figure out what kind of list you are working with as soon as you add some items to your list.<br />
<div>
<pre class="brush: scala">scala> val x = 1 :: 2 :: 3 :: Nil
x: List[Int] = List(1, 2, 3)
scala> Nil
res0: scala.collection.immutable.Nil.type = List()
</pre>
</div>
As you can see in the last line, Nil can't infer a type when no elements of any type are provided. Sometimes using empty collections like this in Scala can cause problems in which the compiler can't infer the type information. In such cases, you can just declare the type when declaring the def or val (we don't use vars). Here's an example that illustrates the point:<br />
<div>
<pre class="brush: scala">scala> Some(4) map { x => x * x } getOrElse 0
res0: Int = 16
scala> Some(4.0) map { x => x * x } getOrElse 0
res1: AnyVal = 16.0
scala> None map { x => x * x } getOrElse 0
<console>:8: error: value * is not a member of Nothing
None map { x => x * x } getOrElse 0
^
scala> val n : Option[Double] = None
n: Option[Double] = None
scala> n map { x => x * x } getOrElse 0
res3: AnyVal = 0
scala> n map { x => x * x } getOrElse 0.0
res4: Double = 0.0</pre>
</div>
As can be seen from the example, using operations such as <i>map</i> and <i>getOrElse</i> are convenient ways to transform Optional types to desired outputs. At the same time, care must be taken to make sure type information isn't lost so that you get the expected result. Despite these potential pitfalls, Scala's ability to treat Options as Iterables makes dealing with them very convenient.<br />
<br />
Overall, Option is a much better solution than dealing with frequent null checking.<br />
<br /></div>
<div>
<b>Clojure</b></div>
<div>
Being hosted on the JVM, Clojure's nil is identical to Java's null. However, how Clojure treats nil is very different from Java. Rather than exploding every time nil is used <i>a la</i> Java or adding special features to handle exceptional behavior <i>a la</i> Scala, Clojure has a few simple rules that make it easy to work with nil. First, in logical expressions nil logically evaluates to false (along with false itself). In Clojure we call this "truthiness" and it is awesome. Second, when dealing with collections nil is treated like an empty list. These simple rules result in a simple, elegant solution to the dreaded null problem and it also results in a lot less code. To illustrate, here's our random food grab function in Clojure:<br />
<div>
<pre class="brush: clj">(defn rand-food []
(condp < (rand)
0.75 "Ham"
0.5 "Cheese"
0.25 "Bacon"
nil))
</pre>
</div>
As you can see, I don't do anything special (No Some/None construct) and just return nil (which is null in Java). It is the simplest solution, which is what Clojure is all about. In Java, this solution would be error-prone since it is using null. Here's how it works in Clojure:<br />
<div>
<pre class="brush: clj">(let[food (take 10 (repeatedly rand-food))
layers (filter identity food)]
(conj (into ["Bread"] layers) "Bread"))
=> ["Bread" "Ham" "Bacon" "Cheese" "Ham" "Bacon" "Ham" "Bacon" "Bacon" "Ham" "Ham" "Bread"]
</pre>
</div>
<div>
Here's another snippet of examples that show how Clojure deals with nil when it is placed in the position of a null argument or collection. Compare <i>or</i> in Clojure with <i>getOrElse</i> in Scala. Much simpler.</div>
<div>
<pre class="brush: clj">(reduce + (map #(* % %) [1 2 3])) ;A non-nil example
=> 14
(reduce + (map #(* % %) nil)) ;I can reduce into a nil collection
=> 0
(or nil 4) ;Compare to getOrElse in Scala
=> 4
(and nil 4) ;Both items must exist
=> nil
(conj nil 4) ;I can conjoin 4 to nil to create the list (4)
=> (4)
</pre>
</div>
As with Scala, Clojure provides a better way to handle null than Java. In contrast to Scala, Clojure does not add any special classes to handle null. Rather, it uses slightly different rules so that nil has meaning in collection and logical operations. For more on nil in Clojure, read about "nil punning" <a href="http://www.lispcast.com/nil-punning">here</a>.<br />
<br />
Hands down, I prefer Clojure's handling of nil to either Scala or Java's approach.<br />
<b><br /></b>
<b>Conclusion</b><br />
Null is a unavoidable fact of life. Sometimes things go wrong and the best answer is no answer. How this situation is dealt with depends very much on the language you are working in. Java, realizing that null is bad, throws lots of exceptions that you get to deal with, usually with lots of error checking. Scala provides a mechanism to return something (Some value) or nothing (None) that allows you to more easily deal with situations that might go bad. Clojure has a few rules for how to deal with nil, primarily the concepts of "truthiness" and treating nil like an empty list. The rules pretty much solve all your problems when it comes to nil.<br />
<br />
These approaches shed light on the designs of these languages, especially the more recent ones. Scala is, in my opinion, a complicated language that solves many problems via additional mechanisms to deal with the problems inherited from Java. I still think it is a great language, but it does take a decidedly baroque approach to solving problems. Clojure, despite seeming otherworldly at first, is a truly simple language. The more I use it, the more impressed I am with its simple design and philosophy. Either way, I think you'll find either of these language's solutions to the null problem preferable to Java's.</div>
Mark Bastianhttp://www.blogger.com/profile/17120391255299566028noreply@blogger.com0tag:blogger.com,1999:blog-5558410860908654317.post-79991824054036418662015-05-27T08:00:00.000-07:002015-05-27T08:00:00.341-07:00Exercises in Clojure with Commentary<div>
<b>Introduction</b></div>
I was recently browsing some links from my Twitter feed and came across some programming exercises and their solutions. I selected a few that I thought were interesting, solved them, and now I want to present my solutions here along with some commentary on how they were solved. I think the solutions and especially their comments might be helpful to new Clojurists as well as Java developers who are looking for some examples of why Clojure code is so much more concise. Without further ado, here they are.<br />
<div>
<br /></div>
<div>
<b>Problem 1: Square Code/Crypto Square</b></div>
<div>
Read all about it <a href="https://github.com/trikitrok/MyExercismExercises/blob/master/clojure/crypto-square/README.md">here</a> and <a href="http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html#easy">here</a>. Basically, you do the following to encode a text string:</div>
<div>
<ol>
<li>Convert the initial string to a lowercase character string with only alpha characters</li>
<li>Arrange the result of step 1 into a square of text (If the string length is not a square integer the last line will not be as long as the rest of the square)</li>
<li>Create a sentence in which the "words" are the columns of the square</li>
</ol>
<div>
Here is my solution:</div>
<div>
<div>
<pre class="brush: clj">(require '[clojure.string :as s])
(defn crypto-square[s]
(let[ss (-> s s/lower-case (s/replace #"[^a-z]" ""))
l (-> ss count Math/sqrt Math/ceil int)]
(->> ss
(partition l l (repeat ""))
(apply map vector)
(map #(apply str %))
(s/join " "))))
</pre>
</div>
<br />
You can give it a try right here:<br />
<br />
<div>
<fieldset style="background-color: #f0f0f0;">
<br />
<form>
Enter text to encode: <input id="cryptosquare-input" size="80" type="text" value="A beginning is the time for taking the most delicate care that the balances are correct." />
<script>
function myFunction() {
var x = document.getElementById("cryptosquare-input");
var out = practice.funcs.crypto_square(x.value);
document.getElementById("cryptosquare-output").innerHTML = out;
}
</script>
<button onclick="myFunction()" type="button">Convert!</button>
<br />
Result:<br />
<div id="cryptosquare-output">
</div>
</form>
</fieldset>
<br />
Commentary:<br />
<ul>
<li>This solution makes heavy use of threading macros (<a href="https://clojuredocs.org/clojure.core/-%3E">-> is called "Thread first"</a> and <a href="https://clojuredocs.org/clojure.core/-%3E%3E">->> is called "Thread last"</a>). These are great for assembling computation pipelines. These macros take the item after the arrow and repeatedly insert the argument into the next form in the pipeline, carrying forward the result into each successive form. The "first" and "last" refer to where in the succeeding form the new argument goes (first goes after the function name in the form, last goes in the last position in front of the last parens).</li>
<li>The <a href="https://clojuredocs.org/clojure.core/partition">partition</a> function creates partitions from a sequence with a specified chunk and step size. A final default argument is used to pad the last partition when the sequence cannot be equally subdivided.</li>
<li>The default padding argument to partition along with the <a href="https://clojuredocs.org/clojure.core/repeat">repeat</a> function make ensuring that the string was square trivial. Any missing characters in a non-square string are filled in by padding with empty strings.</li>
<li>The apply method (which I use in several more of these examples) "applies" a function to a sequence by inserting the function in the first position of the sequence and evaluates the new sequence as if it were a list.</li>
<li>A key takeaway in this example is that threading computations as shown really only works with immutability and side-effect free functions. Were either of these properties missing, there are no guarantees regarding your computations and assembling these sorts of pipelines are impossible.</li>
</ul>
<b>Problem 2: Kindergarten Garden</b></div>
<div>
In this problem, described <a href="https://github.com/trikitrok/MyExercismExercises/blob/master/clojure/kindergarten-garden/README.md">here</a> and solved <a href="http://garajeando.blogspot.com/2015/05/exercism-kindergarten-garden-in-clojure.html">here</a>, two rows of plants are assigned to a group of students who are sorted alphabetically by name. Each student gets a block of plants from left to right (two each in the front row and two each in the back). Depending on the letters in the diagram, you are to determine which kind of plants each student has.</div>
</div>
<div>
<pre class="brush: clj">;Solution 1
(defn char->plant[s]
(map #(case %
\V "Violets"
\R "Radishes"
\C "Clover"
\G "Grass") s))
(defn kinder[names rows]
(apply merge-with concat
(map
#(zipmap (sort names) (partition 2 %))
(map char->plant (s/split-lines rows)))))
;Solution 2
(defn kinder-thread-last[names rows]
(->> rows
(s/split-lines)
(map #(map { \V "Violets" \R "Radishes" \C "Clover" \G "Grass" } %))
(map #(zipmap (sort names) (partition 2 %)))
(apply merge-with concat)))
</pre>
</div>
</div>
<div>
Results:<br />
<div>
<pre class="brush: clj">(def ch ["Alice" "Bob" "Charlie" "David" "Eve" "Fred" "Ginny" "Harriet" "Ileana" "Joseph" "Kincaid" "Larry"])
(def rows "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV")
(prn ((kinder-thread-last ch rows) "David"))
;prints ("Radishes" "Violets" "Clover" "Radishes")
</pre>
</div>
</div>
<div>
Commentary:<br />
<ul>
<li>I provided two solutions. My first did not use threading macros. I think the thread last version (#2) is much easier to read. It is quite easy to see the sequence of steps being followed.</li>
<li>Another important thing to note is how I mapped the characters in the gardens to plants. I can use a case statement like what is done in the char->plant function. However, a simpler and more concise solution is to simply use a map. Clojure maps (and vectors and sets) are not just data structures, but are also functions. This may seem weird at first, but it actually makes sense. A map (in any language) is simply a discrete 1:1 function that maps keys to values. A vector is a function of its indices and a set is a function of its contents. Using this knowledge, I just mapped the characters in the strings using the map of characters to plant names as an inlined function.</li>
</ul>
</div>
<div>
<div>
<b>Problem 3: Treasure Hunt</b></div>
<div>
In the treasure hunt problem described <a href="http://users.csc.calpoly.edu/%7Ejdalbey/103/Projects/ProgrammingPractice.html">here</a> you are given a 2D array of two digit map coordinates. From a starting coordinate that I am assuming comes from a pirate, you look up each successive coordinate on the map by using the first and second digits of the current coordinate as the row and column on the map. Once a coordinate maps to itself, start digging, you are done.<br />
<br />
Here is my solution:</div>
</div>
<div>
<div>
<pre class="brush: clj">(defn map-step[m clue]
(let [n (get-in m [(dec (quot clue 10)) (dec (rem clue 10))])]
(when (not= n clue) n)))
(defn treasure-hunt [m start]
(take-while identity (iterate (partial map-step m) start)))
</pre>
</div>
Here it is in action:
<br />
<div>
<pre class="brush: clj">(def treasure-map [[34 21 32 41 25]
[14 42 43 14 31]
[54 45 52 42 23]
[33 15 51 31 35]
[21 52 33 13 23]])
(prn (treasure-hunt treasure-map 11))
;prints (11 34 42 15 25 31 54 13 32 45 35 23 43 51 21 14 41 33 52)
</pre>
</div>
Commentary:<br />
<ul>
<li>The map-step function simply computes the next step in the map. What's beautiful about this method is that it very naturally uses the Clojure <a href="https://clojuredocs.org/clojure.core/get-in">get-in</a> function to solve the problem. get-in is a Clojure function that allows you to drill down into a nested data structure by providing a vector of successive indices. Conveniently, our two-digit coordinates can be split into a vector for get-in access. Note that I do need to decrement the coordinates to make them 0 indexed.</li>
<li>The only additional logic required was the exit criteria. I compared the looked up coordinate to the input. If they are different, I return the update. Otherwise, nil is returned.</li>
<li>The treasure-hunt is where the real awesomeness happens. I can create a <a href="https://clojuredocs.org/clojure.core/partial">partial</a> map-step function by filling in the first argument of the function with a treasure map. I now have a function that takes an [x y] coordinate and returns an [x y] coordinate. I want to repeatedly do this until I return nil. Sounds like a perfect candidate for Clojure's <a href="https://clojuredocs.org/clojure.core/iterate">iterate</a> function. iterate takes a function and an initial condition and repeatedly returns the result of the applied function and reapplies that result to itself. To determine a stopping condition, I take results while the function satisfies the <a href="https://clojuredocs.org/clojure.core/identity">identity</a> function. Identity is a clever method that simply returns what is passed to it. In Clojure, anything not false or nil will pass logical truth tests (this is called truthiness), so as long as my function returns new coordinates I will take coordinates from it.</li>
<li>It is quite common in Clojure to store your state in a structure and do a computation that returns a new state that is similarly structured to the original. Whenever you do this, iterate may be a great option. You can take a certain number of items from the iterated function or continue until a stop condition is met.</li>
<li>Finally, this problem really is one of data navigation, and that is something Clojure excels at. Clojure is all about nested heterogeneous data structures and has many methods to safely inspect, update, and transform them. Furthermore, Clojure's semantics for truthiness and handling of nil makes what would normally be exceptional behavior simple to handle.</li>
</ul>
</div>
<div>
<div>
<b>Problem 4: Queen Attack</b></div>
<div>
The basic problem is this: Given two queens on a chessboard, are they in an "attack" configuration. In other words, are they in a shared column, row, or diagonal. Here's a <a href="http://garajeando.blogspot.com/2015/05/exercism-queen-attack-in-clojure.html">solution</a> from my original browsing exercise. As presented <a href="http://users.csc.calpoly.edu/%7Ejdalbey/103/Projects/ProgrammingPractice.html">here</a>, the problem is to encode an 8x8 board using different characters. I took a different approach and simply used the coordinates of the pieces to determine if they were visible to each other.<br />
<br />
Here is my complete solution:</div>
<div>
<pre class="brush: clj">(defn queen-attack?[w b]
(let [diff (map - w b)
zs ((juxt first second #(reduce - %) #(reduce + %)) diff)]
(true? (some zero? zs))))
</pre>
</div>
</div>
<div>
<div>
Here are a few invocations of the function with results in comments:<br />
<div>
<pre class="brush: clj">(prn (queen-attack? [2 3] [2 10])) ;true - same row
(prn (queen-attack? [9 10] [2 10])) ;true - same column
(prn (queen-attack? [4 3] [2 10])) ;false
(prn (queen-attack? [7 12] [5 10])) ;true - same diagonal
</pre>
</div>
Commentary:</div>
<div>
<ul>
<li>The most important thing to note in this solution is that being on the same row, column, or diagonal reduces to simple difference checks on the piece indices. A zero difference in the row or column means you are on the same row or column. If the sum or difference of the row and column change is 0 you are on the same diagonal. If this last rule doesn't make sense, stop for a second and think about it. To move diagonally, you must move the same amount up or down as you do left or right. So, the sum or difference (depending on direction) will be 0.</li>
<ul>
</ul>
<li>I use <a href="https://clojuredocs.org/clojure.core/juxt">juxt</a> to compute the 4 potential zeroes. I also discuss juxt at length in this <a href="http://fn-code.blogspot.com/2015/03/using-juxt-to-compute-signed-angular.html">post</a>.</li>
<li><a href="https://clojuredocs.org/clojure.core/some">some</a> <a href="https://clojuredocs.org/clojure.core/zero_q">zero?</a> checks if any zeroes exist. If any exist, the result is true. It none exist the result is nil. Therefore, I call <a href="https://clojuredocs.org/clojure.core/true_q">true?</a> to covert a nil to false. Note that Clojure uses "truthiness" when doing logic checks (i.e. nil and false return false, everything else returns true). So, I could just as easily drop the true? check if I felt like it.</li>
<li>Probably the biggest takeaway from this problem is the difference between a standard OOP solution vs. an FP solution. In OOP the natural approach to the problem would be to start with a nice class hierarchy such as a Board (which would contain 64 Squares). You would probably have a ChessPiece class which you'd extend into Pawn, Rook, Knight, Bishop, Queen, and King classes. At this point you'd probably determine some basic methods such as move and attack which would allow you to finally approach solving the problem. In FP, you generally think directly in terms of what you are trying to do since your domain model is just data. Had I been asked to solve the problem for all chess pieces in Clojure, I'd probably just create a <a href="https://clojuredocs.org/clojure.core/defmulti">multimethod</a> that dispatches on the attacker type (:pawn, :rook, :knight, :bishop, :queen, or :king). Each resulting <a href="https://clojuredocs.org/clojure.core/defmethod">defmethod</a> would be as concise as the above function.</li>
</ul>
<div>
<b>Conclusion</b></div>
<div>
In a few short lines, I was able to solve 4 programming puzzlers using Clojure. While none of the problems were particularly hard, each illustrated several interesting functions and aspects of Clojure programming that I thought were worth pointing out. In particular, Clojure excels at representing, manipulating, and navigating data as simple structures. I find it much easier to model the data directly using Clojure's data structures than developing elaborate class hierarchies for each problem. This way of thinking allows you to jump right into solving your problem without having to spend a lot of time figuring out how to represent the data.</div>
<br />
If you liked this page or learned anything from it, please tweet the link,follow me on Twitter, and/or follow this blog. Your support and interest is the fuel that drives the effort.<br />
<a class="twitter-share-button" data-url="http://fn-code.blogspot.com/2015/05/exercises-in-clojure-with-commentary.html" data-via="mark_bastian" href="https://twitter.com/share">Tweet</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<a class="twitter-follow-button" data-show-count="false" href="https://twitter.com/mark_bastian">Follow @mark_bastian</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<a class="twitter-hashtag-button" data-related="mark_bastian" href="https://twitter.com/intent/tweet?button_hashtag=clojure">Tweet #clojure</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<br />
<div style="clear: both;">
</div>
<ul><ul>
</ul>
</ul>
</div>
</div>
<script src="http://markbastian.github.io/practice/js/cljpractice.js"></script><br />
<script id="hiddenlpsubmitdiv" style="display: none;"></script><script>try{for(var lastpass_iter=0; lastpass_iter < document.forms.length; lastpass_iter++){ var lastpass_f = document.forms[lastpass_iter]; if(typeof(lastpass_f.lpsubmitorig2)=="undefined"){ lastpass_f.lpsubmitorig2 = lastpass_f.submit; if (typeof(lastpass_f.lpsubmitorig2)=='object'){ continue;}lastpass_f.submit = function(){ var form=this; var customEvent = document.createEvent("Event"); customEvent.initEvent("lpCustomEvent", true, true); var d = document.getElementById("hiddenlpsubmitdiv"); if (d) {for(var i = 0; i < document.forms.length; i++){ if(document.forms[i]==form){ if (typeof(d.innerText) != 'undefined') { d.innerText=i; } else { d.textContent=i; } } } d.dispatchEvent(customEvent); }form.lpsubmitorig2(); } } }}catch(e){}</script><br />
<script id="hiddenlpsubmitdiv" style="display: none;"></script><script>try{for(var lastpass_iter=0; lastpass_iter < document.forms.length; lastpass_iter++){ var lastpass_f = document.forms[lastpass_iter]; if(typeof(lastpass_f.lpsubmitorig2)=="undefined"){ lastpass_f.lpsubmitorig2 = lastpass_f.submit; if (typeof(lastpass_f.lpsubmitorig2)=='object'){ continue;}lastpass_f.submit = function(){ var form=this; var customEvent = document.createEvent("Event"); customEvent.initEvent("lpCustomEvent", true, true); var d = document.getElementById("hiddenlpsubmitdiv"); if (d) {for(var i = 0; i < document.forms.length; i++){ if(document.forms[i]==form){ if (typeof(d.innerText) != 'undefined') { d.innerText=i; } else { d.textContent=i; } } } d.dispatchEvent(customEvent); }form.lpsubmitorig2(); } } }}catch(e){}</script><br />
<script id="hiddenlpsubmitdiv" style="display: none;"></script><script>try{for(var lastpass_iter=0; lastpass_iter < document.forms.length; lastpass_iter++){ var lastpass_f = document.forms[lastpass_iter]; if(typeof(lastpass_f.lpsubmitorig2)=="undefined"){ lastpass_f.lpsubmitorig2 = lastpass_f.submit; if (typeof(lastpass_f.lpsubmitorig2)=='object'){ continue;}lastpass_f.submit = function(){ var form=this; var customEvent = document.createEvent("Event"); customEvent.initEvent("lpCustomEvent", true, true); var d = document.getElementById("hiddenlpsubmitdiv"); if (d) {for(var i = 0; i < document.forms.length; i++){ if(document.forms[i]==form){ if (typeof(d.innerText) != 'undefined') { d.innerText=i; } else { d.textContent=i; } } } d.dispatchEvent(customEvent); }form.lpsubmitorig2(); } } }}catch(e){}</script>Mark Bastianhttp://www.blogger.com/profile/17120391255299566028noreply@blogger.com0tag:blogger.com,1999:blog-5558410860908654317.post-18847349504687385602015-05-15T08:49:00.000-07:002015-05-15T08:49:06.586-07:00FP from the Fire Hose<div class="separator" style="clear: both; text-align: left;">
Earlier this year I gave a presentation at work entitled "Functional Programming (FP) from the Fire Hose." My goal was to present, to a mostly OOP audience, some of the basics of FP while also providing examples that show that FP isn't just academic. You can't sell people on FP with concepts alone. People's eyes just glaze over and roll back into their heads when you start talking functional concepts like first class and higher order functions, immutability, statelessness, referential transparency, homoiconicity, and the like (I won't even say the M-word). With that in mind, I tried to go fast on FP and show several clear examples that demonstrate FP in action, even at a very small scale.</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
In my presentation, I do (or attempt to do) the following:</div>
<div class="separator" style="clear: both; text-align: left;">
</div>
<ol>
<li>Explain FP in 5 minutes to an OOP audience</li>
<li>Explain why functions are important using collections as the context.</li>
<li>Explain that first class functions are a necessary but not sufficient condition for FP. You also must have immutability.</li>
<li>Solve a small problem in both FP and OOP style. The examples, written in Clojure and Java, respectively, show the simplicity and power of FP vs. traditional OOP.</li>
<li>Demonstrate how you can write functional code in a stateful application using Clojure concurrency primitives.</li>
</ol>
<div>
Here's the video and links to additional resources. I hope you enjoy.</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<iframe allowfullscreen="" class="YOUTUBE-iframe-video" data-thumbnail-src="https://i.ytimg.com/vi/Ie2dinyRDu0/0.jpg" frameborder="0" height="380" src="https://www.youtube.com/embed/Ie2dinyRDu0?feature=player_embedded" width="480"></iframe></div>
<br />
The slides are <a href="http://markbastian.github.io/fp-firehose/fp-firehose.html#/">here</a>, and the accompanying repo and demo are <a href="https://github.com/markbastian/fp-firehose">here</a>.<br />
<br />
If you liked this page or learned anything from it, please tweet the link,follow me on Twitter, and/or follow this blog. Your support and interest is the fuel that drives the effort.<br />
<a class="twitter-share-button" data-url="http://fn-code.blogspot.com/2015/05/fp-from-fire-hose.html" data-via="mark_bastian" href="https://twitter.com/share">Tweet</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<a class="twitter-follow-button" data-show-count="false" href="https://twitter.com/mark_bastian">Follow @mark_bastian</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<a class="twitter-hashtag-button" data-related="mark_bastian" href="https://twitter.com/intent/tweet?button_hashtag=clojure">Tweet #clojure</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<br />
<div style="clear: both;">
</div>
Mark Bastianhttp://www.blogger.com/profile/17120391255299566028noreply@blogger.com2tag:blogger.com,1999:blog-5558410860908654317.post-66923426151460625562015-04-11T00:27:00.001-07:002015-04-25T07:26:49.513-07:00A-maze-ing Mazes with ClojureMazes can be pretty fun. Children love them. Add a dungeon theme (e.g. monsters, loot, and a dragon boss) and most computer programming adults like them, too.<br />
<br />
In this post, I'll show you how to make some mazes in Clojure/ClojureScript. Feel free to print some off for your next trip with your kids or adapt this code to your next campaign if you are a DM.<br />
<br />
The techniques I'll be covering are Prim's algorithm and Depth-First Search with Backtracking. Afterwords, I'll show you the code and discuss some interesting points.<br />
<br />
Here are the algorithms:<br />
<br />
<b>Prim's Algorithm</b><br />
This algorithm will produce mazes with a fair amount of branching and not a lot of long corridors. Here are the steps in the algorithm:<br />
<ol>
<li>Mark all cells in your maze as undiscovered.</li>
<li>Select a start cell and mark it as visited.</li>
<li>Mark all neighbors of the start cell as frontier cells.</li>
<li>Randomly select a cell from the frontier as your next cell.</li>
<li>Add it to the maze.</li>
<li>Add any neighbors of the selected cell that are undiscovered to the frontier.</li>
<li>Connect the selected cell to the maze by randomly selecting an adjacent cell that is already in the maze and mark the two cells as connected.</li>
<li>If the frontier is not empty, goto 4. Otherwise, your maze is complete.</li>
<li>You can optionally mark a cell as the end, but this is just a convenience for knowing when you can leave the dungeon. The maze is fully connected so the endpoint is arbitrary.</li>
</ol>
<div>
<b>Depth-First Search with Backtracking</b></div>
<div>
DFS will produce longer corridors since it continues to move forward until it is blocked. Here are the steps in the algorithm:</div>
<ol>
<li>Select a cell, mark it as in the maze, and put it on the path stack.</li>
<li>Randomly select an unvisited neighbor of the current cell, connect it to the previous cell, and put the new cell on the path stack. Since we are always stepping forward to a different cell we are going to make longer paths. This is the "Depth-First" part of the algorithm.</li>
<li>If the current cell has unvisited neighbors, goto 2. Otherwise, pop the top off the path stack, make the new top of the stack the current cell, and goto 2 (This is the "backtracking" part of the algorithm). Repeat until every cell has been visited.</li>
<li>As with Prim's algorithm, you can choose an exit if desired, but this is arbitrary.</li>
</ol>
You can read more about these <a href="http://en.wikipedia.org/wiki/Maze_generation_algorithm">here</a> and <a href="http://weblog.jamisbuck.org/2011/2/7/maze-generation-algorithm-recap.html">here</a>.<br />
<br />
Without further ado, here are mazes generated using these algorithms. There is a reset button below the second maze to recreate them if you want.<br />
<br />
<div class="container">
<b>Maze generated using Prim's algorithm</b><br />
<canvas height="400" id="primCanvas" style="border: 1px solid rgb(0, 0, 0);" width="400"></canvas></div>
<div class="container">
<b><br /></b>
<b>Maze generated using Depth-First Search with Backtracking</b><br />
<canvas height="400" id="dfbCanvas" style="border: 1px solid #000000;" width="400"></canvas>
<br />
<div>
<button id="regenMazes" type="button">Reset</button>
</div>
<br />
<b>The Source</b><br />
Here is the complete implementation of the maze generators. Less than 50 lines! Despite being such a small namespace, it does a lot. Continue on past the code to see my comments and observations regarding this exercise.<br />
<br />
Note that I did not include the source for rendering the mazes onto the HTML canvas. You can see that as well as a standalone Java Swing version of the code by checking out the complete project <a href="https://github.com/markbastian/mazegen">here</a>.<br />
<br />
<pre class="brush: clj; ruler: false">(ns mazegen.rules)
(defn create-empty
"Create an empty rectangular maze."
[rows cols]
(vec (take rows (repeat (vec (take cols (repeat #{})))))))
(defn neighbors
"Compute the neighbors of a given coordinate."
[maze [i j]]
(->> (map vector
((juxt inc identity dec identity) i)
((juxt identity inc identity dec) j))
(filter #(get-in maze %))))
(defn open-wall
"Create pathways between the src and dst coords of the maze."
[maze src dst]
(-> maze
(update-in src conj dst)
(update-in dst conj src)))
(defn prim-gen
"Create a maze using Prim's method."
[empty-maze start end]
(loop [maze (update-in empty-maze start conj :start)
frontier (into #{} (neighbors maze start))]
(if (empty? frontier)
(update-in maze end conj :end)
(let [dst (rand-nth (vec frontier))
n (neighbors maze dst)
{ f true s false } (group-by #(empty? (get-in maze %)) n)]
(recur (open-wall maze (rand-nth s) dst)
(into (disj frontier dst) f))))))
(defn depth-first-gen
"Create a maze using a depth-first recursive search with backtracking."
[empty-maze start end]
(loop [maze (update-in empty-maze start conj :start)
visited [start]]
(if (empty? visited)
(update-in maze end conj :end)
(let [n (neighbors maze (first visited))
f (filter #(empty? (get-in maze %)) n)]
(if (empty? f)
(recur maze (rest visited))
(let [dst (rand-nth (vec f))]
(recur (open-wall maze (first visited) dst)
(conj visited dst))))))))
</pre>
<br />
<b>Comments, Observations, Lessons Learned</b><br />
Here are some points that you might find interesting from this exercise:<br />
<ul>
<li>The Data Model: In Java you would likely have a Maze class with Cells or some similar type hierarchy. In Clojure, you generally think about the data and model your domain in terms of simple data structures (vectors, maps, and sets). As the program grows, the data can flexibly grow with it. I modeled the maze as a 2D vector of sets. Each set represents a cell that contains links to other cells as well as any other data that I want to throw into it. In this case, I add :start and :end keywords to cells to represent entry and exit points. Had I used an object hierarchy, I would have to have made some decisions<i> a priori</i> regarding what can and can't go into a Cell. Would a cell have a start, be a start, or something else? Suppose I wanted to add a dragon at the exit that I had to fight to complete the maze. In Object Land, I now have to go back and redesign my type hierarchy to accommodate such a thing. In Clojure, I simply add a :dragon to the current cell set. If I decide I want something more hierarchical, I could easily change my sets in each cell to a map, or even add a map entry to the set (Maybe the dragon isn't just a keyword - Perhaps I also need to store its hit points or other attriubutes). Alternatively, I could have represented the maze as a map of [x y] coordinates as keys and another nested data structure as values.</li>
<li>The neighbors function has some awesomeness that needs some explaining. First, see that I am using the <b>juxt</b> function (as explained <a href="http://fn-code.blogspot.com/2015/03/using-juxt-to-compute-signed-angular.html">here</a> and <a href="http://fn-code.blogspot.com/2015/03/conways-game-of-life-demonstration-and.html">here</a>) to determine my neighbor's coordinates. However, I may get coordinates like [-1 0] that are off the maze (the maze has minimum coords of [0 0] and maximum of [(dec dim) (dec dim)]). Rather than checking if the coordinate is in the grid or not, I attempt to access it using the <b>get-in</b> function. The great thing about get-in is that it allows you to safely drill down into nested data structures. If the element is missing or the path you are navigating is bad, it returns nil (You can also provide an optional default value). Contrast this to Java or Scala, where you must be very careful about navigating into nested data structures (e.g. maps of maps). One wrong get and you will get a NullPointerException. The <b>filter</b> method is called on the results of calling get-in over all of the coordinates in the maze. Filter will drop all of the nils that are returned, so anything off the maze goes away.</li>
<li>The neighbors and open-wall methods make use of threading macros (-> is thread first) and (->> is thread last). These deserve a post of their own, but the short explanation is that they take a value and feed it through a succession of functions. As each function is called, the result is passed to the next function. This allows you to create processing pipelines. The first vs. last distinction has to do with where the thing being threaded goes in the next function (first or last position). As I said, this is worthy of its own post and I won't go into a ton of detail here.</li>
<li>Finally, we don't goto in Clojure. Any self-respecting language would use recursion instead. Clojure has a pattern for recursion called loop-recur. Loop is the first line of the pattern and initializes a set of items to be iterated upon. This is typically followed by an if or other branching statement that has a stop condition and a recursion condition. If the stop condition is met, you return. If the recur condition is met, you update the items being iterated upon. In my examples, I used recur to track the maze, the active cell, the frontier, and similar items. One important point about loop-recur is that recur must be in the tail position of the calculation (the last thing being calculated before looping). By doing so, the construct is transformed under the covers so that the stack does not grow and you are just doing a simple loop.</li>
</ul>
<div>
There is more that can be gleaned from this short application, but I think this is good for now. It is easy to see, however, that Clojure is a very powerful and expressive language.</div>
<div>
<br /></div>
<div>
<b>Conclusion</b></div>
<div>
Mazes are an interesting problem that can demonstrate a lot of features of functional programming. In this case, the functional language of choice was Clojure. Clojure's powerful data modeling aspects combined with its expressiveness made it easy to create a flexible and concise solution to this problem.</div>
<div>
<ul>
</ul>
</div>
If you liked this page or learned anything from it, please tweet the link and/or follow me on Twitter. Your support and interest is the fuel that drives the effort.<br />
<a class="twitter-share-button" data-url="http://fn-code.blogspot.com/2015/04/a-maze-ing-mazes-with-clojure.html" data-via="mark_bastian" href="https://twitter.com/share">Tweet</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<a class="twitter-follow-button" data-show-count="false" href="https://twitter.com/mark_bastian">Follow @mark_bastian</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<a class="twitter-hashtag-button" data-related="mark_bastian" href="https://twitter.com/intent/tweet?button_hashtag=clojure">Tweet #clojure</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<br />
<div style="clear: both;">
</div>
<br /></div>
<script src="http://markbastian.github.io/mazegen/js/mazegen.js"></script>Mark Bastianhttp://www.blogger.com/profile/17120391255299566028noreply@blogger.com3tag:blogger.com,1999:blog-5558410860908654317.post-38938012399359545402015-03-31T21:32:00.000-07:002015-03-31T22:39:15.327-07:00Using juxt to Compute Signed Angular Distances<span style="font-family: Arial, Helvetica, sans-serif;">Juxt is one of those weird Clojure functions that just doesn't make sense until you see it in action. However, once you get it, you love it. This short example shows a great use of the juxt function.</span><br />
<div>
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">What does juxt do? It creates a function that is the application of a list of functions to a single argument. In simpler terms, a typical function returns one result for one input. In the case of juxt, you create a function that returns n results for one input. The n results are computed from the n functions that you juxtapose.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">For example, suppose you need both the cosine and sine x for a given application. You could create two separate variables, like so:</span><br />
<div>
<pre class="brush: clj">(defn angle->cartesian
"Convert an angle (in radians) to its cartesian vector
equivalent."
[theta]
(let[x (Math/cos theta)
y (Math/sin theta)]
[x y]))
</pre>
</div>
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">However, it is more convenient to juxtapose (set side by side) the two items being computed into a single vector.</span></div>
<div>
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">Here's a problem that builds on the above to show how nice juxt is:</span></div>
<div>
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">You are given two angles in degrees and need to compute the signed distance between them. These angles aren't constrained to [0, 360). If the angles are outside of this range, we want to use the equivalent angle within the desired range. Here are a few examples of the desired output:</span><br />
<br /></div>
<table style="border-collapse: collapse; border-color: #ccc; border-spacing: 0;"><tbody>
<tr><th style="background-color: #f0f0f0; border-color: #ccc; border-style: solid; border-width: 1px; color: #333333; font-family: Arial, sans-serif; font-size: 14px; font-weight: normal; overflow: hidden; padding: 10px 5px; text-align: center; word-break: normal;">First Angle</th><th style="background-color: #f0f0f0; border-color: #ccc; border-style: solid; border-width: 1px; color: #333333; font-family: Arial, sans-serif; font-size: 14px; font-weight: normal; overflow: hidden; padding: 10px 5px; word-break: normal;">Second Angle</th><th style="background-color: #f0f0f0; border-color: #ccc; border-style: solid; border-width: 1px; color: #333333; font-family: Arial, sans-serif; font-size: 14px; font-weight: normal; overflow: hidden; padding: 10px 5px; word-break: normal;">Signed Distance</th></tr>
<tr><td style="background-color: white; border-color: #ccc; border-style: solid; border-width: 1px; color: #333333; font-family: Arial, sans-serif; font-size: 14px; overflow: hidden; padding: 10px 5px; text-align: center; word-break: normal;">10</td><td style="background-color: white; border-color: #ccc; border-style: solid; border-width: 1px; color: #333333; font-family: Arial, sans-serif; font-size: 14px; overflow: hidden; padding: 10px 5px; text-align: center; word-break: normal;">20</td><td style="background-color: white; border-color: #ccc; border-style: solid; border-width: 1px; color: #333333; font-family: Arial, sans-serif; font-size: 14px; overflow: hidden; padding: 10px 5px; text-align: center; word-break: normal;">10</td></tr>
<tr><td style="background-color: white; border-color: #ccc; border-style: solid; border-width: 1px; color: #333333; font-family: Arial, sans-serif; font-size: 14px; overflow: hidden; padding: 10px 5px; text-align: center; word-break: normal;">20</td><td style="background-color: white; border-color: #ccc; border-style: solid; border-width: 1px; color: #333333; font-family: Arial, sans-serif; font-size: 14px; overflow: hidden; padding: 10px 5px; text-align: center; word-break: normal;">10</td><td style="background-color: white; border-color: #ccc; border-style: solid; border-width: 1px; color: #333333; font-family: Arial, sans-serif; font-size: 14px; overflow: hidden; padding: 10px 5px; text-align: center; word-break: normal;">-10</td></tr>
<tr><td style="background-color: white; border-color: #ccc; border-style: solid; border-width: 1px; color: #333333; font-family: Arial, sans-serif; font-size: 14px; overflow: hidden; padding: 10px 5px; text-align: center; word-break: normal;">350</td><td style="background-color: white; border-color: #ccc; border-style: solid; border-width: 1px; color: #333333; font-family: Arial, sans-serif; font-size: 14px; overflow: hidden; padding: 10px 5px; text-align: center; word-break: normal;">10</td><td style="background-color: white; border-color: #ccc; border-style: solid; border-width: 1px; color: #333333; font-family: Arial, sans-serif; font-size: 14px; overflow: hidden; padding: 10px 5px; text-align: center; word-break: normal;">20</td></tr>
<tr><td style="background-color: white; border-color: #ccc; border-style: solid; border-width: 1px; color: #333333; font-family: Arial, sans-serif; font-size: 14px; overflow: hidden; padding: 10px 5px; text-align: center; word-break: normal;">10</td><td style="background-color: white; border-color: #ccc; border-style: solid; border-width: 1px; color: #333333; font-family: Arial, sans-serif; font-size: 14px; overflow: hidden; padding: 10px 5px; text-align: center; word-break: normal;">350</td><td style="background-color: white; border-color: #ccc; border-style: solid; border-width: 1px; color: #333333; font-family: Arial, sans-serif; font-size: 14px; overflow: hidden; padding: 10px 5px; text-align: center; word-break: normal;">-20</td></tr>
<tr><td style="background-color: white; border-color: #ccc; border-style: solid; border-width: 1px; color: #333333; font-family: Arial, sans-serif; font-size: 14px; overflow: hidden; padding: 10px 5px; text-align: center; word-break: normal;">-350</td><td style="background-color: white; border-color: #ccc; border-style: solid; border-width: 1px; color: #333333; font-family: Arial, sans-serif; font-size: 14px; overflow: hidden; padding: 10px 5px; text-align: center; word-break: normal;">350</td><td style="background-color: white; border-color: #ccc; border-style: solid; border-width: 1px; color: #333333; font-family: Arial, sans-serif; font-size: 14px; overflow: hidden; padding: 10px 5px; text-align: center; word-break: normal;">-20</td></tr>
<tr><td style="background-color: white; border-color: #ccc; border-style: solid; border-width: 1px; color: #333333; font-family: Arial, sans-serif; font-size: 14px; overflow: hidden; padding: 10px 5px; text-align: center; word-break: normal;">350</td><td style="background-color: white; border-color: #ccc; border-style: solid; border-width: 1px; color: #333333; font-family: Arial, sans-serif; font-size: 14px; overflow: hidden; padding: 10px 5px; text-align: center; word-break: normal;">-350</td><td style="background-color: white; border-color: #ccc; border-style: solid; border-width: 1px; color: #333333; font-family: Arial, sans-serif; font-size: 14px; overflow: hidden; padding: 10px 5px; text-align: center; word-break: normal;">20</td></tr>
</tbody></table>
<div>
<br /></div>
<div>
<span style="font-family: Arial, Helvetica, sans-serif;">You can probably solve this problem with some combination of mod, rem, and/or quot, but I find it much easier to simply transform the angles to their cartesian vector form and do the math in that coordinate system. Recall that for unit vectors the dot product of vectors A and B is A*B=cos(<span style="color: #252525; line-height: 22px;">θ), where </span><span style="color: #252525;"><span style="line-height: 22px;">θ is the angle between the vectors. Likewise, </span></span>the sign of the cross product of the vectors indicates the the direction of rotation via the right hand rule. So, I can use dot to compute my angular offset and cross to determine the direction.</span></div>
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">With that knowledge, here's my function to compute angular distances:</span>
<br />
<div>
<pre class="brush: clj">(defn angular-delta
"Compute the difference between two angles,
where angles are in degrees."
[a b]
(let [[u v] (map (juxt #(Math/cos %) #(Math/sin %))
(map #(Math/toRadians %) [a b]))
theta (Math/acos (reduce + (map * u v)))
sign (Math/signum (reduce - (map * u (reverse v))))]
(Math/toDegrees (* sign theta))))
</pre>
</div>
<br />
<span style="font-family: Arial, Helvetica, sans-serif;">Pretty slick, eh? Notice how the combination of juxt and destructuring (the automagic mapping of the result to the [x y] vector) both shortened my code and created a single vector structure, which communicated my intent. As an afterthought, let me state that I am only converting to degrees since they are easier on the eyes for most humans. In the real world, I'd leave the function in terms of radians since, in the words of my favorite college math professor, "Degrees are for temperature, radians are for math."</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">Does this all give the right answer? Let the REPL decide:</span><br />
<div>
<pre class="brush: clj">(defn angular-delta
"Compute the difference between two angles,
where angles are in degrees."
[a b]
(let [[u v] (map (juxt #(Math/cos %) #(Math/sin %))
(map #(Math/toRadians %) [a b]))
theta (Math/acos (reduce + (map * u v)))
sign (Math/signum (reduce - (map * u (reverse v))))]
(Math/toDegrees (* sign theta))))
=> #'user/angular-delta
(angular-delta 10 20)
=> 10.000000000000012
(angular-delta 20 10)
=> -10.000000000000012
(angular-delta 350 10)
=> 20.00000000000001
(angular-delta 10 350)
=> -20.00000000000001
(angular-delta -350 350)
=> -20.00000000000001
(angular-delta 350 -350)
=> 20.00000000000001
</pre>
</div>
<br />
<span style="font-family: Arial, Helvetica, sans-serif;">Ignoring the precision issues, this gives just the right answer.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">I hope this provides a nice explanation of how you might use juxt in a real problem. There are many other similar cases where you need to compute several independent variations of an initial value to perform a computation. Whenever you need do to this, use juxt.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">If you liked this page or learned anything from it, please tweet the link and/or follow me on Twitter. Your support and interest is the fuel that drives the effort.</span><br />
<a class="twitter-share-button" data-url="http://fn-code.blogspot.com/2015/03/using-juxt-to-compute-signed-angular.html" data-via="mark_bastian" href="https://twitter.com/share">Tweet</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<a class="twitter-follow-button" data-show-count="false" href="https://twitter.com/mark_bastian">Follow @mark_bastian</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<a class="twitter-hashtag-button" data-related="mark_bastian" href="https://twitter.com/intent/tweet?button_hashtag=clojure">Tweet #clojure</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>Mark Bastianhttp://www.blogger.com/profile/17120391255299566028noreply@blogger.com0tag:blogger.com,1999:blog-5558410860908654317.post-90471181689996090382015-03-30T21:34:00.001-07:002015-04-07T22:19:38.184-07:00Conway's Game of Life - A Demonstration and Postmortem of a Clojure/ClojureScript Project<div>
<h2>
<span style="font-family: Arial, Helvetica, sans-serif; font-size: large;">The Project</span></h2>
<span style="font-family: Arial, Helvetica, sans-serif;">I wanted to create an interesting project in Clojure that had the following features:</span><br />
<ul>
<li><span style="font-family: Arial, Helvetica, sans-serif;">The project should be simple, but interesting</span></li>
<li><span style="font-family: Arial, Helvetica, sans-serif;">The project should demonstrate how Clojure, ClojureScript, and cljx interact</span></li>
<li><span style="font-family: Arial, Helvetica, sans-serif;">There should be some cool Clojure features to learn about</span></li>
<li><span style="font-family: Arial, Helvetica, sans-serif;">I wanted to demonstrate how to achieve state in a stateless world</span></li>
</ul>
<span style="font-family: Arial, Helvetica, sans-serif;">I chose to program up Conway's Game of Life, a simple zero-player cellular automaton. It's a fun little simulation
and meets all of my criterion. Here's what it looks like:</span><br />
<br /></div>
<div>
<canvas height="400" id="myCanvas" style="border: 1px solid #000000;" width="400"></canvas>
</div>
<div>
<button id="reset" type="button">Reset</button>
</div>
<div>
<br />
<h3>
<span style="font-family: Arial, Helvetica, sans-serif;">
The Rules</span></h3>
<ul>
<li><span style="font-family: Arial, Helvetica, sans-serif;">A living cell with only one or two neighbors dies</span></li>
</ul>
<ul>
<li><span style="font-family: Arial, Helvetica, sans-serif;">A living cell with two or three live neighbors survives</span></li>
</ul>
<ul>
<li><span style="font-family: Arial, Helvetica, sans-serif;">A living cell with greater than three neighbors dies</span></li>
</ul>
<ul>
<li><span style="font-family: Arial, Helvetica, sans-serif;">A dead cell with exactly three neighbors comes to life</span></li>
</ul>
<h2>
<span style="font-family: Arial, Helvetica, sans-serif; font-size: large;">
Postmortem</span></h2>
<h3>
<span style="font-family: Arial, Helvetica, sans-serif;">
Implementation in Clojure</span></h3>
<span style="font-family: Arial, Helvetica, sans-serif;">Conway's Game of Life is a cellular automaton in which a population of cells evolves with the
following rules:</span></div>
<div>
<span style="font-family: Arial, Helvetica, sans-serif;">This project was coded up in Clojure as an example of what can be done using the clj/cljs/cljx
integration. It was done in three main files: rules.cljx, swingui.clj, and canvasui.cljs. Below you will find
the complete listing of the rules and canvasui namespaces. the swingui namespace is available via git
<a href="https://github.com/markbastian/conway">here</a>.</span></div>
<div>
<span style="font-family: Arial, Helvetica, sans-serif;"><b><br /></b></span>
<span style="font-family: Arial, Helvetica, sans-serif;"><b>rules.cljx</b></span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">Here's a listing of the entire "rule engine" for the application.</span><br />
<pre class="brush: clj; ruler: false">(ns conway.rules)
(defn gen-cell[](if (> (Math/random) 0.7) :alive :dead))
(defn seed-grid [rows cols]
(vec (take rows (repeatedly
(fn [] (vec (take cols (repeatedly gen-cell))))))))
(defn neighbors [[i j]]
(let [x ((juxt inc inc identity dec dec dec identity inc) i)
y ((juxt identity inc inc inc identity dec dec dec) j)]
(map vector x y)))
(defn count-neighbors [grid coord]
(let [n (map #(get-in grid %) (neighbors coord))]
(count (filter #(= % :alive) n))))
(defn sim-step [grid coord]
(let [n-live (count-neighbors grid coord)]
(if (= :alive (get-in grid coord))
(case n-live
(2 3) :alive
:dead)
(if (= 3 n-live) :alive :dead))))
(defn step [grid]
(into [] (for [i (range (count grid))]
(into [] (for [j (range (count (get grid i)))]
(sim-step grid [i j]))))))
</pre>
<span style="font-family: Arial, Helvetica, sans-serif;">Perhaps the most interesting aspect of this project is the amount of code (and I mean the small amount) written
to do this complete implementation. I am continually amazed as I write more Clojure about the expressiveness
and conciseness of the language. Another thing to point out about this file is the cljx extension. By using this, I
can write code that cross-compiles to target both the JVM and the browser via JavaScript. Pretty cool, eh?</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">Finally, I wanted to point out my favorite function in this program: juxt. the Clojure juxt function creates a
function that performs a sequence of operations on a single item and returns that result as an indexed data
structure. This is a perfect function for computing the neighbors of a cell in a grid. This is way better than
having 8 sequential function calls in which you compute steps to the right, upper right, top center, upper left,
and so on.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;"><b>canvasui.cljx</b></span></div>
<div>
<span style="font-family: Arial, Helvetica, sans-serif;">And here is the complete listing for the canvas UI code.</span><br />
<pre class="brush: clj">(ns conway.canvasui
(:require [conway.rules :as rules]))
(defn draw-background [canvas ctx]
(doto ctx
(-> .-fillStyle (set! "#000000"))
(.fillRect 0 0 (.-width canvas) (.-height canvas))))
(defn draw-and-update-grid [canvas ctx grid dim]
(let [dx (/ (.-width canvas) dim)
dy (/ (.-height canvas) dim)]
(do
(draw-background canvas ctx)
(-> ctx .-fillStyle (set! "#00FF00"))
(doseq [i (range (count @grid))]
(doseq [j (range (count (get @grid i)))]
(when (= :alive (get-in @grid [i j]))
(.fillRect ctx (* dx i) (* dy j) dx dy))))
(swap! grid rules/step))))
(set!
(.-onload js/window)
(when (and js/document (.-getElementById js/document))
(let [cells 50
grid (atom (rules/seed-grid cells cells))
canvas (.getElementById js/document "myCanvas")
reset-button (.getElementById js/document "reset")
ctx (.getContext canvas "2d")]
(do
(js/setInterval
#(draw-and-update-grid canvas ctx grid cells) 10)
(set! (.-onclick reset-button)
#(reset! grid (rules/seed-grid cells cells)))))))
</pre>
<span style="font-family: Arial, Helvetica, sans-serif;">Again, it is amazing how few lines of code were written for this demo.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">Finally, take a look at how state is managed in the program. Notice that there are no defs (global variables
within the namespace) anywhere. All of the state is managed by a single atom (line 25) that is created within the
initialization section of the script and is passed to each function. The draw-and-update-grid function draws and
updates the grid (as you might expect) while the reset button is used to reset the grid. Since atoms are
synchronous, I don't need to worry about any sort of concurrency problems. It just works.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">This is a very common pattern in the Clojure world for designing applications. You have three elements:
</span><br />
<ol>
<li><span style="font-family: Arial, Helvetica, sans-serif;">A set of rules that can be thought of as an API or DSL for your project. These rules consist only of defns
or perhaps defs that define constants. They are used to transform or analyze a model. They are completely stateless.</span></li>
<li><span style="font-family: Arial, Helvetica, sans-serif;">A very small number of Clojure concurrency primitives that manage the actual state of the application. In
this case, there is only 1 - the grid atom. You might have other primitives, such as one for preferences, but
the number should be small and separated by concerns.</span></li>
<li><span style="font-family: Arial, Helvetica, sans-serif;">A user interface layer. It could be Swing, an HTML app using ClojureScript, or just a repl.</span></li>
</ol>
<span style="font-family: Arial, Helvetica, sans-serif;">Coming from a background in 10+ years of Java and then about 6 years of Scala, this
was something it took me a while to understand, but now I can't live without. When I got deep into Scala I really
loved it, but had the hardest time figuring out how to manage state. With Clojure everything is just baked in.</span></div>
<div>
<h2>
<span style="font-family: Arial, Helvetica, sans-serif; font-size: large;">
In Conclusion</span></h2>
<span style="font-family: Arial, Helvetica, sans-serif;">I was able to write up a simple cross-platform application in just a handful of lines of code using the amazing
powers of Clojure, ClojureScript, and the cljx project. Despite its simplicity, there are some great things to learn
from the project. If you want to see the entire project, check it out
<a href="https://github.com/markbastian/conway">here</a> or my entire github repo
<a href="https://github.com/markbastian">here</a>. I hope you enjoyed this project and short postmortem.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">If you liked this page or learned anything from it, please tweet the link and/or follow me on Twitter. I hope to
have other projects like this in the future, and your support and interest is the fuel that drives the effort.</span></div>
<a class="twitter-share-button" data-url="http://fn-code.blogspot.com/2015/03/conways-game-of-life-demonstration-and.html" data-via="mark_bastian" href="https://twitter.com/share">Tweet</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<a class="twitter-follow-button" data-show-count="false" href="https://twitter.com/mark_bastian">Follow @mark_bastian</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<a class="twitter-hashtag-button" data-related="mark_bastian" href="https://twitter.com/intent/tweet?button_hashtag=clojure">Tweet #clojure</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<script src="http://markbastian.github.io/conway/js/conway.js"></script>Mark Bastianhttp://www.blogger.com/profile/17120391255299566028noreply@blogger.com0