This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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] { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 |
OlderNewer