Skip to content

Instantly share code, notes, and snippets.

@kaja47
kaja47 / xpath.scala
Created May 2, 2012 02:07
Handy Scala wrapper around Java Xpath API
// moved to https://github.com/kaja47/xpath.scala
object Xpath {
import javax.xml.xpath.{ XPathConstants, XPathFactory }
import javax.xml.namespace.QName
import org.w3c.dom.{ Node, NodeList }
trait CanXpath[T] { def xpathConst: QName; def convert(x: AnyRef): T = x.asInstanceOf[T] }
implicit object CanXpathBoolean extends CanXpath[Boolean] { def xpathConst = XPathConstants.BOOLEAN }
implicit object CanXpathString extends CanXpath[String] { def xpathConst = XPathConstants.STRING }
@kaja47
kaja47 / phplisp.php
Created May 3, 2012 17:31
Object oriented system made only from functions
<?php
// If you want to make an object oriented system from scratch, you must first create the universe.
// Only things you need are functions
function cons($h, $t) {
return function($what) use($h, $t) {
return ($what[0] === 'h') ? $h : $t;
};
}
@kaja47
kaja47 / gist:2636926
Created May 8, 2012 16:22
ScalaZ: Map bifunctor
implicit def MapBifunctor: Bifunctor[Map] = new Bifunctor[Map] {
def bimap[A, B, C, D](k: Map[A, B], f: A => C, g: B => D) =
k map { case (k, v) => (f(k), g(v)) } // k map { f <-: _ :-> g }
}
// ordinary bifunctor on pairs
val add100 = (x: Int) => x + 100
(1, 1) :-> add100 // (1, 101)
add100 <-: (1, 1) // (101, 1)
add100 <-: (1, 1) :-> add100 // (101, 101)
// without lenses
val _board = chan.boards(bIdx)
val _thread = chan.boards(bIdx).threads(tIdx)
val _posts = chan.boards(bIdx).threads(tIdx).posts
Right(chan.copy(boards = chan.boards.updated(bIdx, _board.copy(threads = _board.threads.updated(tIdx, _thread.copy(posts = _posts :+ newPost.post))))))
// with lenses
Right(boardsLens.at(bIdx).mod(chan, b => threadsLens.at(tIdx).mod(b, t => t.copy(posts = t.posts :+ newPost.post))))
@kaja47
kaja47 / jacc.c
Created October 5, 2015 21:28
jaccard similarity using AVX2 SIMD
#include <stdio.h>
#include <immintrin.h>
#include <stdint.h>
int intersectionSize(int* a, const int alen, int* b, const int blen) {
int ai = 0, bi = 0, size = 0;
while (ai < alen && bi < blen) {
int av = a[ai];
int bv = b[bi];
size += ((av == bv) ? 1 : 0);
import com.codecommit.antixml._
class NodeOps[+A <: Node](node: A) {
def \@(attr: String)(implicit ev: A <:< Elem): String = node.attrs get attr getOrElse ""
def \@(attr: Symbol)(implicit ev: A <:< Elem): String = this \@ attr.name
def fulltext(implicit ev: A <:< Elem) = node \\ text mkString " " replaceAll ("\\s+", " ") trim
}
implicit def pimpNode[A <: Node](node: A): NodeOps[A] = new NodeOps(node)
class GroupOps[+A <: Node](group: Group[A]) {
// Scala version of this http://haskelllive.com/
val initialBoardStr = Seq(
"rnbqkbnr",
"pppppppp",
" ",
" ",
" ",
" ",
"PPPPPPPP",
@kaja47
kaja47 / cossim-naive.scala
Created August 8, 2012 01:53
Cosine similarity
type WordFreq = Map[String, Int]
def dot(a: WordFreq, b: WordFreq): Double =
a map { case (k, v) => if (b contains k) v * b(k) else 0 } sum
def magnitude(ws: WordFreq): Double =
math.sqrt(ws.values map (x => x * x toDouble) sum)
def cossim(a: WordFreq, b: WordFreq): Double =
dot(a, b) / (magnitude(a) * magnitude(b))
@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) {