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.
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 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 →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.
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.
There are a few ways to read files in Clojure.
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 →To make it easier for others to work on I've moved my most popular open source project to GitHub.
Already, I've had one pull request with some fixes to allow s3cp to run under Windows more easily.
See the project at https://github.com/bradlucas/s3cp
Continue reading →