… remote connection via nREPL

I am blessed with a lot of different architectures. I don’t really care which OS type I use as long as I can find the keys on the keyboard.

Sometimes, I have Clojure running for weeks on a remote unix host, connecting to it from emacs via slime-connect or nrepl.

I’ve noticed that M-. (slime-edit-definition or nrepl-jump) doesn’t work in the remote connection setup.

During my debugging, I found the reason for this and fixed it. The fix is not complete and will demand a “HOME” environment variable definition on windows (which I have anyway, because it is used by emacs).

The root of the problem is simply, that clojure delivers a local path to emacs. That is ok, if the two ends of the setup has the same host-type, but HOME on MacOS, Linux and Windows windows is not the same. Emacs tries to open the local path and even if the path is modified with the correct tramp information, we end with either the wrong path locally or externally.

The current fix is not entirely bullet-proof for windows, because “HOME” might not be defined on this platform.

test setup and the current fix.

Posted in clojure | Comments Off

… reverse ssh

We have the situation, where a cvs server is not available from a development host, but the development host is available from the cvs server (and a PC has access to both hosts):

       .-----.
       | PC  |
       .-----.
     /         \
    v           v
.-----.       .-----.
| cvs |------>| dev |
.-----.       .-----.

On the cvs machine, we start a cvs tunnel on localhost like this:

[CVSUSER@CVS wrk]$ ssh -nNT -R 2222:127.0.0.1:22 DEVUSER@DEVELOPMENT

and leave the tunnel running.

On the development system we add the following lines to ~/.ssh/config

Host CVS.full.domain
Hostname localhost
Port 2222
User CVSUSER

then we add the following line to ~/.bash_config

export CVSROOT=CVSUSER.full.domain:/cvsroot

and after that, we can interact with cvs as usual (as long as the tunnel is running)

cvs co module-name

Posted in coding | Comments Off

… private git repository on Dropbox

Usually, I have my code on github, but sometimes I’m experimenting with stuff that is not fit for public consumption just yet. In those cases, I set up a git repository inside my dropbox folder.

export DROPBOX=~/Dropbox/repos
export newRepo=project-name
export NAME=$(git config --global --get user.name)

cd ~/projects/$newRepo
git init
git add .
git commit -m 'initial'

HERE=$(pwd)
mkdir -p $DROPBOX/${newRepo}.git
cd $DROPBOX/${newRepo}.git
git --bare init
echo "$newRepo" > description
echo "[gitweb] owner = \"$NAME\"" >> config
cd $HERE

git remote add origin $DROPBOX/${newRepo}.git
git push origin master

After that it is possible to clone the repository on a different machine as needed:

git clone ~/Dropbox/repos/${newRepo}.git

[gist]

Posted in coding | Tagged , , | Comments Off

… not total 4clojure

It’s been almost a year since I posted something last and that is not good. The REPL has not been totally inactive though; using it on my work machine to make test calls to our administrative system; something that I would otherwise do in php.

The last few weeks, I’ve been going steadily through the problems at 4clojure, playing in the code golf league. The “greats” have some really tight solutions, sometimes I’m on par .. sometimes, not so much :-)

I found 4clj-el to make solving the problems a bit easier. (a really great tool)

The problems are fun and keeps my clojure skill honed.

Posted in clojure | Comments Off

… the old snippet server

When I saw that clojureql I wondered what the fuzz was about and decided to try it out on a small example.

Everybody who has read Programming Clojure, has been through the snippet server example in chapter 9.3, so that became the target for my experiments

As it happens, the only changes I had to do to snippets.database was including the appropriate library …

(:refer-clojure 
 :exclude [take drop sort distinct compile conj! disj!])
(:use clojureql.core)

… and then rewriting the few functions that interacts with the database.

(defn select-snippet [id]
  (first @(select snippets (where (= :id id)))))

(defn insert-snippet [body]
  (:id (last @(conj! snippets 
                     {:body body 
                      :created_at (now)}))))

… that’s it! The code ended up being shorter and easier to understand.

At this level clojureql was very nice to work with. The fact that it is possible, at all times, to get at the sql expression that I worked with was very useful.

Posted in clojure | Tagged | Comments Off

… 10 one-liners

A lot of people have responded to 10 Scala One Liners with implementations in a lot of different languages, including clojure. My suggestion can be found in this gist but one of the one-liners are especially interesting, Sieve of Eratosthenes.

There are a lot of different versions of this algorithm. This one is translated from Miranda, an old classic from Bird & Wadler, Introduction to Functional Programming, p.175, and goes something like this:

(defn sieve [xs]
  (filter #(not (zero? (mod % (first xs)))) (rest xs)))

(defn rsieve [xs]
  (map first (iterate sieve xs)))

(defn primes-below [max]
  (take-while #(not (nil? %)) 
	      (rsieve (range 2 max))))

Unfortunately, it blows the stack for large values, but I still like this simple compact definition.

Posted in clojure | Tagged | 2 Comments

… simple mandelbot


..where I realize that each point can be calculated in parallel.

Determining the inclusion in the Mandelbrot Set is relatively easy to define.

(defn mandelbrot?
  ([x y]
     (mandelbrot? (complex x y)))
  ([z]
     (loop [ c 1, m (iterate #(+ z (* % %)) z) ]
       (if (and (> 64 c) (< (abs (first m)) 2))
         (recur (inc c)
                (rest m))
         (if (= 64 c) 0 c)))))

Running through all the points in an interesting range and projecting them to an area of a desired width and height. Here, I decided that the interesting range should include the usual, easily recognizable set.

(defn mandelbrot [width height]
  (for [x (indexed (stepped-range -2  0.5 width))
        y (indexed (stepped-range  1 -1   height))]
    [(first x) (first y) (rem (mandelbrot? (second x) (second y)) 256)]))

With the lazy sequence, the entire area can be plotted:

(defn draw-mandelbrot [width height]
  (let [menu-bar-height 23
        border-width     3]
    (.clearRect gfx 0 0 (+ width border-width) (+ height menu-bar-height))
    (.setSize frame (Dimension. (+ width border-width) (+ height menu-bar-height)))
    (doseq [[x y v] (mandelbrot width height)]
      (plot (transform x y) (colors v)))))

The “(doseq ” is not very practical for parallel processing and I realized that it can be rewritten as “(dorun (map ” like this

(dorun (map (fn [[x y v]] (plot (transform x y) (colors v)))
            (mandelbrot width height)))

… and suddenly, it’s a simple matter of changing map to pmap to get the calculations processed in parallel.

In this case, there doesn’t seem to be much benefit in this. Even on my dual-core iMac, the parallel version is only a bit faster than the sequential version. BUT, it was easy to switch from one to another .. and I am probably missing something.

The code is available on github

Posted in clojure | Tagged | Comments Off

… external slime connection

The iMac is always turned on in the living room, it is usually playing cartoons or music. I never use it for programming as I prefer my good old IBM X40 running Ubuntu, even if it is several times slower and has less memory.

While I was solving Project Euler problems, I only used the power/memory of the iMac a few times because of the one minute rule. I can wait for the X40 to work for a minute.

Today I tried to start clojure on the iMac with this command:

lein swank 4005 192.168.0.100

Which allow me to start another slime connection from emacs on the X40 .. (the X40 has one processor, the iMac has two)

I have to figure out if it is safe to expose the connection to the world or how to secure that connection, for now it only runs on the LAN.

Posted in clojure | Tagged , | Comments Off

… adjusting the keyboard

I am using a Danish keyboard layout, but there are keys that I am not using in Emacs and there are keys that I use a lot, when writing Clojure.

As I am using Brian Carper’s fix for paredit-mode in the REPL and want the REPL and clojure files to behave in the same way, moving the keys have been put in the slime-override-map

Hopefully this will protect my fingers a bit.

I’ll probably have to do something similar for the php-mode I use at work.

Posted in clojure | Tagged | Comments Off

… enter clojure!

The 12th of december 2008, the word “clojure” enters my vocabulary for the first time. I was looking at sbcl/clisp and reading Practical Common Lisp and had done the classic Casting SPELs in Lisp at the time and figured that Clojure might be more interesting to look at.

In the beginning of 2009, setting up clojure was a pain and I might have used more time making emacs do what I wanted than looking at the language. Participation in JAOO and reading “Programming Clojure” in September 2009 was really what gave me the big kick-start.

I found RosettaCode in December and contributed the clojure solution for a few tasks .. then I found Project Euler and for the next 6 months, I went from using the problems to learn clojure to using clojure to solve problems. The REPL and the exploratory aspect of problem research is really awesome but at some point solving Project Euler problems does not add anything new to learning a programming language.

… I got to explore some other angle.

Posted in clojure | Tagged | Comments Off