Skip to content

Instantly share code, notes, and snippets.

@ruippeixotog
ruippeixotog / gist:875bd2bce5fc8ff6e79a
Created August 25, 2015 23:34
scala-scraper: modify the contents of an element
scala> import net.ruippeixotog.scalascraper.browser.Browser
import net.ruippeixotog.scalascraper.browser.Browser
scala> import net.ruippeixotog.scalascraper.dsl.DSL._
import net.ruippeixotog.scalascraper.dsl.DSL._
scala> import net.ruippeixotog.scalascraper.dsl.DSL.Extract._
import net.ruippeixotog.scalascraper.dsl.DSL.Extract._
scala> val browser = new Browser
@ruippeixotog
ruippeixotog / IsSorted.scala
Created July 1, 2014 23:45
Type-level ordering verification
import shapeless._
import shapeless.ops.nat.LTEq.<=
import Nat._
sealed trait ToValue[T, V] { def value: V }
def toValue[T, V](implicit tv: ToValue[T, V]): V = tv.value
object Bool {
sealed trait Bool
@ruippeixotog
ruippeixotog / gist:4049216
Created November 10, 2012 00:30 — forked from hugoferreira/gist:4048818
Genetic Algorithms in Scala
import annotation.tailrec
import util.Random
object main extends App {
val target = "as armas e os baroes assinalados"
val genePool = Array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' ')
def fitness(src: String): Double = src.zip(target).foldRight(0.0f) { (e, acc) => e match {
case (s, t) if s == t => acc + 1
case _ => acc
@ruippeixotog
ruippeixotog / package.json
Created November 16, 2015 12:32
botwars - JavaScript client
{
"name": "botarena-bot-js",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Rui Gonçalves",
"dependencies": {
import shapeless._
trait TypeValue[A, R] {
def apply(): R
}
trait Size[A] extends TypeValue[A, Int]
implicit val shortSize = new Size[Short] {
def apply() = 2
@ruippeixotog
ruippeixotog / prophet.js
Created December 8, 2013 22:01
The Blind Prophet challenge (http://theblindprophet.com)
var lastDist = null;
function initY(distance) {
actionFunc = setYDir;
return 'up';
}
function setYDir(distance) {
var yDir = lastDist <= distance ? 'down' : 'up';
actionFunc = moveY(yDir);
@ruippeixotog
ruippeixotog / gist:4049223
Created November 10, 2012 00:32
Genetic Algorithms in Scala
import annotation.tailrec
import util.Random
import scala.annotation.tailrec
import scala.collection.immutable.Stream.consWrapper
object Main2 extends App {
val target = "as armas e os baroes assinalados"
val genePool = Array[Char]('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' ')
def fitness(src: String, tgt: String): Double = src.zip(tgt).count { case (s, t) => s == t }
@ruippeixotog
ruippeixotog / benchmark.scala
Created April 17, 2016 16:50
ListMap and ListSet benchmarks
import scala.collection.immutable._
import scala.util.Random
def benchmark[A](desc: String)(block: => A): Option[A] = {
val t = System.currentTimeMillis()
try {
val res = block
println(s"$desc: took ${System.currentTimeMillis() - t} ms")
Some(res)
} catch {
@ruippeixotog
ruippeixotog / benchmark.scala
Created May 7, 2016 18:29
ListMap and ListSet benchmarks - Builder
def benchmark[A](desc: String)(block: => A): Option[A] = {
val t = System.currentTimeMillis()
try {
val res = block
println(s"$desc: took ${System.currentTimeMillis() - t} ms")
Some(res)
} catch {
case e: Throwable =>
e.printStackTrace()
println(s"$desc: ${e.getClass.getSimpleName} thrown after ${System.currentTimeMillis() - t} ms")
@ruippeixotog
ruippeixotog / ListMapBenchmark.scala
Created May 8, 2016 14:12
ListMap JMH Benchmark
package net.ruippeixotog.jmh
import java.util.concurrent.TimeUnit
import scala.collection.immutable.ListMap
import org.openjdk.jmh.annotations.{Benchmark, BenchmarkMode, Mode, OutputTimeUnit}
import org.openjdk.jmh.infra.Blackhole
class ListMapBenchmark {