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
// you need Scala 2.10 to run this | |
val text = ??? | |
// we are interested in bi-grams | |
val n = 2 | |
// map from ngrams to seq of following words | |
val map = collection.mutable.Map[Seq[String], Seq[String]]() withDefaultValue Seq() |
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
<?php | |
// PHP implementation of http://norvig.com/spell-correct.html in 49 lines of code | |
// based on http://soundofemotion.com/spellcorrect.txt which is much longer | |
function words($text) { | |
return preg_split("~[^a-z]+~", $text, null, PREG_SPLIT_NO_EMPTY); | |
} | |
function train($features) { |
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 collection.immutable.BitSet | |
case class Matrix(w: Int, h: Int, bitmap: BitSet) { | |
def reset(cells: Seq[(Int, Int)]): Matrix = | |
copy(bitmap = BitSet() ++ (cells map (pos _).tupled)) | |
def pos(x: Int, y: Int): Int = x + y * w | |
def at(x: Int, y: Int): Boolean = bitmap(pos(x, y)) |
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 A { | |
def callThis = println("YOU MANIACS! YOU BLEW IT UP! OH, DAMN YOU! GODDAMN YOU ALL TO HELL!") | |
} | |
val a = new A | |
a.getClass.getMethod("callThis").invoke(a) // reflection | |
a.asInstanceOf[{ def callThis }].callThis // duck-typed reflection |
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
<?php | |
trait WantsAuthenticator { | |
function injectAuthenticator(Authenticator $authenticator) { | |
$this->authenticator = $authenticator; | |
} | |
} | |
trait WantsCache { | |
function injectCache(Cache $cache) { | |
$this->cache = $cache; |
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 javax.imageio.ImageIO | |
import java.io.File | |
import java.awt.image.BufferedImage | |
import java.awt.{ RenderingHints, AlphaComposite } | |
// Content-aware image cropping with ChunkyPNG | |
// https://gist.github.com/a54cd41137b678935c91 | |
final class Cropper(file: String) { | |
val image = ImageIO.read(new File(file)) |
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.regex.Pattern | |
def add(numbers: String, delimiters: Seq[String] = Seq(",")): Int = { | |
val delimRegex = "^//(.+?)\n(.*)$".r | |
val multiDelim = "^//(\\[.+?\\])\n(.*)$".r | |
numbers match { | |
case "" => 0 | |
case multiDelim(dels, ns) => add(ns, dels.init.tail split "\\]\\[") | |
case delimRegex(del, ns) => add(ns, Seq(del)) | |
case ns => |
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
-- InnoDB table posts (uid, chan, board, threadId, id, ....) | |
-- primary (uid) | |
-- index (chan, board, threadId, id) | |
-- slow as fuck (Using where; Using index) | |
select distinct board | |
from posts | |
where chan = '4chan' | |
-- fast (Using where; Using index for group-by) |
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
-- rather slow | |
select * from posts | |
where text rlike '[[:<:]](pic|picture|image|photo)[[:>:]].*related' | |
-- much much faster | |
select * from posts | |
where text like '%related%' | |
and text rlike '[[:<:]](pic|picture|image|photo)[[:>:]].*related' |
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
// http://blog.greweb.fr/2012/11/play-framework-enumerator-outputstream/ | |
import java.io.{ BufferedWriter, OutputStreamWriter } | |
val enumerator = Enumerator.outputStream { os => | |
val writer = new BufferedWriter(new OutputStreamWriter(os, encoding), 4096) | |
template.writeTo(writer) | |
writer.close() | |
} | |
Ok.stream(enumerator andThen Enumerator.eof).withHeaders( | |
"Content-Type" -> "text/html" |