Skip to content

Instantly share code, notes, and snippets.

View jroper's full-sized avatar

James Roper jroper

View GitHub Profile
@jroper
jroper / After.scala
Created March 5, 2015 21:55
ERQX router before and after Play 2.4 upgrade
class BlogRouter(controller: BlogController) extends SimpleRouter {
object ? { def unapply[A](a: A) = Some(a, a) }
def routes = {
// Index
case GET(p"/" | p"") ? Page(page) => controller.index(page)
// View single blog post
case GET(p"/${int(year)}<\d{4}>/${int(month)}<\d{2}>/${int(day)}<\d{2}>/$permalink.html") =>
@jroper
jroper / dsl.scala
Created March 5, 2015 22:06
Play query string DSL possibilities
// In the path string:
case GET(p"/foo?param1=${param1}&param2=${param2}")
// Problems: doesn't support extracting lists of parameters or optional parameters
// Using ? and & extractors to combine multiple string interpolated query string parameters:
case GET(p"/foo" ? q"required=${required}" & q?"optional=${optional}" & q*"many=${many}")
// I think I like this one, q extracts String, q? extracts Option[String], q* extracts List[String]
// One question, how strict should we make it?
@jroper
jroper / Attempts.scala
Created June 9, 2015 14:16
Reader monads
import scalaz.Reader
case class User(id: Int, name: String)
case class Interest(name: String)
trait Database
trait Attempt1 {
// How every explanation of Reader monad I've seen/read goes:
// TEST 1:
// Without scrolling down, can you work out whether bar() is the return value, or a side effecting
// method call?
def test1(): Boolean = {
if (foo) {
bar()
} else {
/*
It is a truth universally acknowledged, that a single man in possession
of a good fortune, must be in want of a wife.
@jroper
jroper / ChatSystem.scala
Last active March 22, 2024 09:14
Akka streams multiple chat room example
import akka.stream._
import akka.stream.scaladsl.{BroadcastHub, Flow, Keep, MergeHub, Sink, Source}
import akka.stream.stage.{GraphStageLogic, GraphStageWithMaterializedValue, InHandler, OutHandler}
import scala.collection.concurrent.TrieMap
sealed trait ChatEvent {
def room: String
}
case class ChatMessage(user: String, room: String, message: String) extends ChatEvent
@jroper
jroper / SourceWithBackoffSupervision.scala
Last active March 12, 2018 14:22
Akka streams Source.restartWithBackoff
package streams.utils
import java.util.concurrent.ThreadLocalRandom
import akka.NotUsed
import akka.stream._
import akka.stream.scaladsl.Source
import akka.stream.stage._
import scala.concurrent.duration._
@jroper
jroper / LazyBroadcastHub.scala
Created July 15, 2017 11:06
Akka streams LazyBroadcastHub - a broadcast hub that only keeps its source materialized as long as there are consumers
package streams.utils
import akka.NotUsed
import akka.stream._
import akka.stream.scaladsl.{BroadcastHub, Keep, RunnableGraph, Source}
import akka.stream.stage._
import scala.concurrent.duration.{Duration, FiniteDuration}
/**
@jroper
jroper / Angular2.scala
Last active May 19, 2021 15:00
Play Angular 2 integration
import play.sbt.PlayRunHook
import sbt._
import java.net.InetSocketAddress
object Angular2 {
def apply(log: Logger, base: File, target: File): PlayRunHook = {
object Angular2Process extends PlayRunHook {
private var watchProcess: Option[Process] = None
@jroper
jroper / keybase.md
Created September 19, 2017 02:43
keybase.md

Keybase proof

I hereby claim:

  • I am jroper on github.
  • I am jroper (https://keybase.io/jroper) on keybase.
  • I have a public key ASDmToLnfJ0iYnejVzX5FIoZNvTJi_A9SX3kF12i-1pbwgo

To claim this, I am signing this object:

@jroper
jroper / cua-wac-enabler.js
Created November 14, 2017 23:42
CUA WAC password field enabler - for use with password managers.
// ==UserScript==
// @name CUA WAC password field enabler
// @namespace https://jazzy.id.au
// @version 0.1
// @description Enable the WAC password field so that a password manager can fill it.
// @author James Roper
// @match https://ob.cua.com.au/ib/*/*Login*.action
// @grant none
// ==/UserScript==