Skip to content

Instantly share code, notes, and snippets.

View archie's full-sized avatar
😃

Marcus Ljungblad archie

😃
View GitHub Profile
@archie
archie / Zoo.scala
Last active December 28, 2015 20:29
Playing with futures in Scala
import scala.util.{ Failure, Success }
import scala.concurrent.{ Future }
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
import math.random
object Zoo extends App {
val animals = List("monkey", "donkey", "giraffe").map { animal =>
for {
@archie
archie / GraphGenerator.scala
Created November 9, 2013 02:32
Demo using Scala generators constructing random instances of InductiveGraph with nodes having an edge to another node with 75% probability.
package graph
trait Generator[+T] {
self =>
def generate: T
def map[U](f: T => U): Generator[U] = new Generator[U] {
def generate: U = f(self.generate)
}
def flatMap[U](f: T => Generator[U]): Generator[U] = new Generator[U] {
def generate: U = f(self.generate).generate
@archie
archie / FSMDemo.scala
Last active December 26, 2015 19:19
Demo FSM in Scala and Akka with spec
package demo
import akka.actor.Actor
import akka.actor.FSM
import akka.actor.ActorSystem
import akka.actor.Props
import akka.actor.ActorRef
sealed trait State
case object Awake extends State
@archie
archie / Hashing.scala
Last active September 4, 2019 09:24
Implementation of various hash functions for strings in Scala. Details here: http://hs.ljungblad.nu/post/64624649234/a-dive-into-the-mystery-of-hashes
object Hashing {
val BUCKETSIZE = 1000
val words = io.Source.fromFile("/usr/share/dict/words").getLines
.map(x => x.toLowerCase).toList
def initBuckets(size: Int): Array[List[String]] = {
var arr = new Array[List[String]](size)
arr.map(_ => List[String]())
}
@archie
archie / Main.scala
Last active December 25, 2015 17:39
First implementation of a Trie in Scala. Only supports Strings as of now.
import scala.language.postfixOps
import sys.process._
import trie._
object Main {
val ENTER = 10
val BACKSPACE = 127
val BUFFER_SUGGESTION_THRESHOLD = 2
@archie
archie / Example.scala
Last active December 25, 2015 13:18
Draft of inductive graph implementation in Scala based on http://web.engr.oregonstate.edu/~erwig/papers/InductiveGraphs_JFP01.pdf
import graph._
object Main {
def main(args: Array[String]): Unit = {
println(" Exploring the graph ")
val sample =
Context(Adj(List(("left", Node(2)), ("up", Node(3)))),
Node(1), "a", Adj(List(("right", Node(2))))) &:
Context(Adj(List()), Node(2), "b", Adj(List(("down", Node(3))))) &:
@archie
archie / run_test.py
Created February 26, 2013 12:05
Illustrating how to load (through discovery) Python unit tests and executing them.
def test_project(self, projectname):
logging.info('Running tests on: %s' % projectname)
loader = unittest.TestLoader()
tests = loader.discover(projectname)
runner = unittest.TextTestRunner()
runner.run(tests)
@archie
archie / gist:4482874
Created January 8, 2013 10:53
I found this somewhere online but I cannot remember where, so reposting here. Using the command line tool 'imagesnap' it grabs a photo every time you make a commit. It is activated through the post-commit hook. ➜ myrepo git:(master) cat .git/hooks/post-commit
#!/usr/bin/env ruby
file="~/.gitshots/#{Time.now.to_i}.jpg"
unless File.directory?(File.expand_path("../../rebase-merge", __FILE__))
puts "Taking capture into #{file}!"
system "imagesnap -q -w 3 #{file} &"
end
exit 0
@archie
archie / fabfile.py
Created May 11, 2012 08:53
Fabric example
@hosts("local")
def prepare_config(data, maxcap='5'):
local("cp deploy/template.conf deploy/application.conf")
local("sed -i -f \"s/FOLDERPLACEHOLDER/" + data + "/g;s/MAXCAPACITYPLACEHOLDER/" +
maxcap + "/g;\" deploy/application.conf")
def config():
put("deploy/application.conf", "~/recsys/")
run("sed -i \"s/IPPLACEHOLDER/$(hostname -f)/g;\" ~/recsys/application.conf")
@archie
archie / gist:2422239
Created April 19, 2012 16:47
Clustering
...
# Read the vectors
data = read_from_file(sys.argv[1])
items = numpy.array(data)
# Normalise vectors
normalised_items = whiten(items)
# Cluster time!