Skip to content

Instantly share code, notes, and snippets.

View rcoh's full-sized avatar

Russell Cohen rcoh

View GitHub Profile
@rcoh
rcoh / nothrowable.scala
Created April 18, 2014 23:41
Why you should never catch throwable.
def inlineme(f: => Int): Int = {
try {
f
inlineme {
return 0
}
} catch {
case ex: Throwable => println(ex); return 1
}
}
@rcoh
rcoh / therm.nut
Last active August 29, 2015 14:00
setPoint <- 68;
function updateTemp() {
// Read the ambient temperature
temp <- readTemp();
// If it's below what it should be, turn on the heat.
if (temp < setPoint) {
heatOn();
} else {
@rcoh
rcoh / tmp102.nut
Last active August 29, 2015 14:00
TMP 102 Electric IMP i2c
// Adapted from:
// http://techgurka.blogspot.com/2013/04/quick-temperature-graph-using-electric.html
class TemperatureSensor {
i2cPort = null;
constructor(port) {
i2cPort = port;
i2cPort.configure(CLOCK_SPEED_100_KHZ);
}
// Retrieve temperature (from local sensor) in deg F
@rcoh
rcoh / post1complete.nut
Last active August 29, 2015 14:00
Complete code after Post 1
// Adapted from:
// http://techgurka.blogspot.com/2013/04/quick-temperature-graph-using-electric.html
class TemperatureSensor {
i2cPort = null;
constructor(port) {
i2cPort = port;
i2cPort.configure(CLOCK_SPEED_100_KHZ);
}
// Retrieve temperature (from local sensor) in deg F
@rcoh
rcoh / scalaclosures.scala
Last active August 29, 2015 14:00
Scala Nested Closures
def inlineMeAgain[T](f: => T): T = {
f
}
def inlineme(f: => Int): Int = {
try {
inlineMeAgain {
return f
}
} catch {
@rcoh
rcoh / dangerouspattern.scala
Last active August 29, 2015 14:00
A dangerous pattern
try {
aDangerousFunction()
} catch {
case ex: Throwable => println(ex)
// Or even worse
case ex => println(ex)
}
@rcoh
rcoh / blog1.scala
Created August 18, 2014 20:33
blog1.scala
abstract class RegexExpr
// ., a, b
case class Literal(c: Char) extends RegexExpr
// a|b
case class Or(expr1: RegexExpr, expr2: RegexExpr) extends RegexExpr
// ab -> Concat(a,b); abc -> Concat(a, Concat(b, c))
case class Concat(first: RegexExpr, second: RegexExpr) extends RegexExpr
@rcoh
rcoh / regexblog2.scala
Created August 18, 2014 20:37
regexblog2
object RegexParser extends RegexParsers {
def charLit: Parser[RegexExpr] = ("""\w""".r | ".") ^^ {
char => Literal(char.head)
}
@rcoh
rcoh / regexblog2.scala
Created August 18, 2014 20:37
regexblog2
object RegexParser extends RegexParsers {
def charLit: Parser[RegexExpr] = ("""\w""".r | ".") ^^ {
char => Literal(char.head)
}
@rcoh
rcoh / regexblog2.scala
Created August 18, 2014 20:37
regexblog2
object RegexParser extends RegexParsers {
def charLit: Parser[RegexExpr] = ("""\w""".r | ".") ^^ {
char => Literal(char.head)
}