Skip to content

Instantly share code, notes, and snippets.

View JohnMurray's full-sized avatar

John Murray JohnMurray

View GitHub Profile
@JohnMurray
JohnMurray / Mail.scala
Created February 7, 2017 17:35 — forked from mariussoutier/Mail.scala
Sending mails fluently in Scala
package object mail {
implicit def stringToSeq(single: String): Seq[String] = Seq(single)
implicit def liftToOption[T](t: T): Option[T] = Some(t)
sealed abstract class MailType
case object Plain extends MailType
case object Rich extends MailType
case object MultiPart extends MailType
extern crate iron;
extern crate mysql;
use iron::prelude::*;
use iron::status;
use iron::{Handler};
struct Router {
routes: HashMap<String, Box<Handler>>
}
@JohnMurray
JohnMurray / silly-use-of-threads.java
Created March 17, 2016 00:14
Silly Java Threads Question - MOAR THREADS!!!!
for (int i = 0; i < 10_000; i++) {
Thread t = new Thread(() -> System.out.println("doing something"));
t.start();
t.join();
}
// Eventually throws java.lang.OutOfMemoryError: unable to create new native thread
@JohnMurray
JohnMurray / Build.scala
Created November 19, 2015 16:42
AutoPlugin not overriding
import sbt._
import sbt.Keys._
object MyPlugin extends AutoPlugin {
override lazy val projectSettings: Seq[Setting[_]] = Seq(
resourceDirectory in Compile := file("/tmp")
)
}
object MyBuild extends Build {
package trees
object Main extends App {
trait Node
case class InnerTreeNode ( left:Node, right:Node, value:Int ) extends Node
case object StopNode extends Node
def leaf(x: Int): Node = InnerTreeNode(StopNode, StopNode, x)
@JohnMurray
JohnMurray / set.scala
Created October 28, 2014 17:34
Extract from BuiltinCommands object in SBT
def set = Command(SetCommand, setBrief, setDetailed)(setParser) {
case (s, (all, arg)) =>
val extracted = Project extract s
import extracted._
val dslVals = extracted.currentUnit.unit.definitions.dslDefinitions
// TODO - This is possibly inefficient (or stupid). We should try to only attach the
// classloader + imports NEEDED to compile the set command, rather than
// just ALL of them.
val ims = (imports(extracted) ++ dslVals.imports.map(i => (i, -1)))
val cl = dslVals.classloader(currentLoader)
@JohnMurray
JohnMurray / .travis.yml
Last active August 29, 2015 14:00
Code snippets for scala-project blog post on www.johnmurray.io
language: scala
scala:
- 2.10.3
script:
- sbt compile test:compile
- sbt scalastyle
- sbt test
jdk:
- oraclejdk7
- openjdk7
@JohnMurray
JohnMurray / linked-list.rs
Created January 29, 2014 12:39
Simple linked-list struct in Rust that doesn't work.
struct Node<T> {
value: T,
next: Option<~Node<T>>
}
struct LinkedList<T> {
head: ~Node<T>,
tail: ~Node<T>
}
Object Util {
def dangerousString(str: String) : Future[String] = {
liveDangerously()
Future.successful(str)
}
// now becomes
def dangerousString(str: String) : Future[String] = {
@JohnMurray
JohnMurray / timing.scala
Created January 22, 2014 17:04
Simple timing wrapper for synchronous operations
def timing[A](x: => A): Int = {
import com.github.nscala_time.time.Imports._
import org.joda.time.PeriodType
val then = LocalDateTime.now
x
new Period(then, LocalDateTime.now, PeriodType.millis).getValue(0)
}