tag:blogger.com,1999:blog-53979959779781800672024-03-13T14:33:07.030-07:00Lisp4Fun - musings on computers and lifeJohan Berntssonhttp://www.blogger.com/profile/11335430712667887923noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-5397995977978180067.post-53441278129848784012010-02-27T21:09:00.001-08:002010-02-27T21:30:31.604-08:00Leiningen and VimClojureLeiningen seems cool. It will take care of installing any jars you need for your clojure application, and setting up the classpath, so you don't have to worry about all the Java stuff and can get down to clojure hacking.<br /><br />I spent some time yesterday getting Leiningen and VimClojure to work together. Here are the notes in case someone else has the same problem:<br /><br />> mkdir code/clojure<br />> cd code/clojure<br /><br /><span style="font-weight: bold;">LEININGEN</span><br /><br />I downloaded the install script from http://github.com/technomancy/leiningen and put it<br />in ~/code/clojure/lein.<br /><br />> chmod a+x lein<br />> ./lein self-install<br />> ./lein new helloworld<projectname><br />> cd helloworld<projectname><br /><br /><span style="font-weight: bold;">NAILGUN server</span><br /><br />Next step was to add the nailgun server needed by vimclojure. To do this I added these lines<br />to project.clj (which was generated by "lein new helloworld" before)<br /> [org.clojars.ato/nailgun "0.7.1"]<br /> [org.clojars.gilbertl/vimclojure "2.1.2"]<br /></projectname></projectname><br />Now update the dependencies to generate a lib directory with all jars we need:<br /><br />> ../lein deps<br /><projectname><projectname><br />VIMCLOJURE<br /><br /></projectname></projectname>I already had the vimclojure vim plugin installed, but needed to add<projectname><projectname> the nailgun client for VimClojure. For this I downloaded VimClojure (http://www.vim.org/scripts/script.php?script_id=2501), unpacked it somewhere and ran "make". This generated a file called "ng" which I copied to ~/code/clojure. I then added this line to .vimrc:<br />let vimclojure#NailgunClient = "/home/johan/code/clojure/ng"<br /><br />I then put these alias definitions in .bashrc:<br />alias lein='/home/johan/code/clojure/lein'<br />alias ng='java -server -cp src:classes:lib/* com.martiansoftware.nailgun.NGServer 127.0.0.1'<br /><br /><span style="font-weight: bold;">TESTING AND RUNNING</span><br /><br />> cd ~/code/clojure<br />> ng &<br />> vim src/helloworld.core.clj<br />You should now be able to type <leader>sr to open a REPL buffer (\sr if using the default settings)<br /><br /></leader></projectname></projectname>Johan Berntssonhttp://www.blogger.com/profile/11335430712667887923noreply@blogger.com0tag:blogger.com,1999:blog-5397995977978180067.post-85848408807502299952009-04-09T19:07:00.000-07:002009-04-12T18:18:37.319-07:00VimClojure on UbuntuI have just installed vimclojure-2.0.0 on Ubuntu. Here is my log, in case someone wants to do something similar<br /><pre><br />Download vimclojure: http://kotka.de/projects/clojure/vimclojure.html<br /><br /> cd<br /> mkdir clojure<br /> cd clojure/<br /> svn co http://clojure.googlecode.com/svn/trunk clojure<br /> cd clojure/<br /> ant<br /> cd ..<br /> mkdir jars<br /> cp clojure/clojure.jar jars<br /> git clone git://github.com/kevinoneill/clojure-contrib.git<br /> cd clojure-contrib/<br /> ant -Dclojure.jar=../clojure/clojure.jar<br /> cp *jar ../jars<br /> tar xvfz vimclojure-2.0.0.tar.gz <br /> cd vimclojure-2.0.0/<br /> vi local.properties<br />clojure.jar=/home/johan/clojure/jars/clojure.jar<br />clojure-contrib.jar=/home/johan/clojure/jars/clojure-contrib.jar<br />nailgun-client=ng<br />vimdir=/home/johan/.vim<br /> ant<br /> mkdir ~/.vim/autoload<br /> mkdir ~/.vim/doc<br /> mkdir ~/.vim/indent<br /> mkdir ~/.vim/syntax<br /> mkdir ~/.vim/ftdetect<br /> mkdir ~/.vim/ftplugin<br /> mkdir ~/.vim/ftplugin/clojure<br /> cp autoload/vimclojure.vim /home/johan/.vim/autoload/<br /> cp doc/clojure.txt /home/johan/.vim/doc/<br /> cp indent/clojure.vim /home/johan/.vim/indent/<br /> cp syntax/clojure.vim /home/johan/.vim/syntax/<br /> cp ftdetect/clojure.vim /home/johan/.vim/ftdetect/<br /> cp ftplugin/clojure.vim /home/johan/.vim/ftplugin/<br /> cp ftplugin/clojure/completions-clojure.* /home/johan/.vim/ftplugin/clojure<br /> vi ~/.vimrc<br />filetype plugin indent on<br />syntax on<br />let clj_highlight_builtins = 1<br />let clj_highlight_contrib = 1<br />let clj_paren_rainbow = 1<br />let clj_want_gorilla = 1<br />let vimclojure#NailgunClient = "/home/johan/clojure/vimclojure-2.0.0/ng"<br />autocmd BufRead,BufNewFile *.clj nmap <F5> <Plug>ClojureEvalFile<br />autocmd BufRead,BufNewFile *.clj nmap <F6> <Plug>ClojureEvalLine<br />autocmd BufRead,BufNewFile *.clj nmap <C-F6> <Plug>ClojureEvalToplevel<br />autocmd BufRead,BufNewFile *.clj nmap <F7> <Plug>ClojureStartRepl<br />autocmd BufRead,BufNewFile *.clj nmap <F8> <Plug>ClojureDocLookupWord<br />autocmd BufRead,BufNewFile *.clj nmap <F8> <Plug>ClojureDocLookupInteractive<br /> vi ~/.bashrc<br /># Clojure<br />export CLOJURE_EXT=/home/johan/clojure/jars<br />export LD_LIBRARY_PATH=/home/johan/clojure/bin-linux<br />alias clojure='/home/johan/clojure/clojure-contrib/launchers/bash/clj-env-dir'<br />if ! (ps -ef | grep com.martiansoftware.nailgun.NGServer > /dev/null)<br />then<br /> sh /home/johan/clojure/vimclojure-2.0.0/bin/ng-server &<br />fi<br /></pre><br /><br />And for the final step, start vim and type ":helptags ~/.vim/doc" to make the documentation available with ":help clojure"<br /><br />You can also add this Nailgun startup script in /etc/init.d/nailgun<br /><pre><br />#! /bin/sh<br /># Install: ln -s /etc/init.d/nailgun /etc/rc3.d/S99nailgun<br /><br />do_start () {<br /> export CLOJURE_EXT=/home/johan/clojure/jars<br /> export LD_LIBRARY_PATH=/home/johan/clojure/bin-linux<br /> sh /home/johan/clojure/vimclojure-2.0.0/bin/ng-server &<br />}<br /><br />do_stop () {<br /> `ps -ef | grep java | grep NGServer | awk '{print "kill -9 "$2}'`<br />}<br /><br />case "$1" in<br /> start)<br /> do_start<br /> ;;<br /> stop)<br /> do_stop<br /> exit 0<br /> ;;<br /> restart)<br /> do_stop<br /> do_start<br /> exit 0<br /> ;;<br /> *)<br /> echo "Usage: $0 start|stop|restart" >&2<br /> exit 3<br /> ;;<br />esac<br /></pre>Johan Berntssonhttp://www.blogger.com/profile/11335430712667887923noreply@blogger.com0tag:blogger.com,1999:blog-5397995977978180067.post-81461329266270832352009-02-02T18:32:00.000-08:002009-02-02T18:44:48.269-08:00Xith3D with ClojureI've been working a bit with 3D graphics with Clojure, and I have now tested various libraries. jME was a bit awkward to set up in Clojure, and Java3D is dying, so my current efforts are concentrated on Xith3D. I had some problems installing Xith3D in Ubuntu 8.10, so here is how to get started:<br /><br /><span style="font-weight:bold;">Download</span><br /><pre><br /> mkdir xith<br /> cd xith<br /> svn co https://xith3d.svn.sourceforge.net/svnroot/xith3d/trunk xith3d<br /> svn co https://xith-tk.svn.sourceforge.net/svnroot/xith-tk xith-tk<br /></pre><br /><br /><span style="font-weight:bold;">Compiling problems and how to solve them</span><br /><br />xith3d only compiles with java-6-sun, not openjdk-6, so<br />remove all other java implementations (see /usr/lib/jvm) first.<br /><pre><br /> cd xith<br /> ant<br /></pre><br />Another problem is that ant is too old, and svnrevget fails. To fix<br />this, go to xith and do "svn log". Note the latest revision number,<br />and go to xith-tk. Edit build.xml, and find the svnrevget line.<br />Comment it out and hard-code the version number:<br /><pre><br /> [!-- svnrevget property="version.build" path="${project.xith3d}"/--]<br /> [property name="version.build" value="1668"]<br /></pre><br />Now you can do "ant" in the xith-tk folder.<br /><br /><span style="font-weight:bold;">Testing</span><br /><br />Here is a basic test file (put it in the xith folder, call it test.clj)<br /><pre><br />(import '(org.xith3d.loop RenderLoop)<br /> '(org.xith3d.render Canvas3D Canvas3DFactory)<br /> '(org.xith3d.base Xith3DEnvironment))<br /><br />(let [env (Xith3DEnvironment.)<br /> canvas (.createWindowed Canvas3DFactory 800 600 "My empty scene")<br /> rl (RenderLoop.)]<br /> (.addCanvas env canvas)<br /> (.setMaxFPS rl 120.0)<br /> (.setXith3DEnvironment rl env)<br /> (.begin rl))<br /></pre><br />And a script to start it with<br /><pre><br />#!/bin/sh<br />export LD_LIBRARY_PATH=xith3d/third-party/gluegen/linux-i586:$LD_LIBRARY_PATH<br />export LD_LIBRARY_PATH=xith3d/third-party/jogl/linux-i586:$LD_LIBRARY_PATH<br />java -cp ~/.clojure/clojure.jar:xith3d/dist/xith3d.jar:xith3d/third-party/jagatoo.jar:xith3d/third-party/math/openmali.jar:xith3d/third-party/jogl/jogl.jar:xith3d/third-party/gluegen/gluegen-rt.jar clojure.lang.Script test.clj<br /></pre><br /><br />and an empty window appears on the screen.Johan Berntssonhttp://www.blogger.com/profile/11335430712667887923noreply@blogger.com0tag:blogger.com,1999:blog-5397995977978180067.post-12262624597361639142009-01-17T20:23:00.000-08:002009-01-18T17:32:35.464-08:00XmlRpc with ClojureI know, I know... I really should update this blog more often. Brief recap: last year I've made a fairly big Common Lisp project (<a href="http://forge.opensimulator.org/gf/project/funmv/">funmv</a>), and played quite a bit with both Haskell and Clojure. Today I want to share some experiences using XmlRpc with Clojure.<br /><br />Clojure has the advantage of running on JVM, which means that it can use Java libraries. The problem is that, being a Lisp with a functional flavour, it sometimes doesn't mix well with the object-oriented programming style mandatory in Java development. I needed to implement a Xml-Rpc server in clojure, but my first attempts with apache xmlrpc and others ended in failure since their addHandler methods required an object, and would match the XmlRpc message methods with the name of the member functions. It is possible to instantiate Java-objects with the <span style="font-style: italic;">proxy</span> special form in Clojure, but this only allows you to implement or subclass already declared methods, and creating an object completely from scratch seemed too complicated.<br /><br />Finally I found an XmlRpc library from <a href="http://xmlrpc.sourceforge.net/">redstone</a> (download redstone-simple-xmlrpc-1.0.zip), which allows you to define your own dispatch routine. This allows me to inspect the method name and call normal Clojure functions, rather than relying on Java reflection magic. The server code is short and sweet:<br /><pre><br />(def server (new redstone.xmlrpc.simple.Server 8080))<br />(def h (proxy [redstone.xmlrpc.XmlRpcInvocationHandler] []<br /> (invoke [method-name arguments]<br /> (cond<br /> (= method-name "add") (+ (nth arguments 0) (nth arguments 1))<br /> true (throw (new Exception "No such method"))))))<br />(doto (.getXmlRpcServer server) (.addInvocationHandler "test" h))<br />(.start server)<br /></pre><br /><br />Here is a Java test client<br /><pre><br />import redstone.xmlrpc.XmlRpcClient;<br />public class SampleClient<br />{<br /> public static void main( String[] args ) throws Exception<br /> {<br /> XmlRpcClient client = new XmlRpcClient( "http://localhost:8080", false );<br /> Object reply = client.invoke( "test.add", new Object[] {1, 2} );<br /> System.out.println (reply); // should be 3<br /> }<br />}<br /></pre><br /><br />You will need to add the jar files to the classpath:<br /><pre><br />javac -cp xmlrpc-1.1.1.jar SampleClient.java<br />jar cf SampleClient.jar *class<br />java -cp SampleClient.jar:xmlrpc-1.1.1.jar SampleClient<br /></pre>Johan Berntssonhttp://www.blogger.com/profile/11335430712667887923noreply@blogger.com0tag:blogger.com,1999:blog-5397995977978180067.post-37330337602314325212008-03-11T17:09:00.000-07:002008-03-11T01:11:50.217-07:00Is Qi the answer?I have returned to functional programming after more than 10 years of C, C++, C# and Java, and it feels good. The old favorite is of course Lisp, but much has happened in the field since I was active, and I thought I should check out the statically typed languages, such as Haskell. Well, it turned out to be a fairly unpleasant experience. Not because it was hard to make programs - the program structure in Scheme is fairly similar to Haskell - but because the typing system keeps getting in the way of my iterative way of programming. I want to write some code, play with it, extend it, play with it some more, and let it grow step by step. In Haskell, like in statically typed imperative languages, I seem to spend most of the time fixing type errors instread. Of course they have to be taken care off, but not when I'm prototyping!<br /><br />But perhaps I found a good compromise: Lisp syntax, Haskell-like functional programming, <span style="font-style: italic;">optional</span> type checking, lots of libraries. <a href="http://www.lambdassociates.org/qilisp.htm">Qi</a> seems to mix the best of two worlds, and so far it has been a joy to use. First prototype your application, then turn on type checking, fix the errors and you should have a pretty robust program without the Haskell straitjacket. That is my hope, time will tell if it works out in practice.Johan Berntssonhttp://www.blogger.com/profile/11335430712667887923noreply@blogger.com0