New personal site

After years thinking about it, finally I’ve done it: I’ve registered a custom domain (a simple URL redirect) and I’ve create an app on the popular cloud hosting site Heroku: it uses the new Clojure support feature and the new (and cool) Clojure mini MVC Framework Noir.

The site is a bit slow the first time you visit it, because the app is pushed out of cache after a period of inactivity, but man, it’s free!
I really like the site layout (and Jquery is too cool). What do you think about it? Let me know! Ah, here is the url, visit it!

www.alfredodinapoli.com

On the future of clj3D

Hi to everyone,
I know that in more than a month, no new post have appeared on this blog (I apologies for this), but I’ve been busy with some cool university projects.
I’ve slowed down the developing of clj3D, for one main reason: the library needs a geometric kernel.
A geometric kernel ensure that for every shape you’ll draw on the screen, there will be a structure/data/objects representing it. Furthermore, we need a hierarchical structure, like an Hasse diagram. I will begin developing the geometric kernel as soon as possible, meanwhile be patient and explore the already-coded part of clj3D.
Bye!
Alfredo

clj3D: The first (serious as a Monkey) Clojure 3D library

As we should know, a language can’t be neither omni-comprehensive nor usable in every situation. There are some kind of areas (e.g. graphics and speed-critical software) where Java sucks badly. Yeah, there are some interesting projects but they aren’t nearly comparable to the C or C++ ones. The world of 3D graphics is very fascinating and it’s my primary goal trying to find a satisfying job into it. But, what all this stuff have to do with Clojure?
Well, a Clojure flaw was a lack of 3D graphics library. I’m not talking about idiomatic wrappers (like the awesome penumbra) around OpenGL calls, since these kind of library are too low level (in my opinion). We need something to  easily create and transform 2D and 3D objects using some solid foundations.

In my free time I’m working on this kind of library, my project was called clj-3D (not too much original, I know :D). As the documentation says “Clj3D is a Clojure graphic library for manipulating 3D and 2D objects. It aims to be the Clojure standard in 3D and 2D rendering”: I think that every attempt to enrich the awesome language Clojure is, worth the effort.
Clj3D mesh together to worlds: the former is the JMonkeyEngine, a wonderful project that is trying to make Java less horrible into 3D Graphic field; the latter is PLaSM, a geometric language developed within my university.

With clj3D you are able to display something with few lines of code: you can write your own 3D models and display it directly with clj3D! Check this out:

(use '(clj3D fenvs viewer) :reload)
(view (cube 1))

This display a light gray 1x1x1 cube. clj3D is under massive development, so only few subsets of PLaSM and JMonkey have been wrapped/coded. I hope that this project will be embraced by the Clojure community, and made better as well. For the usage and installation the github page is always “work in progress”, but you can use leiningen to satisfy the dependencies and for compile the java extra classed needed. Actually clj3D is tested only under Mac OS X, so feedbacks are welcome! I will push monthly releases on Clojars, so stay tuned!

Bye!

Alfredo

A bit of Clojure magic and high order functions are served

Hi guys, what’s up?

I’m a little busy right now. I’ve started what is probably going to be my last semester of university courses and I’ve started coding a Clojure 3D library (coming soon on these screens). But this is the good time to take a break and write something on this blog. A really cool (and useful) features of Haskell is the ability to use High Order Functions. Take a look to this simple Haskell snippet:

> :t take
take :: Int -> [a] -> [a]

> :t take 10
take 10 :: [a] -> [a]

> :t take 10 ['a'..'z']
take 10 (enumFromTo 'a' 'z') :: [Char]

As you can see the arguments are “eaten” one by one in a way that remember the dear old Pacman. In a nutshell take 10 returns a function that applied to a sequence return a result. Simple enough, isn’t it?
Unfortunately this doesn’t happens by default in Clojure. Observe this little function:

(defn sum [a b] (+ a b))

If you’ll try to invoke

(sum 1)

you we’ll get the following:

Wrong number of args (1) passed to: user$sum

This is absolutely normal, since it is the Clojure default behaviour. Googling and Stack-overflowing around a bit, I came up to a simple snippet to implement with a macro the required behaviour:

(defmacro defn-decorated
  "like defn except it accepts an additional vector of
   decorator functions which will be applied to the base definition.
   the decorators are applied in left-to-right order."
  {:author "Robert McIntyre"
   :arglists '[[name [modifers*] doc-string? attr-map? [params*] body]
	       [name [modifers*] doc-string? attr-map? ([params*] body) + attr-map?]]}
  [fn-name decorators & defn-stuff]
  `(do
     (defn ~fn-name ~@defn-stuff)
     (alter-var-root (var ~fn-name) (reduce comp identity (reverse ~decorators)))
     (var ~fn-name)))

(defn curry**
  [number-of-args f]
  (fn
    ([& args]
       (let [number-of-inputs (count args)]
	 (if (= number-of-inputs number-of-args)
	   (apply f args)
	   (curry** (- number-of-args number-of-inputs)
		    (apply (partial+ partial+ f) args)))))))

(def curry* (curry** 2 curry**))

(By the way you can find the complete clj file here.) Here’s an example of application:

(defn-decorated sum [(curry 2)] [a b] (+ a b))

But a thing I didn’t like was the explicit passing of the number of argument which the function is composed, so I wrote my own macro:

(defmacro defn-ho
  [fn-name & defn-stuff]
  (let [number-of-args (count (first defn-stuff))]
    `(defn-decorated ~fn-name [(curry* ~number-of-args)] ~@defn-stuff)))

With this simple add-on, you can easily write any kind of high order function in a way like this:

(defn-ho new-sum [a b c] (+ a b c))

I haven’t tried it in particular situations (e.g. with destructuring) but don’t expect it to work with function with multiple arity: indeed, but really  you will need to create n-arity high order function??

Feedback are welcome, as always!

Cheers!

Alfredo

Alfredo the (Android) Clojurist

In the last post, I was just wondering how it could be possible to write a simple Clojure-based Android app. Well, I’ve succeeded, so I want to share this simple experiment with you. The main idea behind this weird experiment is that  you can import a java .jar into your Android application, then call any public-exposed method within your Android app: due to the fact that Leningen allows you to export a Clojure project into a STANDALONE jar, who prevent you to use all this awesomeness??

Download the Android SDK

In order to develop full-fledged Android apps you need the Android SDK, you can download it from here (I really like the site’s graphics). Once you have chosen your favourite platform, unzip che SDK and run the android executable (you can find it into the bin directory). The scope of this post is not teaching you how to install the Android SDK, so I’ll move further 🙂

Setting the environment

Although you can use your favourite IDE or editor, I really like IntelliJIdea, cause it provides Clojure as well as Android support. You can easily install LaClojure plugin from Idea’s plugin manager.

Another Yet Greeter out there

Which is the dumbest program you can create? We’ll create a simple program that greet you (wow, cool!). Create a Leiningen project as usual:

lein new greeter

Then write this into your core.clj file:

(ns greeter.core
  (:gen-class
    :methods [#^{:static true} [greet [String] String]]))

(defn -main [&args] )

(defn greet [param]
  (str "Hello, " param))

(defn -greet
  "Java wrapper around greet method"
  [param]
  (greet param))

The code is pretty straightforward, just two observation:

  • You can expose a method to outside with the minus before the function/method name (e.g. -greet)
  • You must specify the Javish signature for your metod, see the line under :gen-class

Now we can easily export this project as a standalone .jar (remember to set the :main method into project.clj!!):

lein uberjar greeter

Now we are ready to embed our greeter into an Android project.

Final step

Now we are going to create a new Android project. For the brevity sake, I suggest you to use an IDE.
In IntelliJ Idea you can create a new Android project from stratch: (File -> New Project -> Android Module). Leave all the options to default. Now open MyActivity.java and you should see something like this:

package com.example;

import android.app.Activity;
import android.os.Bundle;

public class MyActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Now we have to import out greeter standalone, so right click on the name of project in the project panel and click on “Open Module settings”, go to the “Dependencies” tab and add your brand-new standalone: greeter-1.0-SNAPSHOT.jar. Apply and quit.

As the last step, we have to hack a little MyActivity.java code, in order to greet using our greet method:

package com.example;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import greeter.core;

public class MyActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        TextView tv = new TextView(this);
        tv.setText(core.greet("Alfredo the Clojurist"));
        setContentView(tv);
    }
}

Now run the project (you have to configure down the emulator, I don’t think it will be too difficult) and BAM!:

This is only the tip of the iceberg. Stay tuned for other weird experiments. The Frankestein era has begun!

Towards a new era?

Few days ago, my iPhone 3G smashed on the ground, suddenly showing the white screen of death.
I’ve spent several nights thinking about it, then I’ve decided switching to an Android phone. My choice was an LG Optimus One. This morning I’ve purchased it and I’m very satisfied.

The coolest thing of all this stuff is that Android is Java based, so who prevents me to write my Clojure code, packing it into a jar and then importing this jar into an Android Project?
I’m looking forward to play with the Android SDK and Clojure.

Stay tuned.