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
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
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
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
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) |
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
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 { |
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
// 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!" |
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
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 |
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 top = new TopK() | |
for ( i <- 1 to 10 ) top.insert(i) | |
for (i <- 10 to 0 by -1) top.insert(i) |
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
# 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 |