Skip to content

Instantly share code, notes, and snippets.

View andresilva's full-sized avatar
🌴
Off

André Silva andresilva

🌴
Off
  • Porto, Portugal
View GitHub Profile
@andresilva
andresilva / fizzbuzz.scala
Last active February 23, 2016 23:12
fizzbuzz sorcery edition
val ms = Array("", "Fizz", "Buzz", "FizzBuzz")
var acc = 810092048
var c = 0
val N = 50
(1 to N).foreach { i =>
c = acc & 3
println(if (c > 0) ms(c) else i)
acc = acc >> 2 | c << 28
}
@andresilva
andresilva / server.js
Last active February 23, 2016 18:46
HTTP dev logging server
var finalhandler = require('finalhandler');
var http = require('http');
var morgan = require('morgan');
morgan.token('type', function (req, res) {
return req.headers['content-type'];
});
morgan.token('data', function (req, res) {
return req.data;
@andresilva
andresilva / benchmark-results.md
Last active January 12, 2016 21:06
Spire Xorshift Benchmarks
[info]                   benchmark     ms linear runtime
[info]                 NextIntJava  13.08 ===
[info]                NextIntScala  13.07 ===
[info]                NextIntLcg32   1.35 =
[info]                NextIntLcg64   1.32 =
[info]           NextIntBurtleRot2   1.37 =
[info]           NextIntBurtleRot3   1.46 =
[info]                NextIntCmwc5   1.35 =
[info] NextIntWell512a 4.48 =
@andresilva
andresilva / xorshift.scala
Created January 6, 2016 22:54
xorshift in scala
import scala.util.Random
object xorshift64star {
private[this] var x: Long = {
val x = Random.nextLong
if (x == 0) -1 else x
}
def apply() = {
x ^= x >>> 12
@andresilva
andresilva / fingertree.rs
Created October 13, 2015 00:20
Finger Tree
use std::rc::Rc;
pub enum FingerTree<A> {
Empty,
Single(A),
Deep(Rc<Digit<A>>, Rc<FingerTree<Node<A>>>, Rc<Digit<A>>)
}
pub enum Digit<A> {
One(A),
@andresilva
andresilva / fingertree.rs
Created October 11, 2015 02:11
rust finger tree
use std::iter::FromIterator;
use std::rc::Rc;
pub enum FingerTree<A> {
Empty,
Single(A),
Deep(Rc<Digit<A>>, Rc<FingerTree<Node<A>>>, Rc<Digit<A>>) // TODO: the middle subtree should be lazily evaluated
}
use fingers::FingerTree::{Empty, Single, Deep};
@andresilva
andresilva / string.scala
Created July 6, 2015 17:17
JVM string decoding performance
package benchmark
import java.nio._
import java.util.concurrent.TimeUnit
import org.openjdk.jmh.annotations._
object Random {
def random = new scala.util.Random(0)
def nextString(size: Int) = random.nextString(size)
@andresilva
andresilva / gist:03686fa60c0431a7e51b
Created June 22, 2015 15:00
Scala Type Inference
type Foo[A] = A => Long => Int
implicit def foo(s: String)(l: Long) = 3
implicitly[Foo[String]] // Works
implicit def foo(s1: String, s2: String)(l: Long): Int = 3
implicitly[Foo[(String, String)]] // Doesn't work
implicitly[(String, String) => Long => Int] // Works :-(
implicit def foo(t: (String, String))(l: Long): Int = 3
@andresilva
andresilva / rbtree.rs
Last active December 4, 2018 06:00
rust pattern match rc
use std::rc::Rc;
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Color {
Black,
Red
}
use Color::{Black, Red};
@andresilva
andresilva / gist:310a96fae981c6af71c0
Created February 10, 2015 22:02
LambdaLisp random bot
DUM 200
LDC 0
LDF 7
AP 1
LDF 18
RAP 200
RTN
LDC 0
LD 0 0
LDC 199