Archive for the ‘Clojure’ Category

Writing views in Noir with Clojure

I’ve been busily looking at the options, getting a feel for templating languages, scrapers and all around view generation in Noir. There are a lot of options out there, including everything Java ever did along with a series of clojure specific plugins and concepts.

So the conclusion?

Slim for Clojure wins out for me by a wide margin. Slim is similar to Haml in its syntax, white space sensitive and generally easy to read, while adding a few nice features and eliminating a few more characters from your HTML generation.

What does it look like? A little like this:

div
  ul
    li
      p Hello World

The Clojure implementation of Slim is developed by Chas Lemley, a Ruby software developer in the Atlanta area.

*Disclaimer: I have worked with both Andy Stone, the creator of Slim and Chas Lemley. Some bias may apply.

Getting the unmodified post body with Noir

Just a quick little post today about something that was more frustrating than you’d expect to google. Basically all I wanted was to get the entire post body without any modifications to it in any way, shape or form by any form of middle ware.

Sounds simple, and really it was very simple, once I found a random Google Groups post talking about it.

First thing is first, add this line to your view

(:require [noir.request :as request])

Then access the body within your defpage with the following code:

(let [request (request/ring-request)
body (slurp(:body request))]
(println body)
)

And there you go. The quick, easy, no noise to fight through explanation of how to get the entire post body in WebNoir.

Considering major projects

I’ve got a couple of big projects on my mind right now, and I’m curious about what the general feel is out there about some of the open source options I’m considering, so I thought I’d rattle them off and see if anyone had any thoughts.

#1 WordPress-like Clojure CMS

This project would be massive and I’d need to build up a strong community around it, but the idea is to take clj-blog and integrate it with more of a full-purpose-cms. This project would probably be based in Noir, use Korma and generally stick to the things I’ve already decided I’m comfortable with.

#2 Clojure migrations

This one should be pretty obvious to anyone used to the ActiveRecord style migrations. There is something to say about having all of that in a runnable little script.

#3 A generator library for Noir

Noir adopts a sinatra-style “Do what you feel like doing” attitude for the structure. I’ve been thinking about rolling up a lein plugin that would generate more opinionated Noir projects (probably including Korma by default, setting up an MVC style structure and integrating the Clojure Migrations project mentioned above). This is another big project, and one that would really use Noir as a base for a much more complex framework. I’m torn on this because while it IS nice to have everything in one place, rails has been getting far too opinionated lately.

#4 An implementation of Slim and/or Haml 

The Haml implemntations are hopelessly out dated (many not seeing updates over the last two or three years) and as far as I know no port of Slim has been done. This might be a cool project to improve an otherwise painful templating situation in Clojure.

And that’s all she wrote:

Let me know if anything there seems stupid, useful or otherwise worth commenting on. More importantly let me know if any of that exists and I just failed at researching it!

Clojure and QT for desktop applications

Up until now I have exclusively talked about web applications, and to a degree that stems from my mindset that the future of applications is the web. I stand by that…with caveats.

I’m embarking on a new personal project which requires a pure offline environment, something I haven’t done in any form in years and realistically have never done. Even in my past working with C# applications they always connected to remote databases maintained on networks and relied heavily on server storage. So for me, this will be my first time working on a stand alone desktop application.

Of course the Clojure comes in because I want to build something cross platform, targeting Mac OSx and Windows XP – 8…okay maybe not 8. I’m not sure yet.

At any rate if you’re not familiar with QT you should be. It’s a cross-platform UI framework maintained/developed by Nokia available through an LGPL license, making it a legitimate choice for commercial and open source uses. Read more

Simple form submission with Noir

One of the most basic things we do with interactive websites is submit data via the form tag. This method of input is so familiar to end users and programmers that we hardly think about it anymore. Naturally, one of the first questions I had as I started using Noir for web development was “How do I get data back from the user?”

For the most part it’s a very simple task thanks to form helpers in Hiccup (the HTML rendered that Noir houses underneath) that allow us to build what we need in much the same way we would with Ruby. Read more

Reading excel documents in Clojure

Let me take a minute to introduce you to a library called Incanter. Incanter is a wonderful little R-style statistical computing library written in Clojure. It is also the easiest way to read in data sets from Microsoft Excel documents in Clojure.

To use Incanter just include it in your project.clj (you are using lein, right) using:

[incanter "1.3.0"]

Then run lein deps in your console to install the dependency.

After that all you need to do to retrieve an excel file is use the following code.

(use ‘(incanter core io excel))

(defn get_excel (read-xls “/My/File/Location.xlsx”))

There you are. You now have the content of your excel file in a readable, manipulable list that you can use any way you please. This is both an easier and more pleasant experience than what I’ve seen with Excel documents in ruby, especially when you need to open both xls and xlsx documents.

Building libraries for public consumption

AKA: A temporary distraction from my time with Clojure.

This distraction is almost entirely based on my recent experience with Korma, the SQL DSL-of-choice for Clojure.

Korma itself is a great library, and I’m a huge fan. It’s solid dependable and well thought out, well, mostly. And there’s where my problem occurred.

My days are spent working with Oracle and SqlServer. Two technologies that are, by most measures, just unpleasant. They’re good at what they do and where they do it, but the level of standards compliance they support is about nil. So while working with Clojure and Korma I tried to do a limit on a query. You know the old “Select * from my_table LIMIT 20″. Read more

Clojure’s web stack

Disclaimer: This is by no means an exhaustive list nor should it be taken as one. This is what I chose, with a few brief reasons as to why.

Like most languages still trying to find its legs, Clojure web stacks are plentiful. Beyond the basics of Tomcat vs. JBoss (and others) you have to ask yourself should I go with Ring? What about Compojure? Maybe I want to try something more advanced an rails-like? There’s a lot to consider, and few clear winners. I’m going to declare a few here, but this is my own opinion and not necessarily the opinion of the Clojure using masses.

Before I get started naming winners I want to point out that the Clojure community is very young indeed. The language itself was written by Rick Hickey and made its first appearance in 2007 after almost three years of development. Five years is not a long time for a new language, particularly a lisp-style functional language, to build communities and toolsets.

That doesn’t mean the communtiy hasn’t been hard at work though. So let’s go bottom up.

Read more

Dependency Woes

I’ve got 99 problems but thankfully clojure dependencies aren’t one, at least not anymore.

Yesterday Clojure dependencies failed me completely, leaving me unable to load Noir, MySql or any other project dependencies using lein deps. Googling was nearly fruitless and the problem didn’t seem to be a global one. Special thanks to Kyle Gann for the solution to my problems.

rm -rf ~/.m2/repository/org/clojure/clojure/

And that’s all there is to it. Next time you run your lein commands it will retrieve Clojure and all of the dependencies you need and you’ll be good to go.

 

Clojure From a Rubyist Perspective

I like to preach language agnosticism, I believe in it with a religious fervor. But the truth is I do Ruby, I’ve done Ruby for a long time and I’m comfortable in it. It’s a structure I understand, I know the libraries the features and the communities.

So it’s time to kick myself in the ass and get on the Clojure train before it leaves the station, because I don’t want to be that guy who just knows Ruby.

Read more

Return top

About

That is not dead which can eternal lie. And with strange aeons even death may die.
- H.P. Lovecraft

Social Widgets powered by AB-WebLog.com.