Skip to content

Instantly share code, notes, and snippets.

View eboto's full-sized avatar

Erem Boto eboto

  • San Francisco Bay Area
View GitHub Profile
@eboto
eboto / scala_composition.scala
Created July 6, 2012 18:43
A demonstration of how to make composition a first (or 1.5th) class citizen in Scala OO code
// A demonstration of how to make composition a first (or 1.5th) class
// citizen in Scala OO code, as it is better to favor composition
// over inheritance.
//
// The basic idea is to create a sister trait named "MyTraitComposition" to your
// base trait. The composition trait specifies an abstract member "delegate"
// and delegates the full MyTrait interface to that delegate. Later on,
// concrete implementations can implement MyTrait with MyTraitComposition
// to easily extend the behavior of another existing concrete implementation
// via composition.
@eboto
eboto / Test_hook_trait_composition.scala
Created July 19, 2012 23:01
Demonstrates using multiple traits to mix in multiple "Before" hooks, with an intended usage in creating re-usable and composable execution contexts for test cases.
//
// A demonstration of using multiple traits to mix in multiple "Before" hooks,
// with an intended usage in test code.
//
// The demonstration shows that multiple traits inheriting BeforeEach (or more
// germanely scalatest's BeforeAndAfterEach) can be sufficiently composable to use
// in our testing toolchain without sewing our own strait jacket.
//
object Main {
@eboto
eboto / either_for_errors.scala
Created August 8, 2012 22:32
How to use Scala's Either[A, B] sanely for error handling
//
// Demonstration on how to use Scala's Either[A, B] type to include error cases in function return values
// and eventually transform those domain errors into more useful end types, like Redirects.
//
// This demonstration uses the example of ordering a product, where ordering can fail for a variety
// of reasons including failure in our payment provider (Stripe) or failure due to insufficient
// product inventory.
//
object Main {
@eboto
eboto / make_git_test_tree.sh
Created September 11, 2012 01:08
Sets up a git repo with a particular branch layout. Use it to play with merging vs rebasing
# make_git_test_tree.sh
#
# This script sets up a git repository with a particular branch layout. Playing around with rebasing and merging
# the branches should help your understanding of git.
#
# Make sure you're in a goddamned empty directory when you execute this script. The script
# shouldn't be in the same directory.
#
# Here's an example, assuming this file is in the current working directory:
#
@eboto
eboto / authentication_nested_match_vs_for_comprehension.scala
Created September 13, 2012 19:06
Authentication: Nested match vs For Comprehension
/*
* Let's authenticate an attempted email and password against our database in two different ways
* using scala. Authentication can fail due to:
* (1) No account matching the e-mail (findByEmail(someEmail) can be either None or Some(Account))
* (2) No password being associated with the account (account.password can be either None
* or Some(Password))
* (3) The password for the account was incorrect (password.is(attempt) returns true or false)
*
* We'll take two different approaches: one using a series of pattern matches and another using
* for comprehensions. Which is more readable? Which is more idiomatic? <gruff military voice> I'm
@eboto
eboto / scala_compiler_crasher.scala
Created October 5, 2012 22:08
A doozy of a compiler crash we saw at Egraphs
// This example produces a compiler error in Scala 2.9.1 and 2.9.2. It works fine in Scala 2.10M7.
// The error text is: "java.lang.Error: symbol value blobKey does not exist in Crasher$$anonfun$1.apply"
object Crasher {
def main(args: Array[String]) {
println("This will never be printed because the compiler will crash")
}
}
class Crasher { // this can be a trait, class, object, whatever
@eboto
eboto / testable_code.scala
Created October 16, 2012 19:11
Examples of writing testable code using traits
trait MyService {
def metricProvider: OtherService1
// My work goes below
def getMetrics: Seq[Metric] {
// code that actually uses dep1 and dep2 to get metrics
}
}
object MyService extends MyService {
@eboto
eboto / futures.scala
Created October 17, 2012 23:58
Using Futures
// This is not a particularly impressive example of using futures.
def getMetrics: List[EgraphsMetric[Int]] = {
import akka.pattern.{ ask, pipe }
val futureMetrics: List[Future[EgraphsMetric[Int]]] = for (actor <- actors: List[ActorRef]) yield {
implicit val timeout = Timeout(5 seconds)
for {
metric <- ask(actor, GetMetric).mapTo[EgraphsMetric[Int]]
@eboto
eboto / play_2_assets_pipeline.scala
Created November 2, 2012 20:15
Play 2 Assets pipeline
package assetproviders {
import play.api.mvc.Action
import play.api.mvc.Controller
import play.api.mvc.AnyContent
import play.api.mvc.Call
/**
* This simple interface is meant to mimic the existing interface in Play 2.0
* for the Assets controller it provides. By implementing this it is possible
* to mix and combine various AssetProviders to create a custom Asset controller
@eboto
eboto / tb.scala
Created November 9, 2012 10:18
Example of how types could work together in toybox
package controllers
import play.api._
import play.api.mvc._
import play.api.mvc.Results._
/** Imagine that the stuff inside this package is the toybox plugin code */
package toybox {
/**