One of the nice things about MongoDB (particularly when using it in Clojure via the Congomongo library) is that its map-based query language is so amenable to the creation of a domain-specific language, or DSL. Creating and manipulating maps is like breathing in Clojure, so it is trivial to decompose the different query requirements of your application into a small collection of simple functions that can be used to create a rather fluent domain-specific language.
[Read More]
Writing Elegant Clojure Code Using Higher-Order Functions
Back when I first started writing Clojure code, I heard lots about the use of higher-order functions (also known as HOFs). Since functions are first-class language members in Clojure, you can do things like pass them as arguments or return them from function calls. Any function that accepts or produces another function in this way is a higher-order function. This allows you to write some very powerful and consise code, because you can capture the general form of a computation, while allowing its specific behavior to be determined at runtime by the user.
[Read More]
Github's new "Fork and Edit" Feature is Awesome
Github recently rolled out a new Fork and Edit feature that is pretty awesome. It basically allows you to create your own fork of any Github project, edit files in a new branch of that fork, and create a pull request back to the original project, all from your web browser, and in about as much time as it’s taken me to write this sentence. This really lowers the barrier for contributing code to other projects.
[Read More]
The Importance of MongoDB Key Names
Coming from a relational database background, you might not devote a lot of thought to the names of your columns. That is to say, while you make an effort to come up with a sensible and descriptive naming scheme for your columns, you probably don’t think about the amount of space those names take up. And why should you? In a relational databases, the column names are stored once, probably in a central metadata table.
[Read More]
Not-So-Private Clojure Functions
If you’ve been programming in Clojure for longer than, oh, about 5 minutes, you probably already know how defn creates a publicly accessible function in a namespace, while defn- creates a private one. If you’re outside the original namespace and you try to call a private function, you will get the smackdown.
Here’s a simple demonstration. We’ll create two functions, one public and one private, in the user namespace:
user> (defn hello [] "Hello World") #'user/hello user> (hello) "Hello World" user> (defn- secret [] "TOP SECRET") #'user/secret user> (secret) "TOP SECRET" If we switch to the other namespace, though, we can only use the public one:
[Read More]
Using YUI_config to Set Up Custom YUI Modules
Once you have been using the YUI JavaScript framework for a little while, you’ll inevitably need to write your own custom modules. For me, I started creating lots and lots of widgets and needed to figure out how to properly modularize everything. The documentation for creating modules using YUI.add is good, and it got me up and running quickly. So far, so good.
Armed with a handful of widgets in proper YUI modules, I needed a way to actually register them with my YUI instances in order to use them.
[Read More]
MongoDB Query Tricks, or "Why Don't MongoDB Doesn't Not have $and?"
Pardon the atrocious grammar; there is a point!
I recently had a tricky time formulating a particular query in MongoDB. As you probably know, MongoDB has a number of query operators to use. It’s got stuff like $in, $nin, $or, and others, but no $and. Normally, you don’t need something like $and, since the capability is there implicitly; you just list off all your conditions on your different document fields, and MongoDB finds all the documents that satisfy them all.
[Read More]
Using MapReduce in Congomongo Now
My patch to Congomongo that adds support for running MapReduce jobs in MongoDB was accepted, but that code hasn’t yet been pushed out to the official Congomongo SNAPSHOT jar in Clojars.
Until that happens, if you’d like to use the new MapReduce code, you can use my version.
If you’re using Leiningen, add this to your project.clj
:
[org.clojars.christophermaier/congomongo "0.1.3-SNAPSHOT"]
And here’s the dependency information for Maven:
<dependency>
<groupId>org.clojars.christophermaier</groupId>
<artifactId>congomongo</artifactId>
<version>0.1.3-SNAPSHOT</version>
</dependency>
Using Idiomatic Clojure, Part 1 - comp
As I read the Clojure code of others, I come across better ways to write my own code. Today’s example comes from The Joy of Clojure by Michael Fogus and Chris Houser.
I often find myself writing anonymous functions along the lines of
#(not (vector? %)) to act as filters in various places (filter, for, take-while, etc.). I always thought it looked a bit gnarly like that. Fortunately, there is a better way, using the comp function.
[Read More]
My iPhone Plays MP3s Again
I recently upgraded my old iPhone 3G to iOS 4.1; I’d heard the horror stories about the abysmal performance that iOS 4.0 had with the older phone, so I waited until Apple had the fix. Everything went well with the upgrade; I think my phone might actually be a little faster. But then I tried playing some music.
A large portion of my library just wouldn’t play. I’d select a song and the iPod app would try to play it for a second or two, and then it would move on to the next song.
[Read More]