Sunday, July 31, 2016
Thursday, July 28, 2016
Scala: Trees: binary, sorted, tail recursive
Scala: Tail recursive tree traversal
http://www.scala-lang.org/old/node/7984
Scala Tree Sort Examplehttps://www.garysieling.com/blog/scala-tree-sort-example
https://dzone.com/articles/scala-tree-sort-example
Binary tree
http://www.scala-lang.org/old/node/52.html
http://www.scala-lang.org/old/node/7984
Scala Tree Sort Examplehttps://www.garysieling.com/blog/scala-tree-sort-example
https://dzone.com/articles/scala-tree-sort-example
Binary tree
http://www.scala-lang.org/old/node/52.html
Sunday, July 24, 2016
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).
// 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)
}
}
->
$ 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)
}
}
Saturday, July 23, 2016
Scala enumerations
http://underscore.io/blog/posts/2014/09/03/enumerations.html
https://gist.github.com/chaotic3quilibrium/57add1cd762eb90f6b24
http://stackoverflow.com/questions/1898932/case-objects-vs-enumerations-in-scala
http://stackoverflow.com/questions/17646335/combining-enums-and-using-getter-to-return-a-specified-enum
http://jackcoughonsoftware.blogspot.rs/2008/06/scala-and-enums.html
http://stackoverflow.com/questions/26492941/scala-enumeration-with-constructor-and-lookup-table
http://stackoverflow.com/questions/4346580/how-to-add-a-method-to-enumeration-in-scala
https://gist.github.com/chaotic3quilibrium/57add1cd762eb90f6b24
http://stackoverflow.com/questions/1898932/case-objects-vs-enumerations-in-scala
http://stackoverflow.com/questions/17646335/combining-enums-and-using-getter-to-return-a-specified-enum
http://jackcoughonsoftware.blogspot.rs/2008/06/scala-and-enums.html
http://stackoverflow.com/questions/26492941/scala-enumeration-with-constructor-and-lookup-table
http://stackoverflow.com/questions/4346580/how-to-add-a-method-to-enumeration-in-scala
object Suit extends Enumeration {
val Clubs, Diamonds, Hearts, Spades = Value
class SuitValue(suit: Value) {
def isRed = !isBlack
def isBlack = suit match {
case Clubs | Spades => true
case _ => false
}
}
implicit def value2SuitValue(suit: Value) = new SuitValue(suit)
}
object Direction extends Enumeration {
type Direction = Value
val NORTH, SOUTH, EAST, WEST = Value
}
import Direction._
var value: Set[Direction.Value] = Set[Direction.Value](Direction.NORTH, Direction.SOUTH)
Scala enumerations
http://underscore.io/blog/posts/2014/09/03/enumerations.html
https://gist.github.com/chaotic3quilibrium/57add1cd762eb90f6b24
http://stackoverflow.com/questions/1898932/case-objects-vs-enumerations-in-scala
http://stackoverflow.com/questions/17646335/combining-enums-and-using-getter-to-return-a-specified-enum
https://gist.github.com/chaotic3quilibrium/57add1cd762eb90f6b24
http://stackoverflow.com/questions/1898932/case-objects-vs-enumerations-in-scala
http://stackoverflow.com/questions/17646335/combining-enums-and-using-getter-to-return-a-specified-enum
Sunday, July 17, 2016
Scala:Monads
http://james-iry.blogspot.rs/2007/09/monads-are-elephants-part-1.html
http://james-iry.blogspot.rs/2007/10/monads-are-elephants-part-2.html
http://james-iry.blogspot.rs/2007/10/monads-are-elephants-part-3.html
http://james-iry.blogspot.rs/2007/11/monads-are-elephants-part-4.html
http://scabl.blogspot.rs/2013/02/monads-in-scala-1.html
http://james-iry.blogspot.rs/2007/10/monads-are-elephants-part-2.html
http://james-iry.blogspot.rs/2007/10/monads-are-elephants-part-3.html
http://james-iry.blogspot.rs/2007/11/monads-are-elephants-part-4.html
http://scabl.blogspot.rs/2013/02/monads-in-scala-1.html
Scalaz
http://timperrett.com/2014/07/20/scalaz-task-the-missing-documentation/
https://www.infoq.com/presentations/Scalaz-Functional-Programming-in-Scala
https://hseeberger.wordpress.com/2010/11/25/introduction-to-category-theory-in-scala/
https://hseeberger.wordpress.com/2011/01/31/applicatives-are-generalized-functors/
http://eed3si9n.com/learning-scalaz/
https://www.infoq.com/presentations/Scalaz-Functional-Programming-in-Scala
https://www.infoq.com/presentations/Scalaz-Functional-Programming-in-Scala
https://hseeberger.wordpress.com/2010/11/25/introduction-to-category-theory-in-scala/
https://hseeberger.wordpress.com/2011/01/31/applicatives-are-generalized-functors/
http://eed3si9n.com/learning-scalaz/
https://www.infoq.com/presentations/Scalaz-Functional-Programming-in-Scala
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:
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.
| 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 typeUnit
,()
, 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
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
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
Change the version from 2.6.0 to 2.7.2 or what ever the latest version is
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/
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.
Subscribe to:
Posts (Atom)