Skip to content

Instantly share code, notes, and snippets.

@d6y
d6y / option.scala
Created July 8, 2012 18:14
Option(null) is None
for {
entry <- contacts
name <- Option(entry.getName)
full_name <- Option(name.getFullName)
birthday <- Option(entry.getBirthday)
when = birthday.getWhen
}
yield "%s: %s".format(full_name.getValue, when)
# Sendgrid.com configuration:
mail.smtp.host=smtp.sendgrid.net
mail.smtp.auth=true
mail.user=you@example.com
mail.password=letme1n
@d6y
d6y / boot.scala
Created July 8, 2012 18:24
Boot settings
import javax.mail._
def optionalAuth: Box[Authenticator] = for {
user <- Props.get("mail.user")
pass <- Props.get("mail.password")
} yield new Authenticator {
override def getPasswordAuthentication = new PasswordAuthentication(user,pass)
}
Mailer.authenticator = optionalAuth
@d6y
d6y / process.sh
Created July 11, 2012 17:58
Batch convert HTML to Markdown
#!/bin/bash
# Converts HTML from https://exportmyposts.jazzychad.net/ exports to Markdown
POSTS_DIR=/Users/richard/Desktop/d6y/posts
for file in $POSTS_DIR/*.html
do
echo $file
@d6y
d6y / TideSource.scala
Created July 21, 2012 19:05
interface
trait TideSource {
def lowsFor(day:LocalDate): List[Tide]
}
@d6y
d6y / Tide.scala
Created July 22, 2012 09:12
tide
case class Tide(when:LocalTime, height:Metre) {
override val toString = when.toString("HH:mm") + " (" + height + ")"
def forZone(destZone:DateTimeZone) = Tide(
when.toDateTimeToday(DateTimeZone.forID("GMT")).withZone(destZone).toLocalTime(),
height)
}
object VisitBrightonScraper extends VisitBrightonScraper
class VisitBrightonScraper extends TideSource {
def page = Source.fromURL( "http://www.visitbrighton.com/site/tourist-information/tide-timetables").mkString
override def lowsFor(day:LocalDate) = {
// We want the times that start with the date in this
// format: 10th May 2009
val date = day.ordinal + DateTimeFormat.forPattern(" MMM yyyy").print(day)
// Here's a method that uses the time call:
int f() throws MyException {
time("opName", { =>
// some statements that can throw MyException
});
time("opName", {=>
return ...compute result...;
});
}
interface Block {
R execute() throws X;
}
public R time(String opName, Block block) throws X {
long startTime = System.nanoTime();
boolean success = true;
try {
return block.execute();
} catch (final Throwable ex) {
def time(opname, block)
{
long start\_time = System.nanoTime()
boolean success = true
try {
return block()
} catch (Throwable ex) {
success = false;
throw ex
} finally {