4Morefun

Everything between sky and MS-Dos

Compile and run Java code from sublimetext 2

Step one; Create a batch file

Create a batch file that compiles and run java code.

@echo off

rem Save the filename in a variable
set file=%1

rem Compile
javac %file%

rem Run the file (but remove the .java extension (:~0,-5)) with it's arguments (%*). 
java %file:~0,-5% %*

rem Delete the class file (I want my folders clean)
del %file:~0,-5%.class

Save this in your PATH as javar.bat.

Step two; Create a build system

Tools -> build system -> new build system

Paste in this:

{
    "cmd": ["cmd","/c","start cmd \"/c javar $file_name & pause\""]
    "file_regex": ".java$"
}

Step three; Test it!

Create a Java file, write some java code, save as something.java. Press ctrl+b and the result will be printed in a new CMD window.

enter image description here

Clojure – Get command line arguments

After you have created the factorial function and maybe other apps you will realize that it would be nice if you could pass arguments directly via the command line to the script before you run it. Then your script would be a bit more dynamic and useful. This is how you do it:

Some-file.clj

Called with X:\clojure> clojure some-file.clj Hello Sony? 123

(println (nth *command-line-args* 0)) ;; Prints "Hello"
(println (nth *command-line-args* 1)) ;; Prints "Sony?"
(println (nth *command-line-args* 1)) ;; Prints "123" Note: The numbers will be a string.

But it’s a bit much to write, what if we just could write (println (arg 1)) to get the first command line argument. That would be more readable.

(defn arg [x]
    (nth *command-line-args* (dec x) nil))

Now we can get the arguments like this:

(defn arg [x]
    (nth *command-line-args* (dec x) nil))

(println (arg 1)) ;; Prints "Hello"
(println (arg 2)) ;; Prints "Sony?"
(println (arg 3)) ;; Prints "123" Note: The numbers will be a string.
(println (arg 4)) ;; Prints nil, because we haven't passed a fourth argument to the script.

That was a quick tip for now. If you want more smart functions that make clojure programming easier you should check out my github repo, easy clojure.

Btw this post is a little SEO experiment, hence the unnecessarily bold text and the a bit strange title. Want to see if it will affect the google results.

From PHP to Clojure, a beginners guide – Factorial

In this post I’ll compare a recursive factorial function written in PHP and another written in Clojure.

Recursive Factorial

PHP

/**
 * Calculate the factorial of n
 */
function factorial($n) {                 //Define a function, takes one argument, n.
    if($n == 1)
        return 1;                        // IF, do this
    else 
        return $n * factorial(($n - 1)); // ELSE, do this
}

echo factorial(5) . PHP_EOL; //120

Clojure

(defn factorial [n]                   ;; Define a function, takes one argument, n.
    "Calculate the factorial of n"    ;; This is like PHPDOC
    (if (= n 1)
        1                             ;; IF, do this
        (* n (factorial (- n 1)))))   ;; ELSE, do this

(println (factorial 5)) ;; 120

The most interesting thing here are Clojures ifs. The if is a function that take three arguments:

  1. The condition. ((= n 1)
  2. The expression to execute if the condition is true. 1 (return 1; in php.)
  3. The expression to execute if the condition is false. (* n (factorial (- n 1)))

If we had written this function in PHP(+5.3.0) it would be something like this:

function my_if($condition, $ifTrue, $ifFalse) {
    //...
}

my_if("$n == 1",
    function() {
        return 1;
    },
    function() {
        return $n * factorial(($n - 1));
    }
}

Practice time

Take some time and try to write the factorial functions by your self. Or try to write a summation function.

You will probably sooner or later get the error “… Too many arguments to if, …”. This happens when you write code something like this:

(defn faculty [n]
    "Calculate the faculty of n"
    (if (= n 1)                                                         ;; First argument, the condition
        1                                                               ;; Do this if true
        (println "Debug N, to be sure I'm doing this shit right: ", n)  ;; Do this if false
        (* n (faculty (- n 1)))))                                       ;; Do this if... wait! Wtf? Get it? This is a fourth argument, one argument too much.

The problem here is that you now have passed four arguments to the if, the function if can only take three arguments. If you don’t understand look at the PHP code and compare. Anyway to solve this problem you can wrap code in something called do.

The new code:

(defn faculty [n]
    "Calculate the faculty of n"
    (if (= n 1)                                                         ;; First argument, the condition
        1                                                               ;; Do this if true
        (do                                                             ;; Do this if false:
            (println "Debug N, to be sure I'm doing this shit right: ", n)  ;; I'm in the 'do' block!
            (* n (faculty (- n 1))))))                                      ;; Me too!

Okay, that’s everything for now. Hope you have learned something new. Bai!