Skip to content

Instantly share code, notes, and snippets.

@danclien
danclien / router_use_example.js
Created June 30, 2011 04:15
JavaScript Router for Dojo
/*
Now to convince work to let me open-source this...
Javascript router for Dojo
* Influenced by hij1nx's SugarSkull router, RoR, and the ASP.NET MVC router
* Supports static names, regular expressions, and variables
* Automatic parsing for specified data types
* Events for first arrival (onFirst), entering (onEnter), always (on), and leaving (onLeave) a route
* Override-able controllers
import scala.concurrent.duration.Duration
import spray.routing.HttpService
import spray.routing.authentication.BasicAuth
import spray.routing.directives.CachingDirectives._
import spray.httpx.encoding._
trait LongerService extends HttpService with MyApp {
val simpleCache = routeCache(maxCapacity = 1000, timeToIdle = Duration("30 min"))
@danclien
danclien / checking_option.scala
Created August 10, 2013 21:22
Playing around with for comprehensions to check for None in Option[T].
//Setup Option[String]s
val maybeValue1 : Option[String] = Some("Value1")
val maybeValue2 : Option[String] = Some("Value2")
val maybeValue3 : Option[String] = None
// Manually checking each value
val result = maybeValue1 match {
case None => None
case value1 => maybeValue2 match {
case None => None
def wrap(in: Option[String]) : Option[String] = {
println("Running")
in
}
//Setup Option[String]s
lazy val maybeValue1 : Option[String] = wrap(Some("Value1"))
lazy val maybeValue2 : Option[String] = wrap(None)
lazy val maybeValue3 : Option[String] = wrap(Some("Value3"))
@danclien
danclien / HandlingMultipleOptions.scala
Last active December 20, 2015 22:58
Using `for` comprehensions vs only pattern matching for checking multiple Option[T] for None.
//Setup
def expensiveFunction(in: Int) : Option[Int] = {
println(s"Running expensive function for $in")
in match {
case 2 => None
case _ => Some(in)
}
}
// Test for comprehension

Had a WTF moment figuring out how Actions in Play Framework could be defined with and without an implicit request.

package controllers

import play.api.mvc._

object ApplicationController extends Controller {
  def index = Action {
 Ok("Without implicit request")
@danclien
danclien / longtroll.md
Last active December 21, 2015 21:29
Random notes for automating VirtualBox, Vagrant, Ubuntu, and nginx on Windows.
  • Download
    • VirtualBox
    • Vagrant
    • Git
  • Extract VirtualBox .exe setup to get .msi files
    • VirtualBoxInstaller.exe -extract -path C:\Setup
  • Install VirtualBox
    • virtualboxInstallerInExe.msi /passive ADDLOCAL=VBoxApplication,VBoxNetwork
    • This installs VirtualBox without prompting to install unsigned drivers that are not needed
  • Install Vagrant
@danclien
danclien / strangeloop2013.md
Last active December 25, 2015 13:29
Strange Loop 2003 Recommendations
@danclien
danclien / CakePatternTest.scala
Last active December 26, 2015 04:39
Attempting to do dependency injection using the Cake Pattern – `Car` has an `Engine`. Choice of two different engines that's wired with a car under "Example usage" at the bottom.
/*
Dependency injection test using the Cake Pattern
Notes:
* `Component` wrapper traits
* Allows multiple `Component`s to be composed together
* Each of these classes must have their own `Component` trait:
* Dependency interface
* Dependency implementation
* Dependent implementation (class using a dependency)
@danclien
danclien / HelloCakePattern.scala
Last active December 26, 2015 04:39
Port of "Hello StructureMap" in C# over to the Cake Pattern in Scala. From https://gist.github.com/joshuaflanagan/397213
object HelloCakePattern {
Container.appEngine.run
object Container
extends AppEngineComponent
with EnglishGreeterComponent
with ConsoleOutputDisplayComponent {
val appEngine = new AppEngine
val greeter = new EnglishGreeter
val outputDisplay = new ConsoleOutputDisplay