Brad Lucas

Programming, Clojure and other interests

Loading Data With Clojure

March 4, 2012

If you are coming to Clojure from Java you are probably familiar with JDBC. If not do a bit of research and you'll find that it is a layer that allows you to program against a database agnostic layer that communicates to your database through a database specific library. In the following example we'll use the clojure.java.jdbc wrapper for JDBC-based access to databases. This library is not part of what you would automatically have when you just install Clojure. For use to use it we'll have to download it ourselves or use a build tool to do it for us. To keep things simple here I'll assume you are using Leiningen as a build tool. If not you can find clojure.java.jdbc on GitHub. Also, you will have to have a database. Here I'll assume MySQL and explain how to connect to that. The example we'll build here will be an enhancement to the stock quote downloader presented in Reading Files article. Instead of writing the quotes to a file we'll load them into a database. You might want to review the article before continuing.

Database

Here we'll use MySQL but you could be using another. The only difference in the following will be the setting up of your database's JDBC library.

Install and Setup JDBC Driver


Continue reading →

Writing Files In Clojure

March 2, 2012

In the previous post we saw how to read files and then introduced a function to write them. The example presented downloaded stock quotes from Yahoo and then writes the data to a local file. Let's look a bit more into writing to files in Clojure.

Writing Short Strings

The opposite of slurp is spit. This is for writing short strings to files. You call the function with the name of the file and the string.

user=> (spit "testfile.txt" "This is my string")

Continue reading →

Yahoo Quote Downloader In Clojure

February 26, 2012

Yahoo provides downloadable historical quote data if you exercise a properly formatted URL. The data is returned in CSV format and is easily stored in a file. I wrote quote-downloader to accept stock symbols from the command line and request and store the data in symbol.csv files locally.

The program demonstrates reading from the command line, building a unique URL and reading from it as well as saving the results to a file and

The code is on github here https://github.com/bradlucas/quote-downloader


Continue reading →

Reading Files In Clojure – Part 2

February 22, 2012

In the previous part we saw how to read files and print them. This isn't terribly interesting. Typically, you are going to read files and then do some processing on the contents of the file and possibly save the results. Let's do that in a set of examples.

Functionally

Clojure is a functional language and you'll realize that as you learn the language and start writing functions. There is actually more to it than that but for now remember that you are building your program by creating and composing functions. In a purely functional language each function would not have a side effect. In other words the function would accept parameters and return a result without any change to the environment around it. As soon as we started talking about writing a function to process a file and do something with the contents we introduced the idea of a side effect. In our simple example previously we printed the lines. Next we'll process the lines and write the results to another file.

File Copy and Upper Case Example


Continue reading →

Reading Files In Clojure – Part 1

February 20, 2012

There are a few ways to read files in Clojure.

slurp

If you want to simply read a small file into a string then you can use slurp. This function works with local files as well as URLs.

An example:


Continue reading →