This file contains hidden or 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
// 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)(_ + _) |
This file contains hidden or 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
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 |
This file contains hidden or 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
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() { |
This file contains hidden or 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
/* 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 |
This file contains hidden or 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
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 |
This file contains hidden or 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
(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"}) |
This file contains hidden or 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
/** | |
* 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; |
This file contains hidden or 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
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] | |
} |
This file contains hidden or 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
protocol Parameterized { | |
associatedtype ParameterType | |
var p: ParameterType { get } | |
} |
This file contains hidden or 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
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() } | |
} | |
} |
OlderNewer