Skip to content

Instantly share code, notes, and snippets.

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
object LinkedList {
def apply[E]( items : E* ) : LinkedList[E] = items match {
case x if x.isEmpty => Empty
case _ => Node(items.head, apply(items.tail: _*))
}
}
@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(_))
@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 / quotes.txt
Created December 10, 2013 19:18
Programming Quotes
1. “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”
- C.A.R. Hoare (British computer scientist, winner of the 1980 Turing Award)
2. “If debugging is the process of removing software bugs, then programming must be the process of putting them in.”
- Edsger Dijkstra (Dutch computer scientist, winner of the 1972 Turing Award)
3. “Measuring programming progress by lines of code is like measuring aircraft building progress by weight.”
- Bill Gates (co-founder of Microsoft)
4. “Nine people can’t make a baby in a month.” (regarding the addition of more programmers to get a project completed faster)
@eribeiro
eribeiro / weakref-snippet.java
Last active January 5, 2017 14:34
If you are using a *WeakHashMap* and your keys are boxed primitive types like, for example, an Integer with value 10, then you should *NOT* use "Integer.valueOf(10)" or even rely on autoboxing -- e.g., map.put(10, "blabla") -- to populate the keys of this map. REASON: both ".valueOf()" and autoboxing do a *caching* of primitive values (up to a t…
WeakHashMap<Integer, String> map = new WeakHashMap<Integer, String>();
map.put(new Integer(10), "aaa"); // remove entry
// map.put(10, "aaa"); // don't remove entry
// map.put(Integer.valueOf(10), "aaa"); // don't remove entry
for (int i = 0; i < Integer.MAX_VALUE; i++) {
if (map.size() != 0) {
System.out.println("At iteration " + i + " the map still holds the reference to object");
} else {
// Source: https://groups.google.com/forum/#!topic/angular/hVrkvaHGOfc
// jsFiddle: http://jsfiddle.net/pkozlowski_opensource/PxdSP/14/
// author: Pawel Kozlowski
var myApp = angular.module('myApp', []);
//service style, probably the simplest one
myApp.service('helloWorldFromService', function() {
this.sayHello = function() {
return "Hello, World!"
class TopK {
private val K = 10
private val values = new Array[Long](K)
private var size = 0
def insert(item: Long) = {
if (size < K) {
values(size) = item
val top = new TopK()
for ( i <- 1 to 10 ) top.insert(i)
for (i <- 10 to 0 by -1) top.insert(i)
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes
# Add git branch if its present to PS1
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
if [ "$color_prompt" = yes ]; then