The Founder Institute is Now In Accra!

Starting a business has more to it than just a name. Many factors make business successful, and you would rarely have all the knowledge to keep going. That’s one of the reasons why startup…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Compojure

Mastering Clojure Macros — by Colin Jones (34 / 46)

👈 Chapter 6 Build APIs That Say Just What They Mean | TOC | Clojure Koans 👉

Like most of James Reeves’ libraries, Compojure[22] inspires us to strive for elegance in our own work. If you’ve done any web work with Clojure, at some point you’ve probably used Compojure, on top of Ring,[23] to handle your HTTP routing. A typical app using Compojure starts out something like this:

Before we look at the implementations of the Compojure bits that we’re using, take a moment to consider how concise this code is. Beyond the opening ns form, we need only a few lines of code (plus a bit of wiring in the project.clj file) to launch a local web app with lein-ring.[24] We have a defroutes expression that defines a var that we can hand to the server infrastructure, a GET expression that defines a function to be executed upon a GET request to the root URL, and a not-found expression that defines the responder function for any request that didn’t match the other routes.

Now let’s see how Compojure makes things so nice:

That’s a pretty small macro, right? There’s not much code at all, and a lot of it is using name-with-attributes from tools.macro,[25] a handy tool that lets your custom def-like macros act more like what people are used to, allowing docstrings and metadata maps. And the only call that remains, routes (remember that the invocation of routes on the last line is resolved as compojure.core/routes due to the syntax-quote), is just a function. So defroutes is really just a nice layer of syntax to allow you to type this:

instead of the following:

Add a comment

Related posts:

How being different can make you a better designer

Having a diverse team of people is extremely important in a design and branding agency, but why? How will that impact the business, the work we do and the relationships we have with clients? Well I…

Creating Rays

Brace yourself for a fun challenge: build a photorealistic 3D renderer from scratch! It's easier than you think. In just a couple of weeks, build a ray-tracer that renders beautiful scenes with shadows, reflections, brilliant refraction effects, and subjects composed of various graphics primitives: spheres, cubes, cylinders, tri

Beginning of 2018

kita gak pernah tahu apa yang akan terjadi sama diri kita dari tahun ke tahun, hari ke hari, jam ke jam, menit ke menit bahkan sampai detik ke detik. kita cuma bisa beresolusi, melakukan ini itu…