Wednesday, July 30, 2014

How to upgrade Docker on Ubuntu 14.04 ?

http://askubuntu.com/questions/472412/how-do-i-upgrade-docker
http://xmodulo.com/2014/05/manage-linux-containers-docker-ubuntu.html
http://xmodulo.com/2014/05/docker-containers-centos-fedora.html
https://fredjean.net/running-docker-under-linux-mint-16/

Suppose it is Ubuntu Trusty (14.04) release, which has 0.9.1 officially
Update to 0.11.1 release
If you want to upgrade in this release, see new launchpad https://launchpad.net/~docker-maint/+archive/testing, better use ppa installation.
sudo add-apt-repository ppa:docker-maint/testing
sudo apt-get update
sudo apt-get install docker.io
Now I get the latest one
$ docker -v
Docker version 0.11.1, build fb99f99
Be aware, with the 1.0 GA release, this method may be changed
Update to latest release(now 1.1.1)
This is the way stated to install docker for 12.04 LTS in official document and noticed inubuntuupdates.org, which looks work for 14.04 as well.
Refer the command to Getting Docker Installed on Ubuntu 12.04 LTS as well
$ wget -qO- https://get.docker.io/gpg | sudo apt-key add -
$ sudo sh -c "echo deb http://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list"
$ sudo apt-get update
$ sudo apt-get install lxc-docker
Now I get
$ docker -v
Docker version 1.1.1, build bd609d2
lxc-docker will remove old docker package, therefore please backup the configuration in advance.

Monday, July 28, 2014

Java/Scala benchmark and tools

http://shipilev.net/blog/2014/java-scala-divided-we-fail/
http://openjdk.java.net/projects/code-tools/jmh/
http://hg.openjdk.java.net/code-tools/jmh/file/tip/jmh-samples/src/main/java/org/openjdk/jmh/samples/

As a good tradition, we will take some diversions into benchmarking methodology, so that even though the post itself is targeted to platform people, the non-platform people can still learn a few tricks. As usual, if you still haven’t learned about JMH and/or haven’t looked through the JMH samples, then I suggest you do that first before reading the rest of this post for the best experience.

Benchmark

The discussion in that particular StackOverflow thread dates back a few questions, so instead of digging there, we will just take the latest benchmark code, and wrap it up with JMH. JMH already has the bindings for Java and Scala, which somewhat alleviates the difference in testing methodology. You can find full benchmark code here (warning, it contains spoilers).

Wednesday, July 23, 2014

How to run R from Shell script ?

http://stackoverflow.com/questions/2151212/how-can-i-read-command-line-parameters-from-an-r-script
http://stackoverflow.com/questions/5391124/in-r-select-rows-of-a-matrix-that-meet-a-condition
http://stackoverflow.com/questions/7201341/how-can-2-strings-be-concatenated-in-r

I made two files: exmpl.bat and exmpl.r.
  • exmpl.bat:
    set R_Script="C:\Program Files\R-3.0.2\bin\RScript.exe"
    %R_Script% exmpl.R 2010-01-28 example 100 > exmpl.batch 2>&1
    Alternatively using Rterm.exe:
    set R_TERM="C:\Program Files\R-3.0.2\bin\i386\Rterm.exe"
    %R_TERM% --no-restore --no-save --args 2010-01-28 example 100 < exmpl.R > exmpl.batch 2>&1
  • exmpl.r:
    options(echo=TRUE) # if you want see commands in output file
    args <- span=""> commandArgs(trailingOnly = TRUE)
    print(args)
    # trailingOnly=TRUE means that only your arguments are returned, check:
    # print(commandsArgs(trailingOnly=FALSE))
    
    start_date <- span=""> as.Date(args[1])
    name <- span=""> args[2]
    n <- span=""> as.integer(args[3])
    rm(args)
    
    # Some computations:
    x <- span=""> rnorm(n)
    png(paste(name,".png",sep=""))
    plot(start_date+(1L:n), x)
    dev.off()
    
    summary(x)
Save both files in the same directory and start exmpl.bat. In result you got:
  • example.png with some plot
  • exmpl.batch with all what was done
Just to add - you could add environment variable %R_Script%:
"C:\Program Files\R-3.0.2\bin\RScript.exe"
and use it in your batch scripts as %R_Script% .......
Differences between RScript and Rterm:

======================================================================
> colnames <- c="" col1="" col3="" div="" nbsp="">
> data[ , colnames] 
  col1 col3
1    1    4
2    2    5
3    3    6
> data[ , colnames] 
  col1 col3
1    1    4
2    2    5
3    3    6
> r <- colnames="" data="" div="" nbsp="">
> r
  col1 col3
1    1    4
2    2    5
3    3    6
> r[r$col1 == 2,]
  col1 col3
2    2    5
> # this is a comment line
> r[r$col1 == 2 || r$col1==3,]
[1] col1 col3
<0 rows=""> (or 0-length row.names)
> r[r$col1 == 2,]
  col1 col3
2    2    5
> r[r$col1 == 2 | r$col1==3,]
  col1 col3
2    2    5
3    3    6

Saturday, July 19, 2014

Install GNU command line tools on Mac OSX

http://www.topbug.net/blog/2013/04/14/install-and-use-gnu-command-line-tools-in-mac-os-x/

Install Homebrew

First, visit Homebrew homepage and follow the installation instructions to install Homebrew.
Shortcut: install the latest XCode and then run the following command to install:
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
Then add the following line to your .bashrc or .zshrc:
export PATH="$(brew --prefix coreutils)/libexec/gnubin:/usr/local/bin:$PATH"

Install the GNU Command Line Tools

First comes the most important one – GNU Coreutils:
brew install coreutils
GNU Coreutils contains the most essential UNIX commands, such as lscat.

Thursday, July 17, 2014

Scala covariant and invariant

http://abstractlayers.com/2014/03/06/covariance-and-contravariance-in-scala/

If you have 2 classes A,B such that A is subtype of B, and
if you have 2 parameterized types C[A],C[B] such that C[A] is subtype of C[B] then C is said to be covariant
Examples
Immutable List and Queue are examples of covariant Types in scala libraries.
1
2
3
sealed abstract class List[+A] extends .........
class Queue[+A] extends ........
Covariance is indicated by notation + on type parameter passed

Immutablity and Covariance

If you notice in api-docs covariance is generally associated with immutable types , eg scala.collection.immutable.Queue, scala.collection.immutable.List
There is a very good reason for this. From designers perspective, there were 2 reasons where covariance relation could fail
  • Re-Assignment of values. Classic example being java array reassignment problem, resulting in ArrayStore exception
  • Passing generic type as a parameter in method.
Thus to avoid above pitfalls , covariant types are immutable

If you have 2 classes A, B such that A is subtype of B, and
if you have 2 parametrized Types C[A], C[B] such that C[B] is subtype of C[A] then C is said to be contravariant
Examples
OuputChannel trait from scala api is example of contravariant type
1
trait OutputChannel[-Msg] extends AnyRef
Contravariance is indicated by notation – on type parameter passed. 

*** Function parameters are contravariant ***
*** Function’s return value type is covariant ***

https://twitter.github.io/scala_school/type-basics.html
Variance
Scala’s type system has to account for class hierarchies together with polymorphism. Class hierarchies allow the expression of subtype relationships. A central question that comes up when mixing OO with polymorphism is: if T’ is a subclass of T, is Container[T’] considered a subclass of Container[T]? Variance annotations allow you to express the following relationships between class hierarchies & polymorphic types:
MeaningScala notation
covariantC[T’] is a subclass of C[T][+T]
contravariantC[T] is a subclass of C[T’][-T]
invariantC[T] and C[T’] are not related[T]
The subtype relationship really means: for a given type T, if T’ is a subtype, can you substitute it?
scala> class Covariant[+A]
defined class Covariant

scala> val cv: Covariant[AnyRef] = new Covariant[String]
cv: Covariant[AnyRef] = Covariant@4035acf6

scala> val cv: Covariant[String] = new Covariant[AnyRef]
:6: error: type mismatch;
 found   : Covariant[AnyRef]
 required: Covariant[String]
       val cv: Covariant[String] = new Covariant[AnyRef]
                                   ^