Android or iPhone? Which platform should I target first? iPhone, for now, though it’s close. Android cannot (yet) provide the same level of developer tools or paying user base, the Android emulator can’t handle graphics, and I already own an iPhone. I do expect that Android’s competitive position will improve significantly over the next few years, but it’s not there yet. Continue Reading »
Popularity: 33% [?]
Posted in Tech |
5 Comments » | October 6th, 2010
Now that we have implemented Black-Scholes in Clojure, let’s make a Swing GUI for it. The Swing GUI will have text boxes for all the necessary inputs, and calculate prices and Greeks when the button is pressed. It’s a simple and straightforward way to get started in Swing GUI programming in Clojure. Here’s what it looks like. 
The GUI is written entirely in Clojure with the Swing toolkit. Calculation state is stored in a series of atoms. Watches are used to update the output table automatically when an atom changes, an idea from Kotka. I used the excellent MiG Layout for general layout functionality, and generic Swing widgets (JTextFields and a JTable) for the input and output.
Continue Reading »
Popularity: 100% [?]
Posted in Tech |
2 Comments » | July 15th, 2010
Clojure, Log4J, and clojure.contrib.logging can play nicely together. Here’s a reasonable default configuration.
-
If using Leiningen, add
:dependencies [
[log4j "1.2.15" :exclusions [javax.mail/mail
javax.jms/jms
com.sun.jdmk/jmxtools
com.sun.jmx/jmxri]]
]
to your project.clj file and rerun lein deps to auto-install the Log4J JAR.
If not using Leiningen, acquire a Log4J JAR and put it on your classpath.
- Create a file called
log4j.properties on your classpath and put the following into it:
# Based on the example properties given at http://logging.apache.org/log4j/1.2/manual.html
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern= %-5p %c - %m%n
That’s the short version. Read on for the gory details. Continue Reading »
Popularity: 95% [?]
Posted in Tech |
1 Comment » | May 16th, 2010
I wanted to parse some externally-generated and malformed HTML, so naturally I went to the short and sweet clojure.xml/parse function. I got a nasty error:
error: java.io.IOException: Server returned HTTP response code: 503 for URL: http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
It seems that the W3C blocked access to the DTDs two years ago, but Java still tries to load them by default anyway. The following allows clojure.xml to run without checking external DTDs:
(defn startparse-sax-non-validating [s ch]
(.. (doto (. javax.xml.parsers.SAXParserFactory (newInstance))
(.setValidating false)
(.setFeature "http://apache.org/xml/features/nonvalidating/load-dtd-grammar" false)
(.setFeature "http://apache.org/xml/features/nonvalidating/load-external-dtd" false)
(.setFeature "http://xml.org/sax/features/validation" false)
(.setFeature "http://xml.org/sax/features/external-general-entities" false)
(.setFeature "http://xml.org/sax/features/external-parameter-entities" false))
(newSAXParser) (parse s ch))))
Then you can simply (xml/parse "sourcefile.xml" startparse-sax-non-validating). This is not the ideal solution — ideally, we want to use a locally cached DTD — but it works well enough for one-off code. Read on for further information. Continue Reading »
Popularity: 36% [?]
Posted in Tech |
4 Comments » | April 28th, 2010
The date and time classes built into Java are a horrible mess. What are Clojure programmers to do? Use Joda Time instead. Joda Time is coherently designed and easy to use.
JDBC (at least, the PostgreSQL driver) can’t use Joda Time directly (without explicit type mapping). One way to convert a Joda LocalDate into something JDBC can use:
(defn to-sql-date [date]
"Convert any Joda-readable date object (including a string) to a java.sql.Date"
(java.sql.Date. (.. (LocalDate. date) toDateMidnight toInstant getMillis)))
It’s not pretty, but it works. You can follow a similar procedure for any of the Joda classes.
Popularity: 30% [?]
Posted in Tech |
2 Comments » | March 24th, 2010
Clojure.contrib.logging doesn’t have any way to set the log level. This is obviously a problem if you want to make use of various log levels (debug, warn, etc.) to separate different logging depths. Here’s a function to set the logging level on my default clojure.contrib.logging setup:
;;; This version works when (impl-get-log "") returns an org.apache.commons.logging.impl.Jdk14Logger
(use 'clojure.contrib.logging)
(defn set-log-level! [level]
"Sets the root logger's level, and the level of all of its Handlers, to level.
Level should be one of the constants defined in java.util.logging.Level."
(let [logger (.getLogger (impl-get-log ""))]
(.setLevel logger level)
(doseq [handler (.getHandlers logger)]
(. handler setLevel level))))
;;; This version works when (impl-get-log "") returns a java.util.logging.LogManager$RootLogger
(use 'clojure.contrib.logging)
(defn set-log-level! [level]
"Sets the root logger's level, and the level of all of its Handlers, to level.
Level should be one of the constants defined in java.util.logging.Level."
(let [logger (impl-get-log "")]
(.setLevel logger level)
(doseq [handler (.getHandlers logger)]
(. handler setLevel level))))
This log level setting function works with a standard out-of-the-box clojure.contrib.logger on my system — depending on what logging libraries it finds on your classpath, as per the docs, it may not work for you. In particular, you need to be using clojure.contrib.logger to wrap an Apache Commons Logging instance, which in turn wraps a java.util.logging instance. This is the way my system works without any configuration; YMMV. Hopefully, something like this will be assimilated into a universal wrapper in the next clojure.contrib.logging.
For the gory details of how this was constructed… Continue Reading »
Popularity: 45% [?]
Posted in Tech |
7 Comments » | March 14th, 2010
Libcurl’s Java bindings now compile on Macintosh, with a few minor modifications to the Makefile. Get the code from my Github account.
Update: More recent Java bindings, which do not seem to be linked anywhere on the libcurl site, are available at http://www.gknw.net/viewvc/trunk/?root=curl-java. Still no multi support, though.
Popularity: 5% [?]
Posted in Tech |
No Comments » | March 13th, 2010
A Clojure version of my Yahoo Finance Ruby gem seemed like an interesting challenge and a good way to learn Clojure better. This version uses Apache HttpClient, which is significantly slower than libcurl. A libcurl version is on the way. Continue Reading »
Popularity: 45% [?]
Posted in Tech |
5 Comments » | March 13th, 2010
Rincanter allows one to access the R statistical programming language from Clojure. Rincanter also integrates R with Incanter for charting and more stats. Rincanter is based on rJava‘s JRI R-from-Java bridge. Getting this working required making a JNI library available to Clojure via an as-yet-undocumented Leiningen setting, :native-path. Continue Reading »
Popularity: 17% [?]
Posted in Tech |
No Comments » | March 4th, 2010