This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
According to Wikipedia, a Markov Chain is a random process where the next state is dependent on the previous state. This is a little difficult to understand, so I'll try to explain it better: | |
What you're looking at, seems to be a program that generates a text-based Markov Chain. Essentially the algorithm for that is as follows: | |
Split a body of text into tokens (words, punctuation). | |
Build a frequency table. This is a data structure where for every word in your body of text, you have an entry (key). This key is mapped to another data structure that is basically a list of all the words that follow this word (the key) along with its frequency. | |
Generate the Markov Chain. To do this, you select a starting point (a key from your frequency table) and then you randomly select another state to go to (the next word). The next word you choose, is dependent on its frequency (so some words are more probable than others). After that, you use this new word as the key and start over. | |
For example, if you look at the very fir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def wtf(v: Option[Int]) = v match { | |
case None => print("") | |
case Some(x) if x % 2 == 0 => print("Even#") | |
case _ => print("Odd#") | |
} | |
// test | |
(1 to 10).map(x=> Some(x)).foreach(wtf(_)) | |
List(None, None, None, Some(2), None, None).foreach(wtf(_)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object LinkedList { | |
def apply[E]( items : E* ) : LinkedList[E] = items match { | |
case x if x.isEmpty => Empty | |
case _ => Node(items.head, apply(items.tail: _*)) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val arr = Array(2, 4, 5, 19, 21, 28) | |
val sl = arr.toList.sorted | |
val dist = sl flatMap { x => | |
sl flatMap { y => | |
if (x != y) { | |
if (x < y) List(y - x) | |
else List(x - y) | |
} | |
else Nil |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WTF extends Thread { | |
private boolean finished = false; | |
... | |
public void run() { | |
.... | |
while (!finished) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def suffix(l: List[Int]): List[List[Int]] = l match { | |
case Nil => List()::Nil | |
case _ => l :: suffix(l.tail) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val list = (1 to 10).toList | |
// imutable map | |
val map1 = list.foldLeft(Map.empty[Int,String])( (map, value) => map + (value -> value.toString) ) | |
// this is equivalent to | |
val map1 = list.foldLeft(Map.empty[Int,String])( (map, value) => map + ((value, value.toString)) ) | |
// imutable map with more complex value (note the extra parenthesis) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// scala | |
(1 to 100).toList foreach { x => x match { | |
case x if (x % 15 == 0) => println("FizzBuzz") | |
case x if (x % 3 == 0) => println("Fizz") | |
case x if (x % 5 == 0) => println("Buzz") | |
case x => println(x) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def scanTrace() { | |
import collection.mutable.{ HashMap, MultiMap, Set } | |
val source = scala.io.Source.fromFile("google-cluster-data-1.csv") | |
val map = new HashMap[String, Set[String]] with MultiMap[String, String] | |
for (i <- source.getLines()) { | |
val fields = i.split(" ") | |
val parent = fields(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FileSystem fs = FileSystems.getDefault(); | |
for (FileStore e : fs.getFileStores()) { | |
System.out.println(e.name() + " " + e.getTotalSpace() + " " + e.type()); | |
} | |
On my box returns: | |
/dev/sda1 488306122752 ext4 | |
proc 0 proc | |
sysfs 0 sysfs |