Sunday, July 31, 2016

More neural network references

Thursday, July 28, 2016

Scala: Trees: binary, sorted, tail recursive

Sunday, July 24, 2016

Scala IDE Tutorial link

Scala ArrayBuffer documentation

Scala traits, objects and classes

Scala: Instrumented Quicksort

It is some times a lot easier to see what an algorithm is doing if print statements are added to the code. Note that each time that the sort function is called recursively the level counter is incremented. the printout is indented one more space for each level (recursion call).

$ scalac -feature MySorter.scala 
$ scala -classpath . MySorter
my sorter
 [1] in: ( ) 6 3 5 2 7 4 8 1
 [1] pivot:  ( ) 7
  [2] in: (>) 6 3 5 2 4 1
  [2] pivot:  (>) 2
   [3] in: (>) 1
   [3] out: (>) 1
   [3] in: (<) 6 3 5 4
   [3] pivot:  (<) 5
    [4] in: (>) 3 4
    [4] pivot:  (>) 4
     [5] in: (>) 3
     [5] out: (>) 3
     [5] in: (<) 
     [5] out: (<) 
   [3] out: (>) 3 4
    [4] in: (<) 6
    [4] out: (<) 6
  [2] out: (<) 3 4 5 6
 [1] out: (>) 1 2 3 4 5 6
  [2] in: (<) 8
  [2] out: (<) 8
[0] out: ( ) 1 2 3 4 5 6 7 8

[0] final: s1 2 3 4 5 6 7 8

// Original code from http://www.scala-lang.org/docu/files/ScalaByExample.pdf

def sort(xs: Array[Int]): Array[Int] = {
  if (xs.length <= 1) xs
  else {
     val pivot = xs(xs.length / 2)
     Array.concat( sort(xs filter (pivot >)), xs filter (pivot ==), sort(xs filter (pivot <)))
  }
}

// Modified code with instrumentation
// compile the code below

import scala.language.postfixOps

object MySorter {

  def indent(len : Int) : String = {
    var sb = new String

    for (i <- 1="" len="" p="" to="">      sb += " "
    }

    return sb
  }

  def printArray[T](level : Int, msg : String, a : Array[T]) {
    val s = a.mkString(" ")
    val padding = indent(level)
    println(padding + "[" + level +  "] "  + msg + s)
  }

  def sort(xs: Array[Int], level : Int, comparison : String): Array[Int] = {
    var l = level
    l += 1

    val padding = indent(l)

    printArray(l, "in: (" + comparison + ") ", xs)
    if (xs.length <= 1) {
      printArray(l, "out: (" + comparison + ") ", xs)
      xs
    }
    else {
      val pivot = xs(xs.length / 2)
      println(padding + "[" + l +  "]"  + " pivot:  (" + comparison + ") " + pivot)
      var out = Array.concat(
        sort(xs filter (pivot >), l, ">"),
        xs filter (pivot ==),
        sort(xs filter (pivot <),l, "<"))
      l -= 1
      printArray(l, "out: (" + comparison + ") ", out)
      return out
    }
  }

  def main(args: Array[String]) {
    println("my sorter")
    var a = Array(6, 3, 5, 2, 7, 4, 8,1)
    var level = 0
    var aa = sort(a, level, " ")
    //printArray(a)
    printArray(0, "final: s", aa)
  }
}

Scala infix operators: nice Stackoverflow explaination

Sunday, July 17, 2016

Scala:Monads

Scala: Graphs

More Scala URL's

Scalaz

A post about nothing in Scala

The post at the following URL explains nothingness in Scala.

https://oldfashionedsoftware.com/2008/08/20/a-post-about-nothing/

From the URL:
"The method getAStringMaybe returns Option[String]. Option is an abstract class with exactly two subclasses, class Some and object None. Those are the only two ways to instantiate an Option. So getAStringMaybe returns either a Some[String] or None. Some and None are case classes, so you can use the handy match/case construct to handle the result. None is object that signifies no result from the method.
The purpose of an Option[T] return type is to tell callers that the method might return a T in the form of a Some[T], or it might return None to signify no result. This way, the caller supposedly knows when he does and does not need to check for a good return value."

Unit is the same as void in Java

"This is another easy one. Unit is the type of a method that doesn’t return a value of any sort. Sound familiar? It’s like a void return type in Java. Here’s an example:
scala> def doThreeTimes(fn: (Int) => Unit) = {
| fn(1); fn(2); fn(3);
| }
doThreeTimes: ((Int) => Unit)Unit"

"One other use of Nothing is as a return type for methods that never return. It makes sense if you think about it. If a method’s return type is Nothing, and there exists absolutely no instance of Nothing, then such a method must never return.
Your logging method would return Unit. There is a value Unit so it can actually be returned. From the API docs:
Unit is a subtype of scala.AnyVal. There is only one value of type Unit, (), and it is not represented by any object in the underlying runtime system. A method with return type Unit is analogous to a Java method which is declared void."
http://stackoverflow.com/questions/26563213/how-to-instantiate-unit-in-scala

You can just use () whose type is Unit:
scala> import scala.collection.mutable.HashMap
import scala.collection.mutable.HashMap

scala> val myMap = new HashMap[String, Unit]()
myMap: scala.collection.mutable.HashMap[String,Unit] = Map()

scala> myMap + ("myStringKey" -> ())
res1: scala.collection.mutable.Map[String,Unit] = Map(myStringKey -> ())
This is a comment taken from Unit.scala:
There is only one value of type Unit(), and it is not represented by any object in the underlying runtime system.



Tuesday, July 5, 2016

Mac OSX Hadoop setup

Setup JAVA_HOME

http://stackoverflow.com/questions/22842743/setting-java-home-environment-variable-on-mac-osx-10-9

echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile
and restart your shell.
If you have multiple JDK versions installed and you want it to be a specific one, you can use the -v flag to java_home like so:
echo export "JAVA_HOME=\$(/usr/libexec/java_home -v 1.7)" >> ~/.bash_profile
https://amodernstory.com/2014/09/23/installing-hadoop-on-mac-osx-yosemite/

Change the version from 2.6.0 to 2.7.2 or what ever the latest version is
Make a user account on HDFS (use your user name...)
https://docs.hortonworks.com/HDPDocuments/Ambari-2.1.2.0/bk_ambari_views_guide/content/_setup_HDFS_user_directory.html


Now run an example Map Reduce job (change the commands to use the /user/ that you created above):

https://hadoop.apache.org/docs/current/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html#Example:_WordCount_v1.0

http://stackoverflow.com/questions/22726305/get-error-when-i-run-hbase-shell
. Following the instructions here, before run ./bin/hbase shell command you should use ./bin/start-hbase.sh first. Then my problem was solved.