background preloader

Web Development with Clojure

Facebook Twitter

Polymatheia - Understanding Clojure's Persistent Vector, pt. 1. You may or may not heard about Clojure's persistent vectors.

polymatheia - Understanding Clojure's Persistent Vector, pt. 1

It is a data structure invented by Rich Hickey (influenced by Phil Bagwell's paper on Ideal Hash Trees) for Clojure, which gives practically O(1) runtime for insert, update, lookups and subvec. As they are persistent, every modification creates a new vector instead of changing the old one. So, how do they work? I'll try to explain them through a series of blogposts, in which we look at manageable parts each time. It will be a detailed explanation, with all the different oddities around the implementation as well.

For today, we'll have a look at a naive first attempt, and will cover updates, insertion and popping (removal at the end). Note that this blogpost does not represent how PersistentVector isimplemented: There are some speed optimizations, solutions for transients and other details which we will cover later. The Basic Idea Mutable vectors and ArrayLists are generally just arrays which grows and shrinks when needed. Update. Web Development with Clojure. About this Book 232 pages Published: Release: P1.0 (2014-01-14) ISBN: 978-1-93778-564-2 Modern web development needs modern tools.

Web Development with Clojure

Web Development With Clojure shows you how to apply Clojure programming fundamentals to build real-world solutions. You’ll develop all the pieces of a full web application in this powerful language. If you already have some familiarity with Clojure, you’ll learn how to put it to serious practical use. You’ll learn the full process of web development using Clojure while getting hands-on experience with current tools, libraries, and best practices in the language. Throughout the book, you’ll develop key components of web applications, including multiple approaches to database access. This book is for anyone interested in taking the next step in web development. "Good Enough" error handling in Clojure. May 13, 2013 Writing Clojure is not like writing Java.

"Good Enough" error handling in Clojure

In Java, exceptions are an accepted part of the workflow; in Clojure, they are begrudgingly supported out of necessity, but generally avoided. Why is that? Probably because writing code that throws exceptions makes your functional code a lot less functional – that is, a lot less composable. When you can't trust a function to execute and return a value you lose some functional purity.

So what to do when we want to write code that might expect an error to occur? Let's say we have to perform the following (contrived) validations: :address is present and not empty :email contains something that looks like an email address :phone contains a phone number in the format (555) 555-5555 :state is one of a certain subset of US state codes.

A Path to Clojure (3 Mini Reviews) => Productively Distracted. As I have been working with Clojure more and more, I find myself taking the role of "Clojure Evangelist" with my friends and co-workers.

A Path to Clojure (3 Mini Reviews) => Productively Distracted

If I am able to convince them to give Clojure a try, the very next question is always: "Where do I get started? ". Unfortunately, for those coming from a Python/Ruby/Java type background, it's usually not enough for them to go try out a quick hello world tutorial. I think this is mainly because showing off the syntax will not sell anyone on the true power of Clojure and lisp in general. In fact if all you see is the syntax, you may be very turned off before you even get started. So before I am too far away from absolute beginner, I am writing down a path (certainly not the only path) to learning Clojure.

Functional Web Development with Clojure. Ring abstracts HTTP requests and responses into a simple API.

Functional Web Development with Clojure

First, the Ring adapter takes a request from your web server and turns it into a map of the request headers, URL, request type, body, and the like. This allows you to use different web servers - Jetty, Tomcat, JBoss, or whatever - and use the same interface for dealing with requests. It passes this request map on to middleware, if you have specified any. Middleware is another pass-through function that manipulates the request or response map in some way.

One example that you would often use is ring.middleware.params, which parses out the parameters from the query-string or the POST body and turns them into a map, which it puts into the request map. After middleware, the request map arrives at your application. Functional Web Development with Clojure. Ring abstracts HTTP requests and responses into a simple API.

Functional Web Development with Clojure

First, the Ring adapter takes a request from your web server and turns it into a map of the request headers, URL, request type, body, and the like. This allows you to use different web servers - Jetty, Tomcat, JBoss, or whatever - and use the same interface for dealing with requests. It passes this request map on to middleware, if you have specified any. Middleware is another pass-through function that manipulates the request or response map in some way. One example that you would often use is ring.middleware.params, which parses out the parameters from the query-string or the POST body and turns them into a map, which it puts into the request map. After middleware, the request map arrives at your application. Anatomy Of A Clojure Macro. One of the great things about Clojure is the fact that we can (relatively) easily extend the language at compile time using macros.

Anatomy Of A Clojure Macro

I’ve recently implemented (and written about) one such macro that creates a simple interface over node.js functions that require callbacks to continue the flow of execution. Using this macro as a basis I hope to walk through the entire process of creating a clojure macro from inception to implementation, and along the way cover everything that is required for you as a reader (and potentially being new to Clojure) to create a non-trivial macro of your own. I am writing this post because the macro I’ll be dissecting is the very first clojure macro I’ve ever written, and I hope it will be instructive to anyone wanting to write their own macro but does not know where to start. I’ll also cover a few pitfalls and helpful things that I found out along the way. However, before we start playing the game we need to first introduce the players. The Players. A little bit of Clojure. Reflections: Clojure: Symbols, Reading, and Execution. I wanted to read a function from a string, and apply an argument to it.

Reflections: Clojure: Symbols, Reading, and Execution

This seemingly simple task is surprisingly tricky in Lisp-like languages. The basic steps are straightforward: read the contents of a string, parse them as a function name, and apply the corresponding function to the desired arguments. Lisp-based languages are homoiconic, and also tend to have support for read-time evaluation. The combination makes safe parsing of data structures both powerful and dangerous. I tried to rely on my prior Common Lisp knowledge, but that turned out to be information I needed to unlearn. Of Symbols and Namespaces While reading, one has to deal with symbols, as they are invariably part of the input stream. ;; By default intern into the current package ? Clojure is different. The London Clojure Community:Clojure vs. Sca.