Skip to content

Instantly share code, notes, and snippets.

View jesperdj's full-sized avatar

Jesper de Jong jesperdj

View GitHub Profile
@jesperdj
jesperdj / so-topic-3223088.scala
Created July 11, 2010 15:13
Scala: Structural type that refers to itself
// See StackOverflow: http://stackoverflow.com/questions/3201577/scala-how-to-define-a-structural-type-that-refers-to-itself/3223088#3223088
object Example {
trait Multipliable[X] { def *(d: Double): X }
trait Addable[X] { def +(x: X): X }
trait Interpolable[X] extends Multipliable[X] with Addable[X]
implicit def double2interpolable(d: Double): Interpolable[Double] = new Interpolable[Double] {
@jesperdj
jesperdj / MersenneTwister.scala
Created March 25, 2011 22:28
Mersenne Twister - Random number generator
// Mersenne Twister 19937
// Based on code from: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
// Note: This implementation is not thread-safe!
final class MersenneTwister (seed: Int = 5489) {
private val N = 624
private val M = 397
private val MatrixA = 0x9908b0dfL
private val UpperMask = 0x80000000L
@jesperdj
jesperdj / BeanAsMap.java
Created March 26, 2011 07:30
Method to make a Java bean look like a Map.
/*
* Copyright 2011 Jesper de Jong
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@jesperdj
jesperdj / gist:888123
Created March 26, 2011 08:03
Type classes for a generic interpolate method
// See: http://www.scala-notes.org/2010/08/a-generic-interpolate-method-using-type-classes/
// Trait for types that can be multiplied with a T, resulting in an R
trait Multipliable[-T, +R] {
def *(value: T): R
}
// Trait for types to which a T can be added, resulting in an R
trait Addable[-T, +R] {
def +(value: T): R
@jesperdj
jesperdj / gist:888168
Created March 26, 2011 09:46
Fisher-Yates shuffle
// Randomly permutate an array - Fisher-Yates shuffle (see: http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle)
// This method can also take a custom swap method, which is useful for example for Latin hypercube sampling
def shuffle[@specialized(Double) T](array: Array[T], swap: (T, T) => (T, T) = { (a: T, b: T) => (b, a) }): Array[T] = {
val random = new scala.util.Random
for (n <- array.length - 1 to 0 by -1) {
val k = random.nextInt(n + 1)
val (a, b) = swap(array(k), array(n)); array(k) = a; array(n) = b
}
@jesperdj
jesperdj / ResultSetHandler.java
Created March 26, 2011 18:17
Simple callback framework for running SQL statements and handling the results.
/*
* Copyright 2011 Jesper de Jong
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@jesperdj
jesperdj / vecmath.hpp
Created June 10, 2011 17:51
Simple vector math classes with SSE-optimized implementation (C++).
/*
* Copyright 2011 Jesper de Jong
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@jesperdj
jesperdj / FizzBuzz.hs
Created February 29, 2012 10:03
FizzBuzz
-- FizzBuzz in Haskell
import Data.Maybe
fizz x = if x `mod` 3 == 0 then Just "Fizz" else Nothing
buzz x = if x `mod` 5 == 0 then Just "Buzz" else Nothing
ops = [fizz, buzz]
result = map (\x -> let s = concat (mapMaybe ($ x) ops) in if null s then show x else s) [1..100]
@jesperdj
jesperdj / Primes.scala
Created March 14, 2012 14:58
Simple prime number utilities
// Simple prime number utilities
object Primes {
private val buffer = scala.collection.mutable.ArrayBuffer(2)
// Given the primes ps, check if x is a prime by trial division
private def check(ps: Traversable[Int])(x: Int) = {
val limit = math.sqrt(x).ceil.toInt
ps takeWhile { _ <= limit } forall { x % _ != 0 }
}
@jesperdj
jesperdj / Option.java
Last active December 15, 2015 04:19
An Option class in Java.
/*
* Copyright 2013 Jesper de Jong
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software