Skip to content

Instantly share code, notes, and snippets.

@kaja47
kaja47 / textgen.scala
Created August 9, 2012 00:29
Statistical text generator
// 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()
@kaja47
kaja47 / gist:3688772
Created September 10, 2012 03:51
PHP spellcheck
<?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) {
@kaja47
kaja47 / game-of-life.scala
Created September 18, 2012 05:04
Conway's game of life
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))
@kaja47
kaja47 / refl.scala
Created October 3, 2012 11:35
Reflection or not?
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
@kaja47
kaja47 / inject-traits.php
Created October 7, 2012 21:33
inject traits
<?php
trait WantsAuthenticator {
function injectAuthenticator(Authenticator $authenticator) {
$this->authenticator = $authenticator;
}
}
trait WantsCache {
function injectCache(Cache $cache) {
$this->cache = $cache;
@kaja47
kaja47 / gist:3876064
Created October 11, 2012 22:51
Content-aware image cropping with Scala
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))
@kaja47
kaja47 / string-calculator-tdd-cata.scala
Created October 12, 2012 20:20
String Calculator TDD kata
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 =>
@kaja47
kaja47 / wtf.sql
Created October 21, 2012 23:19
MySQL - same queries, vastly different perfomance
-- 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)
@kaja47
kaja47 / gist:3971590
Created October 29, 2012 04:50
Daily dose of MySQL hackery
-- 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'
// 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"