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
// java api | |
import org.w3c.dom._ | |
import javax.xml.xpath._ | |
import javax.xml.parsers._ | |
val domFactory = DocumentBuilderFactory.newInstance | |
domFactory.setNamespaceAware(true) | |
val doc = domFactory.newDocumentBuilder.parse("boox.xml") |
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 org.ccil.cowan.tagsoup.Parser | |
import org.xml.sax.InputSource | |
import javax.xml.transform | |
import java.net.URL | |
val url = new URL("http://boards.4chan.org/b/") | |
val reader = new Parser | |
reader.setFeature(Parser.namespacesFeature, false) | |
reader.setFeature(Parser.namespacePrefixesFeature, false) |
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
SET NAMES utf8; | |
SET foreign_key_checks = 0; | |
SET time_zone = 'SYSTEM'; | |
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO'; | |
CREATE TABLE `posts` ( | |
`uid` bigint(20) unsigned NOT NULL AUTO_INCREMENT, | |
`chan` varchar(10) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, | |
`board` varchar(10) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, | |
`thread_id` bigint(20) unsigned NOT NULL, |
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
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)) |