Skip to content

Instantly share code, notes, and snippets.

@klgraham
klgraham / functionalScala1.scala
Created August 28, 2012 04:58
Some *basic* functional scala
// x = Range(1, ..., 10)
val x = 1 to 10
x.sum
// two ways to fold left
x.foldLeft(0)(_ + _)
(0 /: x)(_ + _)
// two ways to fold right
x.foldRight(0)(_ + _)
@klgraham
klgraham / Complex.scala
Created September 7, 2012 04:05
Complex arithmetic with Scala
import math.sqrt
class Complex (val real: Double, val imag: Double) {
def +(that: Complex): Complex = new Complex(this.real + that.real, this.imag + that.imag)
def -(that: Complex): Complex = new Complex(this.real - that.real, this.imag - that.imag)
val mod = (z: Complex) => math.sqrt(z.real * z.real + z.imag * z.imag)
def *(that: Complex): Complex = {
val real = this.real * that.real - this.imag * that.imag
@klgraham
klgraham / Complex.java
Created September 7, 2012 04:15
Complex arithmetic in Java
public class Complex {
private double real;
private double imag;
public Complex(final double real, final double imag) {
this.real = real;
this.imag = imag;
}
public double real() {
@klgraham
klgraham / gist:3768168
Created September 22, 2012 23:09
Call-by-name vs call-by-value in Scala
/* call-by-value means the parameters are evaluated left to
** right to determine their value before the function itself
** is evaluated
*/
def first(a: Int, b: Int): Int = a
first(3 + 4, 5 + 6) // will be reduced to first(7, 5 + 6), then first(7, 11), and then 7
/* call-by-name means the paramter is passed into the function
** as is. Parameter evaluation takes place after
** substitution
@klgraham
klgraham / gist:3768172
Created September 22, 2012 23:11
Advantage of occasional call-by-name
def loop: Int = loop // this is allowed
def first(a: Int, b: Int): Int = a
def first1(a: Int, b: => Int): Int = a
first(3 + 4, loop) // this will fail
first1(3 + 4, loop) // this will return 7
(ns testsite.views.welcome
(:require [monger.core :as mg]
[testsite.login :as auth]
[testsite.views.common :as common])
(:use [monger.result :only [ok?]]
[monger.collection :only [insert find-maps find-one-as-map]])
(:import [org.bson.types ObjectId]))
;;;; MongoDB interaction functions
(def ^:const record-types #{"book"})
@klgraham
klgraham / Distributions.java
Created March 25, 2016 19:10
Probability monad in Java 8
/**
* How to use Java 8 collections to create the probability monad.
*
* Created by klgraham on 10/25/15.
*/
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
protocol Stochastic {
// the type of value stored in the distribution
associatedtype ValueType
// Sample a single value from the distribution
func get() -> ValueType
// Sample n values from the distribution
func sample(n: Int) -> [ValueType]
}
protocol Parameterized {
associatedtype ParameterType
var p: ParameterType { get }
}
struct UniformDoubleDist: Stochastic {
// Returns a uniform double on [0,1]
func get() -> Double {
return drand48()
}
func sample(n: Int) -> [Double] {
return (1...n).map { x in get() }
}
}