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
// 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 } |
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 | |
// 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; | |
}; | |
} |
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
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) |
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
// 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)))) |
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
#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); |
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.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]) { |
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
// Scala version of this http://haskelllive.com/ | |
val initialBoardStr = Seq( | |
"rnbqkbnr", | |
"pppppppp", | |
" ", | |
" ", | |
" ", | |
" ", | |
"PPPPPPPP", |
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
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)) |
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) { |