Plan9 installing Perl

Installing perl on plan9 based on instructions from “Juan M. Mendez”

term% 9fs sources; ls -l /n/sources/extra

--rw-rw-r-- M 23 rsc sys 14261804 Nov 25 2002 perl.iso.bz2 <-----
--rw-rw-r-- M 23 rsc sys 9404968 Sep 15 2003 pgw.tar.bz2
--rw-rw-r-- M 23 sys sys 28133333 Oct 23 03:04 plan9.tar.bz2
--rw-rw-r-- M 23 rsc sys 47744 Sep 14 2005 postmark.c
--rw-rw-r-- M 23 rsc sys 40841 May 1 2002 pq.tgz
--rwxrwxr-x M 23 rsc sys 640 Nov 3 2004 ps2txt
--rw-rw-r-- M 23 rsc sys 4964315 Nov 16 2002 python.iso.bz2

To install it, I did the following:

term% cp /n/sources/extra/perl.iso.bz2 /usr/glenda
term% cd /usr/glenda
term% bunzip2 perl.iso.bz2
term% 9660srv
term% mount /srv/9660 /n/dist /usr/glenda/perl.iso # full path required
term% mkdir perl
term% dircp /n/dist /usr/glenda/perl/dist

# to prevent “file not found” errors during the replica command, below.
term% >>/dist/replica/client/perl.db
term% >>/dist/replica/client/perl.log

Now, modify /usr/glenda/perl/dist/perl to look like this:


#!/bin/rc

s=/usr/glenda/perl/dist
serverroot=$s
serverlog=$s/perl.log
serverproto=$s/perl.proto
fn servermount { status='' }
fn serverupdate { status='' }

#fn clientmount { 9fs kfs }
c=/dist/replica
clientroot=/
clientproto=/sys/src/cmd/perl/perl.proto
clientdb=$c/client/perl.db
clientexclude=(dist/replica/client)
clientlog=$c/client/perl.log

Now you can do a pull.


term% replica/pull /usr/glenda/perl/dist/perl

Applescript terminal monitoring

The following script monitors the contents of your terminal window, and lets you know if it stops changing.

-- Monitors terminal window contents.
-- If contents do not change, assumes simulation is complete.

set voice_name to "Serena"

say "[[emph +]]I am monitoring the [[emph +]]simulation.  I will let you know when it is completed." speaking rate 200 using voice_name

tell application "Terminal"
	activate
	set old_contents to contents of window 1 as text
end tell

repeat
	delay 5
	tell application "Terminal"
		activate
		set current_contents to contents of window 1 as text
	end tell

	if current_contents = old_contents then
		exit repeat
	else
		say "The simulation is still [[rate 210]]running" speaking rate 200 using voice_name
	end if

	set old_contents to current_contents
end repeat

say "Simulation [[rate 130]]complete." speaking rate 200 using voice_name

Reading keys

Here is how to read a key from the keyboard in Scala:

object Key extends App {
	val con = new tools.jline.console.ConsoleReader()
	println(con.readVirtualKey())
}

and in Python:

def getch():
	import sys, tty, termios
	fd = sys.stdin.fileno()
	old = termios.tcgetattr(fd)
	try:
		tty.setraw(fd)
		return sys.stdin.read(1)
	finally:
		termios.tcsetattr(fd, termios.TCSADRAIN, old)

if __name__ == '__main__':
	print "got ",getch()

Representing a case class as a JSON String

The following shows how to serialize a case class into a JSON string. This is useful if you would like to have a compatible textual representation of a simple scala case class object — for example, here, a Person(name:String, age:Int, city:String).

object cc extends App {

    case class Person(name:String, age:Int, city:String)
    val p = new Person("Patryk", 34, "Baltimore")
    println(mapToJson(caseClassToMap(p)))

    def caseClassToMap(cc: AnyRef) =
        (Map[String, Any]() /: cc.getClass.getDeclaredFields) {(a, f) =>
        f.setAccessible(true)
        a + (f.getName -> f.get(cc))
    }

    def mapToJson(x:Map[String,Any]):String= {
        ("{" + (x collect { case (key,value) => { "\"" + key + "\" : \"" +      value + "\"," } }).mkString + "}").replace(",}", "}")
    }

}

Counting instances of words in text

object wc extends App {

    val text = "This is some text with some repeated repeated words words words"
    println(countsOf(text))

    def countsOf(text:String):Map[String,Int]= {
       var result:Map[String,Int] = Map()

       (text split " ") foreach (thisWord => {
           val oldCount = if (result contains thisWord) result(thisWord) else 0;
           result += (thisWord -> (1 + oldCount))
       })

       result
    }
}

A minimal web server in Scala

Here is a minimalist web server in scala.

import java.net._
import java.io._
import scala.util.control.Breaks._

object Web2 extends App {
val BLANK_LINE_AT_END_OF_REQUEST = 1
val webServer = new Thread("Web Server") {
  def response(text: String, code: String = "200 OK") =
    """HTTP/1.0 """ + code + """
  Content-Type: text/html
  Content-Length: """ + text.length + """

  """ + text

  override def run {
    for(port <- List(8888)) {
      val ss = new ServerSocket(port)
      while (true) try {
        val s = ss.accept
        try {
            breakable {
            for (line <- io.Source.fromInputStream(s.getInputStream).getLines) {
                    println(line)
                    if (line.length < BLANK_LINE_AT_END_OF_REQUEST) break
                }
            }
            s.getOutputStream.write(response("Yes hello!").getBytes)
        } finally { s.close }
      } catch { case ie: InterruptedException => return }
    }
  }
}

webServer.start();

}

Plotting in R from Scala

It’s really useful to be able to make high-quality plots from code. Here, I’m plotting in R from Scala.

To use this, you must get REngine.jar and RserveEngine.jar from the Rserve website and place them on your classpath. You must also start R and start Rserve.

import org.rosuda.REngine._;
import org.rosuda.REngine.Rserve._;
import java.awt._;

object A1 extends App {
    val c = new RConnection();

    c.eval("png('myplot.png',width=512, height=512)");
    c.eval("plot(seq(-10,10,1), seq(-10,10,1)^2); dev.off();");
    val imgData = c.eval("r=readBin('myplot.png','raw',512*512); unlink('myplot.png'); r");
    c.close();

    val img     = java.awt.Toolkit.getDefaultToolkit().createImage(imgData.asBytes());
    val imgIcon = new javax.swing.ImageIcon(img);
    val label = new javax.swing.JLabel(imgIcon);
    val frame   = new javax.swing.JFrame("Test image");
    frame.add(label); frame.pack(); frame.setVisible(true);

}

Extracting words from strings using pattern matching

Suppose you might want to match a sentence or a piece of XML and change a value in each matching string. You can do this using regular expressions, case, and match statements. You can identify the matching strings and extract out the parts that you want. Here’s a little example that finds sentences matching “a big X” where X can be anything.

Note that first I use filter to find the sentences matching the pattern. Then later, I use collect to actually get the wildcard words from the matches.

object Simple1 extends App {

    val data = List(
       "a big apple",
       "a big chair",
       "a little grape",
       "a little table"
   );

    val target = "a big (.*)".r;

    val bigItemSentences = data filter {
        case (anything) =>
            anything match {
                case target(matchedBigItem) => true
                case x => false
            }
    }
    println("Sentences with big items: " + bigItemSentences);

    val bigItems = data collect {
        case target(matchedBigItem) => matchedBigItem;
    }
    println("The big items were: "  + bigItems);
}

The result of running this is:

Sentences with big items: List(a big apple, a big chair)
The big items were: List(apple, chair)

Reading CSV data into associative arrays in Scala

Many data files that we want to access are in CSV format.  In analysis programs, it’s great to be able to access each row as an associative array.  For example, suppose your CSV file looked like this:

name,age,RT
Mercury,25,220
Jupiter,33,230
Apollo,52,260

We would like a nice API to pull out response times (RT) from a column named RT. For example, it would be great to write code like:

  val rows = readCSVFile("mydata.txt")
  rows foreach (row => {
    println(row("RT"))
  })

 

The following code defines an associative array and the corresponding readCSVFile function that you can use in this manner:

type AssociativeArray = Map[String,String]

def readCSVFile(filename:String):List[AssociativeArray]=
{
  val csv = (io.Source.fromFile(filename)
    getLines()
    map (line => line.split(",") toList)
    toList)

  val headers = csv head
  val data = csv tail

  data map (row => (headers zip row).toMap)
}

Learning from Reinforcement in Scala

Here is some minimal code that implements Reinforcement Learning (dynamic programming using Value Iteration) for a grid world problem. Including comments, it’s less than 100 lines of code. 

The code could be modified to use temporal-difference learning with a few small changes.

// -----------------------------------------------------------
// Minimal Reinforcement Learning in Scala
// -----------------------------------------------------------
// Problem:   GridWorld with a goal terminal state
// Algorithm: Value Iteration
// Storage:   Lookup table
// -----------------------------------------------------------
// Patryk Laurent (http://pakl.net)
// -----------------------------------------------------------

object SimpleRL extends App
{
    case class State(x:Int, y:Int)
    case class Action(deltaX:Int, deltaY:Int)

    val maxX  = 10; val maxY  = 10;
    val world = for (i <- 0 to maxX; j <- 0 to maxY) yield new State(i,j);
    val goal  = new State(0,0)

    val north = new Action( 0,-1);
    val south = new Action( 0, 1);
    val east  = new Action( 1, 0);
    val west  = new Action(-1, 0);
    val possibleActions = List(north,south,east,west);

    val epsilon                      = 0.8;
    val discountFactor               = 1.0;
    val initialValue                 = 0.0;
    var vf:   Map[State,Double]      = Map(world map (eachLocation => (eachLocation, initialValue)) : _*);
    var newvf:Map[State,Double]      = Map(world map (eachLocation => (eachLocation, initialValue)) : _*);

    def rf(arrivedAt:State)          = { -1.0 }  // for each move
    def nextState(s:State,a:Action)  = { new State(s.x+a.deltaX, s.y+a.deltaY) }
    def outcomeFor(s:State,a:Action) = { rf(nextState(s,a)) + discountFactor * vf(nextState(s,a)) }
    def isTerminal(s:State):Boolean  = { if (s == State(0,0)) return true; else return false; }
    def isAllowed(s:State,a:Action):Boolean= { // Disallow movements outside the grid
        val next = nextState(s,a);
        if (next.x < 0 || next.x > maxX) return false;
        if (next.y < 0 || next.y > maxY) return false;
        return true;
    }

    // -----------------------------------------------------------
    // Perform Value iteration from each non-terminal state
    // -----------------------------------------------------------
    (1 to 10000).seq.foreach(trial => {
        (world filter (s => !isTerminal(s))) foreach (s => {
                val possible   = possibleActions filter (a => isAllowed(s,a));
                val bestAction = possible.maxBy(outcomeFor(s,_));
                val delta      = outcomeFor(s, bestAction) - vf(s);
                newvf         += s -> (vf(s)+epsilon*delta);
        })
        val temp = vf; vf = newvf; newvf = temp;  // Swap
    })

    // -----------------------------------------------------------
    // Display value function
    // -----------------------------------------------------------
    println("Learned value function (values for for each (x,y) in the grid):");
    for (x <- 0 to maxX) {
        for (y <- 0 to maxY) {
            print(vf(new State(x,y)) + " ");
        }
        println("");
    }

    // -----------------------------------------------------------
    // Define how to make a policy from a value function.
    // -----------------------------------------------------------
    def policy(world:List[State], vf:Map[State,Double], possibleActions:List[Action])=
    {
        var result:Map[State,Action] = Map()
        val nonTerminals = world filter (s => !isTerminal(s));
        nonTerminals foreach (s => {
            val allowedActions = possibleActions filter (a => isAllowed(s,a));
            val bestAction     = allowedActions.maxBy(outcomeFor(s,_));
            result            += (s -> bestAction)
        })
        result
    }

    val optimalPolicy = policy(world.toList, vf, possibleActions.toList);
    println("Policy Demo: 'From 5,5 the best action is " + optimalPolicy.get(new State(5,5)).get+"'");
}