Skip to content

Instantly share code, notes, and snippets.

View davegurnell's full-sized avatar
🐱

Dave Pereira-Gurnell davegurnell

🐱
View GitHub Profile
@davegurnell
davegurnell / macro.scala
Last active August 29, 2015 14:02
Scala macro / for comprehension issue
import scala.language.experimental.macros
import scala.reflect.macros.blackbox.Context
object PrintForMacros {
def printFor[A](expr: A): A =
macro PrintForMacros.printForMacro[A]
}
class PrintForMacros(val c: Context) {
import c.universe._
@davegurnell
davegurnell / di.sjs
Last active August 29, 2015 14:03
sweet.js macro for inserting angular.js dependency injection annotations
macro di {
case {
_(function ($args (,) ...) {
$body...
})
} => {
letstx $names... = _.map(
#{ $args... },
function(ident) {
return makeValue(ident.token.value, ident);
@davegurnell
davegurnell / ConstructorBasedDependencyInjection.scala
Last active August 29, 2015 14:05
Simple dependency injection
trait DatabaseLayer {
def dependency: Int
}
trait ConcreteDatabaseLayer extends DatabaseLayer {
def dependency: Int = 100
}
trait DatabaseResource1 {
this: DatabaseLayer =>
@davegurnell
davegurnell / error-handling-in-scala.md
Created September 5, 2014 10:25
Error handling in Scala

Error Handling in Scala

Scala does not have checked exceptions like Java, so you can't do soemthing like this to force a programmer to deal with an exception:

public void stringToInt(String str) throws NumberFormatException {
  Integer.parseInt(str)
}
@davegurnell
davegurnell / PhpValue.scala
Created October 3, 2014 11:56
Idea for modelling serialized PHP data in Scala
sealed trait PhpValue {
def \(field: PhpValue): Seq[PhpValue] = Seq.empty[PhpValue]
def \\(field: PhpValue): Seq[PhpValue] = Seq.empty[PhpValue]
}
sealed trait PhpArrayLike extends PhpValue {
def fields: Map[PhpValue, PhpValue]
override def \(field: PhpValue): Seq[PhpValue] =
fields.toSeq.collect {
@davegurnell
davegurnell / BigData.scala
Last active September 14, 2021 04:53
Slick database mapping of >22 columns using HLists.
/* ===== NOTE =====
*
* This code sample is out-of-date!
*
* If you're looking for support for shapeless HLists in Slick queries,
* check out [Slickless](https://github.com/underscoreio/slickless).
*
* ================
*/
import slick.driver.MySQLDriver.simple._
@davegurnell
davegurnell / publish-github.sh
Created November 11, 2014 16:46
Shell script: publish examples and solutions to github without their histories
#!/usr/bin/env bash
git checkout master
git branch -D nohistory
for branch in `git branch | cut -c2-`
do
echo "========== $branch =========="
git checkout $branch
git branch -D nohistory
@davegurnell
davegurnell / build.sbt
Created November 12, 2014 16:29
Sample build.sbt to get Play to load different configs for test and dev
lazy val chat = project.in(file(".")).enablePlugins(PlayScala)
scalaVersion in chat := "2.11.4"
libraryDependencies in chat ++= Seq(
jdbc,
anorm,
"org.postgresql" % "postgresql" % "9.3-1101-jdbc4",
"org.webjars" % "bootstrap" % "3.0.2",
"org.scalatestplus" %% "play" % "1.2.0" % "test"
@davegurnell
davegurnell / anorm.scala
Last active August 14, 2023 16:42
A short guide to Anorm
/*
Overview
--------
To run a query using anorm you need to do three things:
1. Connect to the database (with or without a transaction)
2. Create an instance of `anorm.SqlQuery` using the `SQL` string interpolator
3. Call one of the methods on `SqlQuery` to actually run the query
@davegurnell
davegurnell / scala-dojo.scala
Created February 19, 2015 20:00
Notes on error handling and monads from London Scala Dojo on 19th February 2015
def userId(username: String): Option[Int] =
Some(1)
def mostRecentOrderId(userId: Int): Option[Int] =
Some(2)
def orderCost(orderId: Int): Option[Int] =
Some(3)
def mostRecentOrderCost(username: String): Option[Int] =