Skip to content

Instantly share code, notes, and snippets.

@eribeiro
eribeiro / markov-chain.txt
Created December 6, 2013 16:28
Markov Chain Text Generator Source: http://stackoverflow.com/questions/4081662/explain-markov-chain-algorithm-in-laymans-terms Further reference: 'The Practice of Programming' by Brian W. Kernighan and Rob Pike. Chapter 3 provides the algorithm for a Markov chain approach that reads a source text and uses it to generate random text that "reads w…
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
@eribeiro
eribeiro / wtf.scala
Last active December 29, 2015 16:49
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(_))
object LinkedList {
def apply[E]( items : E* ) : LinkedList[E] = items match {
case x if x.isEmpty => Empty
case _ => Node(items.head, apply(items.tail: _*))
}
}
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
@eribeiro
eribeiro / WTF.java
Last active December 21, 2015 04:29
Can someone please explain why on earth is this guy synchronizing on Thread.sleep ? AFAIK, if you have, say, 4 threads, then one is sleeping while the other 3 are blocked, *waiting* to sleep (!!!) (detail: finished is not volatile). Your sincerely, Ed.
public class WTF extends Thread {
private boolean finished = false;
...
public void run() {
....
while (!finished) {
@eribeiro
eribeiro / ex1-ch1.scala
Created August 14, 2013 17:56
Exercise of the first chapter of Okasaki's Functional Data Structures.
def suffix(l: List[Int]): List[List[Int]] = l match {
case Nil => List()::Nil
case _ => l :: suffix(l.tail)
}
@eribeiro
eribeiro / MapBasic.scala
Last active July 25, 2021 05:41
How to do a foldLeft with a Map in Scala, plus basic stuff.
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)
// 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)
}
}
@eribeiro
eribeiro / scanTrace
Last active December 19, 2015 11:49
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)
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