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
def factorial(n: BigInt): BigInt = { | |
def fact(n: BigInt, result: BigInt): BigInt = { | |
if (n == 0) return result | |
else return fact(n - 1, result * n) | |
} | |
return fact(n, 1) | |
} |
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
// Scala analog to Clojure's loop-recur construct | |
def loopRecur[A](index: Int, coll: Seq[A], zippedList: List[(Int, A)]): List[(Int, A)] = { | |
if (coll.isEmpty) return zippedList | |
else return loopRecur(index + 1, coll.tail, zippedList ++ List((index, coll.head))) | |
} | |
// Given a sequecne of items, returns a List of tuples of the form (item index, item) | |
def zipIndex[A](coll: Seq[A]): List[(Int, A)] = { | |
return loopRecur(0, coll, List.empty[(Int, A)]) | |
} |
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
package org.klgraham; | |
/** | |
* Created by klogram on 8/5/16. | |
*/ | |
public class StringReversal { | |
public static String reverseStringIterative(final String s) { | |
StringBuilder sb = new StringBuilder(); |
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
func sign(z: Double) -> Int { | |
// Note that 0 is not considered positive or negative | |
return (z > 0) ? 1 : -1 | |
} |
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
func randomDouble() -> Double { | |
return Double(arc4random()) / Double(UINT32_MAX) | |
} | |
func createData(numPoints: Int) -> [PerceptronDataPair] { | |
var data = [PerceptronDataPair]() | |
for _ in 0..<numPoints { | |
let x = [2.0 * (randomDouble() - 0.5)] | |
let y = line(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
struct PerceptronTrainer { | |
let data: [PerceptronDataPair] | |
func train(inout p: Perceptron) -> Int { | |
var error: Int = 0 | |
for d in data { | |
error = p.backProp(d.input, output: d.output) | |
} | |
return error | |
} |
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
mutating func backProp(input: [Double], output: Int) { | |
let prediction = feedForward(input) | |
let error = output - prediction | |
for i in 0..<weights.count { | |
weights[i] += learningRate * Double(error) * input[i] | |
} | |
} |
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
func activate(bias: Double, bW: Double, x: [Double], w: [Double]) -> Int { | |
var sum = 0.0 | |
for i in 0..<x.count { | |
sum += x[i] * w[i] | |
} | |
sum += bias * bW | |
return step(sum) | |
} |
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
func activate(bias: Double, bW: Double, x: [Double], w: [Double]) -> Int { | |
var sum = 0.0 | |
for i in 0..<x.count { | |
sum += x[i] * w[i] | |
} | |
sum += bias * bW | |
return step(sum) | |
} |
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
func flatMap<B>(f: A -> Distribution<B>) -> Distribution<B> { | |
var d = Distribution<B>(get: {() -> Optional<B> in return nil}) | |
d.get = { | |
(Void) -> B in return f(self.get()!).get()! | |
} | |
return d | |
} |