Blogged by Ujihisa. Standard methods of programming and thoughts including Clojure, Vim, LLVM, Haskell, Ruby and Mathematics written by a Japanese programmer. github/ujihisa

Sunday, September 16, 2012

Freeciv on Mac OS X

If you haven't play Freeciv, it's a good opportunity to try it. It's a freesoftware which is based on Civilization™. I've been playing Freeciv recently.

It's very easy to install Freeciv on Gentoo Linux: just emerge it. It's not very easy to install it on Mac OS X Snow Leopard.

There are 3 ways to install Freeciv on Mac OS X Snow Leopard. (1) 2.1.9 binary package for Snow Leopard, (2) 2.3.1 with sdl on Homebrew's Homebrew-games, or (3) jinhao's patched version of Homebrew formula. Dont choose (1) or (2).

Freeciv has 2 ways to provide GUI interface; sdl and gtk2. sdl interface is very inconvenient. gtk2 interface is much much better but it needs lots of compiling. Still I recommend gtk2 version for everybody.

Note that don't start Freeciv server on Mac OS X. I don't know why but it consumes all the CPU power. Singleplay in Freeciv simply means it starts server and connects itself. Start a server on a Gentoo machine and connect there to play.

Enjoy!

Sunday, September 9, 2012

English Words Lookup/Completion on Gentoo

When I used Mac OS X, I used look command a lot.

$ look standa
standage
standard
standardbred
standardizable
standardization
standardize
standardized
standardizer
standardwise

A Vim plugin neco-look makes your Vim auto-complete English words, using look command internally.

I had been looking for how to use the look command on Gentoo Linux. I finally found that how to do that today.. just install the following packages.

  • sys-apps/util-linux
  • sys-apps/miscfiles

I used e-file command to find portage packages which depend on look command and its dependency /usr/share/dict/words. Install app-portage/pfl to use e-file.

Friday, September 7, 2012

DNS Lookup in Scala

import javax.naming.directory.{InitialDirContext, Attribute}
import javax.naming.NamingException
import scala.collection.JavaConversions._

object L {
    def main(args: Array[String]) {
        println('ip, lookupIp(args.head))
    }

    def lookupIp(host: String): List[String] = {
        val attributes = try {
            new InitialDirContext getAttributes ("dns:/%s" format host)
        } catch {
            case _: NamingException => return Nil
        }
        val list = {
            val attributeEnumeration = attributes.getAll
            var list = List[Attribute]()
            while (attributeEnumeration.hasMore)
                list = attributeEnumeration.next :: list
            attributeEnumeration.close
            list.reverse
        }
        list map (x => x.getID -> x.get.toString) flatMap {
            case ("A", x) => List(x)
            case ("CNAME", x) => lookupIp(x)
            case (_, x) => Nil
        }
    }
}

Sunday, September 2, 2012

Minecraft MOD in Clojure

http://github.com/ujihisa/cloft

I've been writing Clojure for this Minecraft server MOD. This MOD, cloft, uses Bukkit API and is compatible with Minecraft client version 1.3.1+, including the latest stable version 1.3.2.

This MOD was originally based on clj-minecraft made by Deon Moolman.

I'm also running my own minecraft server with using cloft. If you are interested in playing, feel free to ask me on Twitter.

Wednesday, July 4, 2012

ZeroMQ Versions and Bindings

ZeroMQ has two major versions; 2.x.y and 3.x.y. Even though the latter is still release candidate, people are already using it. The problem is that these two versions don't have compatibility. If one system uses version 3.x.y, whole other systems which talk to the system have to use 3.x.y.

There are some binding libraries for each programming languages. Note that some of them don't support one of the versions. The below is the list as of today.

  • Haskell
    • zeromq-haskell: 2.1.x
    • zeromq-haskell3: 3.1.x
    • just use cabal to install them
  • Clojure
    • (Not directly for Clojure but for JVM languages in general)
    • jzmq: both 2.x.y and 3.x.y
    • there are no official clojar/maven/apt/portage packages. one for gentoo is here
  • Scala
    • zeromq-scala-binding: 2.1.x
      • (akka-zeromq uses it)
    • jzmq: both 2.x.y and 3.x.y
      • (ditto in Clojure)
  • Ruby
    • rbzmq: 2.x.y
  • Python
    • pyzmq: 2.x.x (experimentally 3.x.x)

Use Haskell, Clojure, Scala or Python to use ZeroMQ even if you use zeromq version 2 just in case at least one system uses version 3. If you need to use Ruby, just use JRuby to use jzmq. It's not very easy to install jzmq, but if you are using Gentoo, it's easy.

Wednesday, May 9, 2012

Octal or not Octal

A number literal with begins with 0 differs between programming languages. For example, a programming language considers 015 as 13, or as 15 just by ignoring the first 0, or even throws an error.

I categorized some programming languages by the view.

  • 015 == 13
    • Clojure
    • Scala
    • Java
    • Ruby
    • C
    • Vim script
    • JavaScript
  • 015 == 15
    • Haskell
    • Scheme
    • Emacs Lisp
    • Lua
    • Common Lisp
  • Exception
    • Python
    • CoffeeScript

Monday, April 2, 2012

Concurrent PI-Calculator in Clojure

Inspired by a tutorial about akka tutorial in Scala about making a concurrent PI calculator, I made similar on in Clojure without actor model but just with thread and atom.

(ns pi.core
  (:import [java.util.concurrent Executors])
  (:gen-class))

(def sum (partial reduce +))

(defn f [n]
  (/ (if (even? n) 1 -1) (inc (* 2 n))))

(defn solver1 [n]
  (double (* 4 (sum (map f (range 0 n))))))

(defn solver2 [n]
  (def a (atom 0))
  (def tasks
    (let [unit (/ n 4)]
      (for [m (range 0 4)]
        (fn []
          (swap! a (partial + (sum (map f (range (* unit m) (* unit (inc m)))))))))))

  (let [pool (Executors/newFixedThreadPool (count tasks))]
    (doseq [f (.invokeAll pool tasks)]
      (.get f))
    (.shutdown pool))
  (double (* 4 @a)))

(defn -main []
  (let [n 2000]
    (prn (solver1 n))
    (prn (solver2 n))))

Commenting out the line of solver1/solver2, I got the following results on my 2-core with hyper-threading machine; virtually it's 4 core machine.

solver1

real 3.80
user 4.66
sys 0.09
3.141092653621043

solver2

real 2.01
user 3.44
sys 0.05
3.141092653621043

Comparing to the sequential version, the concurrent version was almost 2 times faster.

Followers