Skip to content

Instantly share code, notes, and snippets.

View oreillyross's full-sized avatar
🏠
Working from home

Faktor 10 oreillyross

🏠
Working from home
View GitHub Profile
@oreillyross
oreillyross / describe_type_variance.scala
Created August 5, 2015 10:56
In Scala, arrays are invariant by default and immutable collections (or container types) are covariant or [+A]. Since they are immutable all of your potential type errors will be discovered during compile time as opposed to runtime. Another possible option is to define a container as contravariant ([-A]). Contravariance means that a container wi…
case class InvariantContainer[A]()
case class CovariantContainer[+A]()
case class ContravariantContainer[-A]()
class Person
class User extends Person
class Admin extends User
val inv1: InvariantContainer[User] = InvariantContainer[User] // works
@oreillyross
oreillyross / SBTbuild
Created July 13, 2015 11:14
SBT uses the same directory structure as Maven, and for simple needs, you can generate a compatible structure using a shell script. For example, the following Unix shell script creates the initial set of files and directories you’ll want for most projects
#!/bin/sh
mkdir -p src/{main,test}/{java,resources,scala}
mkdir lib project target
# create an initial build.sbt file
echo 'name := "MyProject"
version := "1.0"
scalaVersion := "2.10.0"' > build.sbt
@oreillyross
oreillyross / Scala.yaml
Created July 9, 2015 04:02
.gitignore for Scala
*.class
*.log
# sbt specific
.cache
.history
.lib/
dist/*
target/
lib_managed/
@oreillyross
oreillyross / MongoFactory.scala
Created June 30, 2015 11:05
This object helps to simplify the interactions with a MongoDB database.
import com.mongodb.casbah.MongoCollection
import com.mongodb.casbah.MongoConnection
object MongoFactory {
private val SERVER = "localhost"
private val PORT = 27017
private val DATABASE = "portfolio"
private val COLLECTION = "stocks"
val connection = MongoConnection(SERVER)
val collection = connection(DATABASE)(COLLECTION)
@oreillyross
oreillyross / web.xml
Created June 30, 2015 09:38
Scalatra provides a nice way of getting you out of the business of declaring your servlets and servlet mappings in the web.xml file. Simply create a boilerplate web.xml file like this in the src/main/webapp/WEB-INF directory
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee ↵
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<listener>
<listener-class>org.scalatra.servlet.ScalatraListener</listener-class>
</listener>
<servlet-mapping>
@oreillyross
oreillyross / html5.html
Created June 29, 2015 12:34
Default html5 page
<!DOCTYPE html>
<head>
<html lang="en">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>HTML5 Structure</title>
<!-- Fonts -->
<link href='http://fonts.googleapis.com/css?family=Lato&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
<!-- Bootstrap -->
@oreillyross
oreillyross / QSort.scala
Created May 20, 2015 09:34
qsort algorithm in Scala
def qsort[T <% Ordered[T]](list: List[T]): List[T] = {
list match {
case Nil => Nil
case x:: xs =>
val (before, after) = xs partition (_ < x )
qsort (before) ++ (x :: qsort(after))
}
name := "My Project" version := "1.0" scalaVersion := "2.11.6" libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.4-SNAPSHOT"
  /** The strict identity type constructor. Can be thought of as `Tuple1`, but with no   *  runtime representation.   */  type Id[+X] = X
sealed trait Free[F[_], A]
final case class Return[F[_], A](a: A) extends Free[F, A]
final case class Suspend[F[_], A](s: F[Free[F, A]]) extends Free[F, A]