Skip to content

Instantly share code, notes, and snippets.

@matanox
matanox / util.py
Last active September 28, 2018 21:29
example of sampling from a distribution vector
def sample_from_distribution(distribution_vector):
''' samples from a provided distribution (e.g. for choosing an action from a VW returned distribution vector) '''
probabilities = list(map(float, distribution_vector))
actions = range(1,len(distribution_vector)+1) # VW actions are 1-indexed not zero indexed
assert(len(actions) == len(probabilities))
s = choices(actions, probabilities) # https://docs.python.org/3/library/random.html#random.choices
action = s[0]
object WeightedWagnerFischerTest extends App {
/* currently tests for the default weights */
val externalLibraryImpl = new info.debatty.java.stringsimilarity.Levenshtein
val random = new scala.util.Random(333) // pin down random seed
def randomString(length: Int) = random.alphanumeric.take(length).mkString
def randomPair(length: Int, maxTouches: Int): (String, String) = {
val string1 = randomString(length).toCharArray
var string2 = string1.clone
@matanox
matanox / GithubHelper.scala
Last active December 29, 2015 23:19 — forked from guizmaii/GithubHelper.scala
Helper object parsing the "Link" header of the Github API v3, in Scala
/*
* parses the link header field (http://tools.ietf.org/html/rfc5988) returned by Github's api
* (c.f. https://developer.github.com/guides/traversing-with-pagination/)
*/
trait GithubLinkHeader {
def parse(linkHeader: IndexedSeq[String]): Map[String, String] = {
assert(linkHeader.size == 1)
linkHeader.head.split(',').map { linkEntry =>
@matanox
matanox / build.sbt
Created October 23, 2015 13:08
multi-project sbt build definition, involving dependent compiler and sbt plugins
val integrationTest = taskKey[Unit]("Executes integration tests.")
lazy val root = (project in file(".")).aggregate(simpleGraph, compilerPluginUnitTestLib, compilerPluginn, canveSbtPlugin, sbtPluginTestLib).enablePlugins(CrossPerProjectPlugin).settings(
scalaVersion := "2.11.7",
crossScalaVersions := Seq("2.10.4", "2.11.7"),
publishArtifact := false, // no artifact to publish for the virtual root project
integrationTest := (run in Compile in sbtPluginTestLib).value // not working: need to bounty http://stackoverflow.com/questions/33291071/invoking-a-subprojects-main-with-a-custom-task
)
lazy val simpleGraph = (project in file("simpleGraph"))
libraryDependencies ++= Seq(
"com.typesafe.slick" %% "slick" % "3.0.0-RC2",
"com.zaxxer" % "HikariCP-java6" % "2.0.1", // HikariCP is the unofficial default connection pooling library for slick
"org.slf4j" % "slf4j-nop" % "1.6.4")
libraryDependencies += "mysql" % "mysql-connector-java" % "latest.release"
@matanox
matanox / Slick 3.0.0 code generation
Last active April 13, 2018 17:12
SBT task for slick code generation from a live database
//
// SBT task for slick code generation for a live database
// ======================================================
//
// This sbt task will generate slick scala classes to match your (MySQL) database schema,
// which will allow you to use the database through slick, in your scala application.
//
// Usage: Review all ? symbols and comments below, to adapt to your system. Embed in build.sbt.
// Make sure the database is up. Run: sbt slickGenerate
// In your scala application, import the resulting scala file