Skip to content

Instantly share code, notes, and snippets.

@kaja47
kaja47 / lol.php
Last active August 29, 2015 14:23
PHP polymorphic inline caches
<?php
class A {
public $somePropertyWithReasonablyLongName = 1;
}
class B {
public $somePropertyWithReasonablyLongName = 1;
}
@kaja47
kaja47 / 1nn.c
Last active August 29, 2015 14:26
nearest neighbor
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
#include <stdint.h>
#include <immintrin.h>
#include "sys/param.h"
@kaja47
kaja47 / StringIntDictionary.scala
Created August 10, 2015 03:08
StringIntDictionary
/** Idiosyncratic dictionary mapping strings to ints specialized for english
* words and other short alphanumeric strings. Long strigns are packed into
* one continuous char array, short alphanumeric strings are inlined into the
* structure that normally points into the array of long strings. This has the
* effect that strings shorter than 9 characters need only 12 bytes per mapping
* and lookup causes only one cache miss.
*
* Warning: Might contains subtle and not-so-soubtle errors.
*/
class StringIntDictionary {
/**
* Decodes a JSON string.
* @param string
* @param boolean
* @param integer
* @return mixed
*/
public static function decode($json, $assoc = FALSE, $depth = 512)
{
$json = (string) $json;
@kaja47
kaja47 / csfdsim.scala
Created December 30, 2014 04:58
How to compute similar movies from CSFD data in 10 minutes and find love of your life
import breeze.linalg._
import breeze.stats
import breeze.numerics._
val dataFile = new File(???)
val userItems: Array[SparseVector[Double]] = loaderUserItemsWithRatings(dataFile, """[ ,:]""".r)
val itemUsers: Array[SparseVector[Double]] = transpose(userItems) map { vec => normalize(vec, 2) }
// weights
val N = DenseVector.fill[Double](itemIndex.size)(userIndex.size) // vector where total numbers of users is repeated
@kaja47
kaja47 / svd-img.scala
Created May 13, 2014 21:57
Visualization of truncated SVD
import breeze._
import breeze.linalg._
import breeze.numerics._
import java.awt.image.BufferedImage
import javax.imageio.ImageIO
val f = ???
val img = javax.imageio.ImageIO.read(new File(f))
val gray = new BufferedImage(img.getWidth, img.getHeight, BufferedImage.TYPE_BYTE_GRAY)
val g = gray.createGraphics()
@kaja47
kaja47 / gist:554f62c61f21b0420720
Created May 9, 2014 19:47
minhash vs. HyperLogLog
// min-hash
val fs: Vector[Int => Int] // hash funkce
items map { it => fs map { f => f(it) } } fold (vectorPairwise(min), initialValue = Vector.fill(infinity))
// HyperLogLog
// size = (1 << 25), time = 800 ms
// size = (1 << 25) - 1999, time = 400 ms ???
// size = (1 << 24), time = 700 ms
val size = 1 << 25
val randos = new util.Random()
val arr = Array.fill(size)(randos.nextInt)
java.util.Arrays.sort(arr)
@kaja47
kaja47 / build.sbt
Created June 28, 2011 09:28
stripbot source
scalaVersion := "2.9.0-1"
libraryDependencies += "commons-codec" % "commons-codec" % "1.4"
libraryDependencies += "oauth.signpost" % "signpost-core" % "1.2.1.1"
libraryDependencies += "org.jdom" % "jdom" % "1.1"
libraryDependencies += "rome" % "rome" % "0.9"
@kaja47
kaja47 / gist:1056244
Created June 30, 2011 13:32
CPS Fibonnacci
def fib[A](n: Int, k: Int => A): A = {
if (n < 2) k(1)
else fib(n-1, (x: Int) =>
fib(n-2, (y: Int) =>
k(x+y)))
}
fib(10, println)