Skip to content

Instantly share code, notes, and snippets.

class !<:<[A, B]
implicit def any[A, B]: A !<:< B = new !<:<[A, B]
implicit def sub1[A, B >: A]: A !<:< B = new !<:<[A, B]
implicit def sub2[A, B >: A]: A !<:< B = new !<:<[A, B]
abstract class Tag
abstract class Unlimited
abstract class Unselected
abstract class Unskipped
@jliszka
jliszka / gist:2880013
Created June 6, 2012 04:55
Multiple phantom types in a single type parameter
object test {
sealed trait Limited
sealed trait Unlimited
sealed trait Lim extends Limited with Unlimited
sealed trait Skipped
sealed trait Unskipped
sealed trait Sk extends Skipped with Unskipped
sealed trait Limited
sealed trait Unlimited
sealed trait Lim extends Limited with Unlimited
sealed trait Skipped
sealed trait Unskipped
sealed trait Sk extends Skipped with Unskipped
sealed trait Selected
sealed trait Unselected
class !<:<[A, B]
implicit def any[A, B]: A !<:< B = new !<:<[A, B]
implicit def sub1[A, B >: A]: A !<:< B = new !<:<[A, B]
implicit def sub2[A, B >: A]: A !<:< B = new !<:<[A, B]
sealed trait ShardKeyNotSpecified
sealed trait ShardAware
trait Sharded
object Injection {
// A table of constant 0 functions
def zeroes: Int => (Int => Int) = (n1: Int) => (n2: Int) => 0
// Update an entry in a function table with a new function at the given position.
def update(t: Int => (Int => Int), k: Int, f: Int => Int): Int => (Int => Int) = {
(n: Int) => if (n == k) f else t(n)
}
def diag(hinv: Int => (Int => Int)): Int => Int = {
abstract class Dual(val rank: Int) {
self =>
// Cell value accessor
protected def get(r: Int, c: Int): Double
// Memoizing cell value accessor
def apply(r: Int, c: Int): Double = memo.getOrElseUpdate(r - c, self.get(r, c))
// The memo table
class Poly(coeffs: Int => Double) {
// Memoizing coefficient accessor. Returns the coefficient for x^n.
def apply(n: Int): Double = memo.getOrElseUpdate(n, this.coeffs(n))
// The memo table
private val memo = scala.collection.mutable.HashMap[Int, Double]()
def +(that: Poly): Poly = new Poly(n => this(n) + that(n))
import Data.List
import Test.QuickCheck
import qualified Data.Map as Map
nats = [1..]
divides a b = b `mod` a == 0
sieve (n:t) m = case Map.lookup n m of
Nothing -> n : sieve t (Map.insert (n*n) [n] m)
Just ps -> sieve t (foldl reinsert (Map.delete n m) ps)
where
import Data.List
import Test.QuickCheck
import qualified Data.Map as Map
nats = [1..]
divides a b = b `mod` a == 0
sieve (n:t) m = case Map.lookup n m of
Nothing -> n : sieve t (Map.insert (n*n) [n] m)
Just ps -> sieve t (foldl reinsert (Map.delete n m) ps)
where
@jliszka
jliszka / Examples.scala
Last active December 31, 2015 17:09
Code for "Bayesian models and causality"
/*
Setup:
$ git clone https://github.com/jliszka/probability-monad.git
$ cd probability-monad
$ ./sbt console
scala> :load Examples.scala
*/
import probability_monad._
import probability_monad.Distribution._