Clojure REPL philosophy and utility applications - clojure

Sorry for the long post, but this forum always asks for use cases :-).
I am frequently called upon to write utility applications (GUI and command line) for my organization. I most commonly write these in Java and more recently Scala.
The "philosophy" in Clojure (and other Lisps) seems to center around the REPL, and I have to agree that it makes a nice development environment, but I obviously cannot produce utilities that require that the users "Install Clojure and Clojure-contrib, then unzip the utility into a directory on your hard drive, start Clojure from the command line using the following classpath, ...". The users do not care that the utility is written in Clojure. They just want to point and click or, at most, type "java -jar Utility.jar -?" on the command line.
My question centers around compilation and separating the application into many namespaces/files.
I know that the main method must be in a Clojure file that includes the (gen-class...) command (or the :gen-class clause on the ns command). Do I do the same for the additional Clojure files, or should they be left as a source code that is packed in the JAR and loaded by the main file? How about testing the additional files from the REPL during development?
I have been using leiningen, cake, and maven to build self-contained JARs (containing unpacked clojure.jar, clojure-contrib.jar, and commons-cli.jar files), but so far I have been writing my code in the same file as the main method.

It's up to you whether you AOT-compile your .clj into .class or let Clojure do it dynamically at runtime. In general, I find it's easier during development to avoid AOT and just use AOT as a performance booster for cases where the cost of compiling your .clj on the fly doesn't make sense (limited environments like Google App Engine or utilities where startup time is critical). For server processes that will run for a long time, there is not much advantage in AOT-compiling.
There are a number of complexities around using AOT classes at dev times that are specific to different dev environments.
One down-side of AOT is that your compiled classes might be incompatible with a future version of Clojure and that is more likely than your clj files being incompatible. That may become more important over time.

When it comes to the separation, I'd keep them in separate namespaces/files:
One for the :gen-class namespace, containing -main and all the other Java-like things.
The other namespace for all the functions.
Ideally, your main should contain just the call to a function from the other namespace, or perhaps some logics for evaluating or repacking of the args.
Your question seems quite similar to the separation between logics code and ui code. You could see the namespace with :gen-class as just an interface provided by your program to Java code, nothing more.
When it comes to the utilities (like command-line, or Swing apps, etc.), there's a trouble with Java in general because of the JVM start time.
Now, you could solve this by making a server app run the REPL in the background all the time, and say, somehow receive an s-expr to be evaluated, and return the result. This could be done as a simple web application which receives an s-expr as URL parameter, and returns the result. Now, with this you could make all utilities in plain old Java, or even bash using wget, since all you'd have to do is access an URL (assuming that server with repl is running in background).
There's a good chance that something like this already exists, so if anyone knows - comments are more than welcome.
Oh, and another thing, the port where repl webapp is exposed would probably have to be closed to the outside world, to prevent Clojure-injections :D

Related

Does AspectJ support modifying the JDK bytecode?

I want to intercept ClassLoader's loadClass method. In order to show the process of loading class by ClassLoader. But I don't know the working principle of aspectj. It can modify the source code of the JDK?
You could just use a debugger and step through the process in order to understand it.
AspectJ can weave aspects into existing class files during compilation (CTW, compile-time weaving) or during class-loading (LTW, load-time weaving).
LTW does not work for JDK classes because those classes are always loaded before the weaving agent. So you have a bootstrapping problem there.
What you can do, however, is to apply CTW to the class files in e.g. rt.jar from your JRE/JDK distribution, zip the woven class files into another JAR and replace the original one with it. Then if you start the JVM with aspectjrt.jar on the boot classpath, you see the aspects in action. I have done that before and it works. But really, this is not what AspectJ was designed for. But you asked, so I told you. It is a very advanced topic though, and I doubt that it is the road you should take as a complete AspectJ greenhorn - no offense meant.
Update: Because I have been asked this question so often, I created a little demo project showing how to weave aspects into the JRE/JDK. I still do not think it makes sense to use it under normal circumstances, but what the heck: Why do people climb the Mount Everest? Because it exists. ;-)

Clojure Worksheets

I have been learning Clojure a bit recently. Is there such a thing in Clojure world as Scala-like worksheets, into which I can put any code and get it evaluated as soon as I save it? Or maybe there's a similar solution for Clojure?
I am now working with lein repl and, sadly, can't say it's the most usable tool ever.
In Lisp development in general (and Clojure in particular) the preferred programming style is what's usually dubbed interactive programming - the developer keeps an image of the app loaded at all times and interacts with it via a REPL. You can easily modify the loaded code on the fly and test changes immediately in the REPL (that's not easy at all with Scala - one has to resort to something like JRebel to do it). I find the Scala worksheets a pretty primitive solution in comparison...
The workflow that I follow in Clojure is:
I open nREPL.el in Emacs - this loads my lein2 project with all of its dependencies and gives me a REPL which I can use the try out stuff
I write some code in source code and load the changed functions (maybe by evaluating a top level form with C-M-x
Afterwards I'd press C-x C-z to jump back to the REPL and I try out the new code in it
I go back to step 2
Basically the Clojure REPL is much more powerful than the Scala REPL and I personally consider it hugely superior to the Scala IDE worksheets. Once you get used to the interactive incremental style of programming Lisp offers everything else starts to look strangely complex by comparison. I'm not familiar with Eclipse's CounterClockWise Clojure plugin, but I'm pretty sure it offers similar functionality to Emacs's nREPL.el.
You might want to take a look at the autoexpect plugin for Leiningen. Every time you save any file in the working directory, the plugin compiles and runs your code; as a bonus, it will evaluate any "expect" function calls which can serve as tests. This is very helpful for test driven development and is a nice compliment to working with the REPL as described in the other answer (I often use one or the other or both together depending on how many test cases I have in place).
I should note that running autoexpect is far faster than running "lein test" or "lein run" repeatedly, due to the startup cost of the JVM and Leiningen.
It sounds like what you are looking for is the Clojure Koan plugin. This a worksheet-style problem-solving exercise tool that watches your edits and provides instant feedback on the correctness of your work.
For actual development workflow I second the advice others here have provided on tooling and interactive environment setup, but since you specifically said you are learning Clojure, this can be a fun approach. There is also a web application called 4Clojure that you might have fun playing with.
However you will eventually (or right away) want to get a smooth and convenient development environment set up, and I haven't seen any mention so far of a few important tools. One person mentioned Nrepl. If you like Emacs, there's a slime/swank-like interaction mode that jacks into nrepl called nrepl.el that provides very nice integration between editing files and messing around in the repl. Similarly there is VimClojure, and you can find plugins for IntelliJ (LaClojure) and Eclipse (Counterclockwise) that are also popular and well-maintained.
Someone mentioned autoexpect. If you are looking for a good TDD setup, I would recommend midje. If you are using a 2.0 preview release of leiningen there are a few issues with the lazytest integration being in flux, and lazytest itself is or should be deprecated. But I prefer midje over expectations, personally, and these problems will surely be worked out in the 2.0 release of lein-midje. The stable version of lein-midje that works with the non-preview (1.x) leiningen has autotest-like functionality using lazytest.
As nobody has mentioned it, cursive is really nice these days, although I was sad to move back to a full blown IDE. You can easily do the same thing as Emacs with the built in repl, just move into the namespace that you are working with and synchronise every time you make changes. It's a really nice way of working.

Easy way to process batch data through web service?

What simple tools would you recommend to read a text file of addresses, send each record separately to a web service for geocoding, and save the batch of results as a text file?
Looking for no-frills component(s) with usage examples, for minimal code-from-scratch. Language irrelevant as long as dev environment is easy to install.
Requirements
- usable by unsophisticated programmer
- low or no cost
- runs under Windows.
Second thought:
How easy would this be to do inside a browser using JavaScript and a library or two?
I'd go for Java and use a flat file parsing library like jFFP or Flatworm
These libraries are pretty easy to understand and to use (I've worked with both of them in the past) and they both provide code samples.
Spring Integration would be another good option but the learning curve might be too big if you are not familiar with Spring and it might be overkill for your simple workflow.
Actually, in your case, I think I'd choose Flatworm for the parsing. You'll find code samples on its website or in How to read and parse flat files in Java. And you could even use it to write your output file like in Writing flat files in Java with Flatworm).
For the SOAP part, I'd use the JAX-WS Reference Implementation (which is included in the JDK 6 so you won't have to add any library if you are using Java 6) and Netbeans IDE. Netbeans IDE has very good support for developing JAX-WS Web Services Client (or here for later versions of Netbeans) and should really ease the process. Once the various classes generated, calling the web service is a matter of 3 lines of code as shown in the examples of the provided links:
// Call Web Service Operation
com.cdyne.ws.Check service = new com.cdyne.ws.Check();
com.cdyne.ws.CheckSoap port = service.getCheckSoap();
// TODO initialize WS operation arguments here
java.lang.String bodyText = "";
java.lang.String licenseKey = "";
// TODO process result here
com.cdyne.ws.DocumentSummary result = port.checkTextBody(bodyText, licenseKey);
Given the generic nature of the requirements, the relatively simple workflow, but its potential to bring a few twists and turns in the design (for example, the need of using https rather than http for webservices, the need of producing some odd token for authentication, or some fancy marshaling or conversion etc.) it might be best to use a modern script language. A very basic plan could be to use a plain shell script (a bat file), base on curl and a few other command line utilities, but this approach may not be flexible enough to deal with some requirements; instead languages such as Perl, PHP, Python, Ruby would be much preferable.
This would provide a low entry barrier, the ability to test elements of the application interactively before putting them into a formal script, and to leverage extensive libraries to deal with the various requirements that may arise, such as the storage of configuration parameters, parsing detail, output format, webservices, maths associated with geo positions etc. etc.
My inclination would be to use Python, but as said most other modern dynamic languages would do.
I would use XMLunit with Eclipse IDE, + JUnit, and JDK1.6 . A finished program that does this might only be 100 lines of code. It's doable by someone who is a novice programmer...
When the program is done you can compile as an .exe file for future use.
I would choose "Strawberry Perl" as my second choice for programming language. Python is slightly harder to use I think.

Test Anything Protocol in Shell scripts

Has anyone seen, tried to implement, or otherwise played with TAP in shell? We're looking to create unit tests across many languages (don't get me started on why this doesn't exist so far), and since we have so much Perl code, we'll be looking at TAP (among others, I imagine). I've found a TAP library for C, Perl, of course, has it built-in, and I've even found an API for Java. But one area missing is shell script testing.
Not that I've found much on unit-testing shell scripts, either, but since we do have thousands of lines of shell code, it'd be nice to be able to test it somehow.
See the list of TAP Producers for a list of libraries. On that list you will find Tap-functions for shell code.
Bats is simple Bash only test framwork, tests could be written in a very clear syntax.
shUnit is the oldest shell test framework, little documentation.
shUnit2 is a most recently project inspired by shUnit, but completely different. Tests could be written in a more xUnit fashion. Most importantly, it is POSIX compatible.
I usually write my own small test framework for my shell scripts. Some things to keep in mind when doing this:
When working with files, make all paths relative to some variable which you can modify in your tests.
diff(1) is great to verify test results (and to display a useful error message to the user)
Use local variables extensively
Everything must be in a function
That said, my "test framework" is mostly a set of shell functions (named test*) and a runTests function which calls them one by one. Nothing fancy, really. Tests create a work directory for the test, copy all necessary files into it, run a function, verify the results against a know-good set of files.

Can any IDE or framework help test new code quickly without having to run the whole application

I mainly develop in native C++ on Windows using Visual Studio.
A lot of times, I find myself creating a new function/class or whatever, and I just want to test that piece of logic I just wrote, quickly.
A lot of times, I have to run the entire application, which sometimes could take a while since there are many connected parts.
Is there some sort of tool that will allow me to test that new piece of code quickly without having to run the whole application?
i.e.
Say I have a project with about 1000 files, and I'm adding a new class called Adder. Adder has a method Add( int, int );
I just want the IDE/tool to allow me to test just the Adder class (without me having to create a new project and write a dummy main.cpp) by allowing me to specify the value of the inputs going into Adder object. Likewise, it would be nice if it would allow me to specify the expected output from the tested object.
What would be even cooler is if the IDE/tool would then "record" these sets of inputs/expected output, and automatically create unit tester class based on them. If I added more input/output sets, it would keep building a history of input/outputs.
Or how about this: what if I started the actual application, feed some real data to it, and have the IDE/tool capture the complete inputs going into the unit being tested. That way, I can quickly restart my testing if I found some bugs in my program or I want to change its interface a bit. I think this feature would be so neat, and can help developer quickly test / modify their code.
Am I talking about mock object / unit testing that already exists?
Sidenote: it would be cool if Visual Studio debugger has a "replay" technology where user can step back to find what went wrong. Such debugger already exists here: http://www.totalviewtech.com/
It's very easy to get started with static unit testing in C++ - three lines of code.
VS is a bit poor in that you have to go through wizards to make a project to build and run the tests, so if you have a thousand classes you'd need a thousand projects. So for large projects on VS I've tended to organised the project into a few DLLs for independent building and testing rather than monolithic ones.
An alternative to static tests more similar to your 'poke and dribble' script could be done in python, using swig to bind your code to the interpreter, and python's doc tests . I haven't used both together myself. Again, you'd need a separate target to build the python binding, and another to run the tests, rather than it being just a simple 'run this class' button.
I would go with Boost.Test (see tutorial here)).
The idea would be to add a new configuration to your project, which would exclude from build all unnecessary cpp files. You would just have to add .cpp files to describe the tests you want to pass.
I am no expert in this area but i have used this technique in the past and it works !
I think you are talking about unit testing and mock objects. Here are couple of C++ mock object libraries that might be useful :-
googlemock which only works with googletest
mockpp
You are essentially asking how can I test one function instead of the whole application. That is what unit-testing is, and you will find many questions about unit-testing C++ on SO.