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
import java.util.Date; | |
/** | |
* Example code for my blog post "The TOCTTOU attack", available at http://out-println.appspot.com/posts/tocttou. | |
*/ | |
public class Tocttou { | |
public static final class Interval { | |
private final Date min; |
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
package test; | |
import java.util.concurrent.ConcurrentLinkedQueue; | |
/** | |
* Illustrates a case when Collection.size() cannot reliably indicate how many elements were dumped by | |
* Collection.toArray(). | |
* | |
* Assuming that the array is reused and was previously filled with unrelated values, the fact that toArray() | |
* writes null after the last element is the only way to find out how many elements were actually dumped. |
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
/** | |
* Clear-headed attempt at a functional-style, "Scala-way" implementation. | |
* | |
* Seems to work on trivial cases. Too lazy to write unit tests now (note to self: write them later and then pretend I did TDD). | |
*/ | |
class GameOfLife(val width: Int, val height: Int, val liveCells: Set[(Int, Int)]) { | |
def nextIteration: GameOfLife = { | |
val newCells = for { | |
(cell, liveNeighborCount) <- liveNeighborCounts |
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
/* Finds the version history of a POM in a Subversion repository */ | |
import scala.sys.process.Process | |
object Conf { | |
val url = "put the url of your POM here" | |
val svn = "svn --username xxx" | |
} | |
case class Revision(number: Int, date: String, version: String) { |
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
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.util.Iterator; | |
import java.util.NoSuchElementException; | |
/** Streams the lines of a {@link BufferedReader} while applying some parsing method. */ | |
abstract class LineParsingIterator<T> implements Iterator<T> { | |
private final BufferedReader input; | |
private T buffered; // parsed but not yet returned to the client |
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
import com.datastax.driver.core.Cluster; | |
import com.datastax.driver.core.Session; | |
import com.datastax.driver.mapping.Mapper; | |
import com.datastax.driver.mapping.MappingManager; | |
import com.datastax.driver.mapping.annotations.Frozen; | |
import com.datastax.driver.mapping.annotations.PartitionKey; | |
import com.datastax.driver.mapping.annotations.Table; | |
import com.datastax.driver.mapping.annotations.UDT; | |
public class DriverDemo { |
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
# Install graphite 0.9.10 on a Mac (Python 2.6 preinstalled) | |
# Gathered from my notes, might not be 100% accurate | |
brew install py2cairo | |
sudo pip install Django==1.3 | |
sudo pip install django-tagging | |
sudo pip install carbon | |
sudo pip install whisper | |
sudo pip install graphite-web | |
sudo pip install tagging # not sure if really useful |
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
import scala.concurrent.Future | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.util.{Try, Success, Failure} | |
val f: Future[Int] = Future(1 / 0) | |
// answer C | |
def applyC[T](f: Future[T]): Future[Try[T]] = f.map(x => Try(x)) | |
// answer D |
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
// Naive solution (two passes) | |
def volume(l: List[Int]) = { | |
def peakRight(l: List[Int]) = l.foldRight(List(0)) { case (i, acc) => (i max acc.head) :: acc }.tail | |
def peakLeft(l: List[Int]) = peakRight(l.reverse).reverse | |
val waterLevels = peakLeft(l) zip peakRight(l) map { case (l, r) => l min r } | |
val volumes = l zip waterLevels map { case (g, w) => (w - g) max 0 } | |
volumes reduceLeft (_ + _) | |
} |
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
import com.datastax.driver.core.*; | |
import com.datastax.driver.core.policies.Policies; | |
import java.net.InetAddress; | |
public class GatherAllListenAddresses { | |
public static void main(String[] args) { | |
Cluster cluster = null; | |
try { | |
cluster = Cluster.builder() |