Tampilkan postingan dengan label Clojure. Tampilkan semua postingan
Tampilkan postingan dengan label Clojure. Tampilkan semua postingan

Agustus 14, 2012

Working with GitHub and Clojars


This guide is used as a note for myself. If you can benefit from this note, that's fine. Here I just wish to document how I work with GitHub and Clojars. I need to do this since I have my own need for Ring middleware to reload automatically a server without restart whenever a namespace in Ring is modified. The original version was not maintained I think, since it uses Clojure 1.2.0.

GitHub

I forked the ring-reload-modified to my Github account. This can be done easily using step 1 and 2 from https://help.github.com/articles/fork-a-repo (I use only step 1 and 2 since I only want to fork and modified my forked version, not upstream version).

Let's say for this time I just want to edit the README.md and project.clj files. After I edit those files, I just put them in committed state:

[bpdp@bpdp-arch ring-reload-modified]$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add [file]..." to update what will be committed)
#   (use "git checkout -- [file]..." to discard changes in working directory)
#
# modified:   README.md
# modified:   project.clj
#
no changes added to commit (use "git add" and/or "git commit -a")
[bpdp@bpdp-arch ring-reload-modified]$

We can see from the status that it has 2 changes: README.md and project.clj but I haven't commit anything. Here's how to commit:

[bpdp@bpdp-arch ring-reload-modified]$ git commit -a
[master ada41aa] Adjust to latest Clojure version (1.4.0)
 2 files changed, 8 insertions(+), 4 deletions(-)
[bpdp@bpdp-arch ring-reload-modified]$ 

Then it's time to push the committed version to Github repository:

 [bpdp@bpdp-arch ring-reload-modified]$ git push origin master
 Username for 'https://github.com': bpdp
 Password for 'https://bpdp@github.com': 
 Counting objects: 7, done.
 Delta compression using up to 2 threads.
 Compressing objects: 100% (4/4), done.
 Writing objects: 100% (4/4), 721 bytes, done.
 Total 4 (delta 2), reused 0 (delta 0)
 To https://github.com/bpdp/ring-reload-modified.git
    6dfdc5c..ada41aa  master -> master
 [bpdp@bpdp-arch ring-reload-modified]$

Let's see the result in Github profile:



The result in Github project page (I have pushed another commit so the history is different with above).



Clojars

To upload the library, I need to prepare my ssh public key first:

[bpdp@bpdp-arch ring-reload-modified]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/bpdp/.ssh/id_rsa): 
/home/bpdp/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/bpdp/.ssh/id_rsa.
Your public key has been saved in /home/bpdp/.ssh/id_rsa.pub.
The key fingerprint is:
87:fc:8f:35:18:20:2e:8f:73:d2:b5:71:ea:0e:85:01 bpdp@bpdp-arch
The key's randomart image is:
+--[ RSA 2048]----+
|    E            |
|     .           |
|      o .        |
|     . = o       |
|    . o S +      |
|     = o B o     |
|    + = o o o    |
|     + o   + .   |
|       .o . .    |
+-----------------+
[bpdp@bpdp-arch ring-reload-modified]$

The public key should be put into the Clojars first. So, first we need to register by using http://clojars.org/register:



Now, it's time to upload to Clojars:

[bpdp@bpdp-arch ring-reload-modified]$ lein2 jar   
Created /home/bpdp/master/clojure/libs/ring-reload-modified/target/ring-reload-modified-0.1.2.jar
[bpdp@bpdp-arch ring-reload-modified]$ lein2 pom
Wrote /home/bpdp/master/clojure/libs/ring-reload-modified/pom.xml
[bpdp@bpdp-arch ring-reload-modified]$ scp pom.xml target/ring-reload-modified-0.1.2.jar clojars@clojars.org:
Enter passphrase for key '/home/bpdp/.ssh/id_rsa': 
Welcome to Clojars, bpdp!
pom.xml                                                           100% 2672     2.6KB/s   00:00    
ring-reload-modified-0.1.2.jar                                    100% 5328     5.2KB/s   00:00    

Deploying org.clojars.bpdp/ring-reload-modified 0.1.2

Success! Your jars are now available from http://clojars.org/
[bpdp@bpdp-arch ring-reload-modified]$

Now I can use the library that I upload to Clojars:



That's all big guy. Have a happy hacking!


Agustus 12, 2012

Mengakses Basis Data Graph OrientDB Menggunakan Clojure

OrientDB adalah salah satu basis data NOSQL dengan kemampuan basis data obyek, graph, document, serta flat. API untuk basis data ini bermacam-macam dan karena dikembangkan menggunakan Java, maka API yang paling utama adalah Java, meski demikian, tersedia juga API untuk JavaScript dan implementasi bahasa-bahasa pemrograman berbasis JVM seperti Clojure dan Scala.

Pada tulisan ini saya akan sedikit membahas tentang akses dengan menggunakan Clojure. Wrapper Clojure untuk API OrientDB dibuat oleh Eduardo Julián. Versi binary bisa diperoleh dari http://clojars.org melalui Leiningen dan versi kode sumber bisa diperoleh di https://github.com/eduardoejp/clj-orient (sayangnya, dokumentasi kurang jelas). Langkah sederhana berikut digunakan untuk mengakses OrientDB dari Clojure menggunakan Leiningen.

1. Buat project

$ lein new orientweb

2. Hasil:

[bpdp@bpdp-arch orientweb]$ tree
|-- README.md
|-- doc
|   `-- intro.md
|-- project.clj
|-- src
|   `-- orientweb
|       `-- core.clj
`-- test
    `-- orientweb
        `-- core_test.clj
5 directories, 6 files
[bpdp@bpdp-arch orientweb]$ 

3. Edit project.clj, isikan berikut ini:
 
(defproject orientweb "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
               :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.4.0"]
                         [clj-orient "0.5.0"]]
  :main orientweb.core)

4. Download dependencies dengan perintah:
 
lein deps

5. Koneksi terdapat pada file core.clj
 
(ns orientweb.core)
(require '[clj-orient.core :as orientcore]
            '[clj-orient.graph :as orientgraph])
 
(orientcore/set-db! (orientgraph/open-graph-db! "remote:localhost/demo" "admin" "admin"))

(def mydb (orientcore/db-info orientcore/*db*))

(defn -main 
   [& args]
   (println "Information about the database:")
   (doseq [[key val] mydb] (prn key val))
   (orientcore/close-db!))

6. Hasil eksekusi:
 
[bpdp@bpdp-arch orientweb]$ lein compile
Compiling orientweb.core
Compilation succeeded.
[bpdp@bpdp-arch orientweb]$ lein run
All namespaces already :aot compiled
Information about the database:
:name "demo"
:url "remote:localhost/demo"
:status "OPEN"
:user #
[bpdp@bpdp-arch orientweb]$ 

Happy hacking!

Juli 13, 2012

Utilizing Leiningen Profile

In my previous post about setting up Emacs for Clojure development, I suggest to include lein-swank inside the plugins' project.clj. Eventhough this approach work, here I will explain how to use Leiningen profile to separate the plugins / libraries which will act as helper and with other plugins / libraries which will be included in the project result. You will find that separation of concerns is beatiful.

Ok, now here's the problem. I would like to use lein-swank for my development with Emacs but I don't want to interfere my project with all of those clutters, I just want my project to include my source code and my libraries. This is where Leiningen profile comes into play. To this end, use profile.clj inside Leiningen home (that is $HOME/.lein/). The default profile which will be executed is "user". Here is mine:

[bpdp@bpdp-arch ~]$ tree .lein/
.lein/
|-- profiles.clj
`-- self-installs   
     |-- leiningen-1.7.1-standalone.jar   
      `-- leiningen-2.0.0-preview7-standalone.jar
 
1 directory, 3 files[bpdp@bpdp-arch ~]$ 


Here's the profile.clj content:

[bpdp@bpdp-arch .lein]$ cat profiles.clj 
{:user {:plugins [[lein-swank "1.4.4"]
                         [lein-pprint "1.1.1"]]}}
[bpdp@bpdp-arch .lein]$ 


This way, you can - for example - execute lein-swank without include them in your project.clj anymore. You may add other plugins. I just use lein-pprint in the meantime. There are still many things that can be done using this profile, for example, you can use this profile to test multiversion plugins / libraries, etc. I will write them later.

Happy hacking!

Juli 12, 2012

Using Emacs for Clojure Development

There are many (confusing) blog posts about using Emacs for Clojure development but I promise you not to make you confuse a bit more (hopefully. :-D). Since I use Clojure for my dissertation and usually Clojurians use Emacs (for obvious reasons), then here I come.

There are some steps to do to prepare Emacs. I use Linux (Arch Linux) here, so, probably it won't be applicable for other OS, but this usually the same with other Linux distros. Some software that needs to be installed properly before are JDK, Clojure, Leiningen (I use version 2), and Emacs (version 24, 23 is okay but you have to prepare package management by yourself and it's quite tricky). Those are already explained properly at each of the website, so I will not repeat it again here. Basically, there are one-time-only setup procedures below:

  1. Setup Emacs package management
  2. Install clojure-mode, swank-clojure, slime and slime-repl
  3. Prepare syntax highlighting
When they are all installed and configured successfully, then the project and source code should be configured  to include swank-clojure.
  1. Prepare project: source code and dependencies.
After all of those steps, then all you have to do is coding. The details follow.

Setup Emacs Package Management
In Linux, usually all of those Emacs file for user will reside in $HOME/.emacs.d/. Inside, there is a file - init.el - which is used to store Lisp script for Emacs configuration. To setup Emacs package management, put this into init.el:


(require 'package)
(setq package-archives '(("ELPA" . "http://tromey.com/elpa/")
    ("gnu" . "http://elpa.gnu.org/packages/")
    ("marmalade" . "http://marmalade-repo.org/packages/")))
(package-initialize)

Those lines are used to let Emacs know about the package archives / repositories (ELPA, gnu, and marmalade) then activate package management. If you edit the init.el file from Emacs, you may activate them by using M-x eval-current-buffer when you are in init.el buffer. If you are confuse, just exit from Emacs (C-x C-c) then fire up Emacs again.

Install clojure-mode, swank-clojure, slime and slime-repl
SLIME (The Superior Lisp Interaction Mode for Emacs) and SLIME-REPL are package to interact with Lisp, especially useful for coding (code evaluation, completion, /etc). In this post, we will install this and make it interact with swank for the benefit of coding in Clojure inside Emacs. Here is how to install them:
  1. M-x package-list-packages, it will access package repositories and show them inside buffer.
  2. Find clojure-mode, swank-clojure, slime, and slime-repl on the list. In each of package, click on the name, then click on "Install" (yes, you can also use your keyboad: put cursor on the name, enter, then "C-x o" to go to the description window below, put cursor on "Install" then RET (or you may call it Enter key).

Prepare Syntax Highlighting
To activate clojure-mode and make all color output, put this into init.el:
;; these for Clojure, activate then turn on 
(require 'clojure-mode)
(defun turn-on-paredit () (paredit-mode 1))
(add-hook 'clojure-mode-hook 'turn-on-paredit)

;; syntax highlighting for swank
(add-hook 'slime-repl-mode-hook
   (defun clojure-mode-slime-font-lock ()
     (require 'clojure-mode)
     (let (font-lock-mode)
       (clojure-mode-font-lock-setup))))
;;
And that's all for setting up Clojure development environment in Emacs. The following detail is only one-time whenever you want to create new project.

Prepare Project Source Code and Dependencies
Using Leiningen, use this command (my Leiningen script name is "lein2", by default, if you follow installation instruction in Leiningen website, this script name should be "lein". I use "lein2" because I have 2 Leiningen alive: "lein" for stable version - 1.x and "lein2" for development version - 2.x):

[bpdp@bpdp-arch clojure]$ lein2 new zax
Generating a project called zax based on the 'default' template.
To see other templates (app, lein plugin, etc), try `lein help new`.
[bpdp@bpdp-arch clojure]$ tree zax/
zax/
|-- README.md
|-- doc
|   `-- intro.md
|-- project.clj
|-- src
|   `-- zax
|       `-- core.clj
|-- target
|   |-- classes
|   `-- stale
|       `-- dependencies
`-- test
    `-- zax
        `-- core_test.clj

8 directories, 6 files
[bpdp@bpdp-arch clojure]$ 
Edit project.clj to include lein-swank plugin:
[bpdp@bpdp-arch zax]$ cat project.clj 
(defproject zax "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.4.0"]]
              :plugins [[lein-swank "1.4.4"]])
[bpdp@bpdp-arch zax]$ 
That's all for project and source code preparation. Now, whenever you want to go crazy with Emacs and Clojure, activate swank server and have it connected from inside Emacs. This step below will show you the details:

Activate swank server
Use this command from shell:

[bpdp@bpdp-arch zax]$ lein2 swank 4005 localhost :colors? true 
Listening for transport dt_socket at address: 50069
Connection opened on localhost port 4005.
From inside Emacs, you may connect to this process by using "M-x slime-connect". Emacs will ask you the host and port where swank live: 

Host: 127.0.0.1
Port: 4005

RET to accept the default value. Emacs will activate SLIME:


The statement "Connected. Your hacking starts... NOW!" probably different with yours. It will be displayed randomly (some will make you laugh or at least smile). Whenever we have source code in Clojure, we can evaluate or do many things (see swank-clojure README). Here I just want to give example of evaluating current buffer. Put the source code into the new buffer, here's the example:

(defn relay [x i]
  (when (:next x)
    (send (:next x) relay i))
  (when (and (zero? i) (:report-queue x))
    (.put (:report-queue x) i))
  x)
 
(defn run [m n]
  (let [q (new java.util.concurrent.SynchronousQueue)
        hd (reduce (fn [next _] (agent {:next next}))
                   (agent {:report-queue q}) (range (dec m)))]
    (doseq [i (reverse (range n))]
      (send hd relay i))
    (.take q)))
 
(time (run 1000 1000))

(print "It's the end of the beginning")
To evaluate and run, go to the buffer, then do "C-c C-k". Here's the result:


The result will be showed in REPL:


That's all. happy hacking!