Skip to content

Instantly share code, notes, and snippets.

View jesperdj's full-sized avatar

Jesper de Jong jesperdj

View GitHub Profile
@jesperdj
jesperdj / HexUtil.java
Last active August 29, 2015 14:21
Utility class for parsing and formatting hex strings to / from byte arrays.
/*
* Copyright 2015 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 / 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 / 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 / PackageUtils.java
Last active September 30, 2015 13:31
Utilities for working with Java packages.
/*
* Copyright 2014 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
@jesperdj
jesperdj / Example.java
Created December 13, 2016 16:37
An enum-like class with a type parameter and a values() method.
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;
public final class Example<T> {
public static final Example<Integer> INT = new Example<>(Integer.class, () -> 123);
public static final Example<String> STR = new Example<>(String.class, () -> "Test");