Skip to content

Instantly share code, notes, and snippets.

View thedmitriyk's full-sized avatar

Dmitriy Kostyuchenko thedmitriyk

View GitHub Profile
@thedmitriyk
thedmitriyk / SpellCheck.scala
Created July 15, 2015 11:13
A naïve Scala implementation of Peter Norvig's Spelling Corrector from http://norvig.com/spell-correct.html Optimized for neither readability nor brevity nor performance.
object SpellCheck {
val alpha = "abcdefghijklmnopqrstuvwxyz"
val model = scala.io.Source.fromFile("big.txt")
.getLines()
.flatMap(_.toLowerCase.split("\\W+"))
.foldLeft(Map.empty[String, Int].withDefaultValue(1)) { (acc, word) =>
acc + (word -> (acc.getOrElse(word, 0) + 1))
}
@thedmitriyk
thedmitriyk / JVMOpcodeLookup.scala
Created October 20, 2015 16:52
JVM Opcode Mnemonic Lookup Map
val opcodeLookup = Map(
0 -> "nop",
1 -> "aconst_null",
2 -> "iconst_m1",
3 -> "iconst_0",
4 -> "iconst_1",
5 -> "iconst_2",
6 -> "iconst_3",
7 -> "iconst_4",
8 -> "iconst_5",
@thedmitriyk
thedmitriyk / JVMParamParser.scala
Last active November 11, 2015 13:46
Parse JVM-style method parameter strings (e.g. "I[[BLjava/lang/String;") into separate components (e.g. Seq("I", "[[B", "Ljava/lang/String;"))
object JVMParamParser {
def parse(sig: String) = {
def parseRec(acc: Seq[String], sig: List[Char]): Seq[String] = {
val drop = sig.dropWhile(_ == '[')
val fill = Seq.fill(sig.length - drop.length)('[').mkString
drop match {
case head :: tail if jvmTypeLookup.contains(head) =>
parseRec(acc :+ s"$fill${head.toString}", tail)
case head :: tail if head == 'L' =>
@thedmitriyk
thedmitriyk / FlickrBase58Coder.scala
Last active November 3, 2016 10:44
Flickr Base58 encoder and decoder written in Scala.
object FlickrBase58Coder {
val alpha = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"
val base = alpha.length
def apply(encodedInput: String) = decode(encodedInput)
def apply(decodedInput: Long) = encode(decodedInput)
def encode(input: String): String = encode(input.toLong)
def encode(input: Long) = {
def enc(in: Long, acc: String): String = if (in < 1) acc else enc(in / base, alpha((in % base).toInt) + acc)