Skip to content

Instantly share code, notes, and snippets.

View oxbowlakes's full-sized avatar

Christopher Marshall oxbowlakes

View GitHub Profile
@oxbowlakes
oxbowlakes / defs.scala
Created October 4, 2012 07:44
Defs in scala
// In scala a def (called a "method") is defined as follows:
def foo(arg: String): Int = expr
// ^ ^ ^^ ^ ^ ^
// ^ ^ ^^ ^ ^ an expression, whose type must match the declared return type, if specified
// ^ ^ ^^ ^ ^
// ^ ^ ^^ ^ the declared return type (optional).
// ^ ^ ^^ ^
// ^ ^ ^^ the type of each argument must be declared
// ^ ^ ^^
@oxbowlakes
oxbowlakes / vals.scala
Created October 4, 2012 07:14
Vals in Scala
// The declaration of a value in scala looks like this:
val x = 1
// ^ ^ ^
// ^ ^ the value we wish to assign our new value to
// ^ identifier (ie name of value)
// type of value (val or var)
// Each identifier in scala has an associated type (or, more accurately, it has many types ranging from the least to the most specific)
@oxbowlakes
oxbowlakes / exercises.scala
Created October 3, 2012 16:00
Scala Exercises
This is how we create an inclusive/exclusive range of Ints:
val ie = 0 until 500
//1. Fill in the missing item to create a range of Ints from 1 to 100 inclusive
val ints = 1 ??? 100
//2. Find the sum of the integers in this range
@oxbowlakes
oxbowlakes / lens-example-tradingday.scala
Created April 24, 2012 10:05
Lens/State Example to remove repetition
object TradingDays extends App {
import scalaz._
import Scalaz._
case class Trade(sym: String, trader: String, qty: Int)
case class TradingDay(symbols: Map[String, SymDay] = Map.empty)
object TradingDay {
@oxbowlakes
oxbowlakes / imperial-position.scala
Created March 13, 2012 12:54
Position example (using monoid)
object Positions {
trait Investment
trait Position {
def investment: Investment
def tradingPnL: Option[Double]
def inventoryPnL: Option[Double]
final def totalPnL = inventoryPnL → tradingPnL
}
import Monoid._
@oxbowlakes
oxbowlakes / imperial-monoid.scala
Created March 13, 2012 12:53
Monoid example
trait Monoid[A] {
def identity: A
def mplus(a1: A, a2: A): A
}
object Monoid {
implicit val IntMonoid = new Monoid[Int] {
def identity = 0
def mplus(a1: Int, a2: Int) = a1 + a2
}
@oxbowlakes
oxbowlakes / imperial-fx.scala
Created March 13, 2012 12:52
FX Rate example
import java.util.Currency
object Fx {
type CcyPair = (Currency, Currency)
case class FxRate(from: Currency, to: Currency, rate: BigDecimal) {
def pair: CcyPair = from → to
def unary_~ = FxRate(to, from, 1 / rate)
def *(that: FxRate): FxRate = {
require(this.to == that.from)
FxRate(this.from, that.to, this.rate * that.rate)
@oxbowlakes
oxbowlakes / scala-interview2.scala
Created March 2, 2012 12:08
Scala interview questions hard
trait MyList[+A] {
def fold[B](k: Option[(A, B)] => B): B
def map[B](f: A => B): MyList[B] = sys.error("Implement me in terms of fold")
def flatMap[B](f: A => MyList[B]): MyList[B] = sys.error("Implement me in terms of fold")
def headOption: Option[B] = sys.error("Implement me in terms of fold")
def tailOption: Option[MyList[B]] = sys.error("Implement me in terms of fold")
def isEmpty = sys.error("Implement me in terms of fold")
def length = sys.error("Implement me in terms of fold")
}
@oxbowlakes
oxbowlakes / scala-interview1.scala
Last active October 1, 2015 08:48
Scala interview questions
object GOption {
def some[A](a: A): GOption[A] = new GOption[A] {
def cata[B](n: => B, s: A => B): B = sys.error("Implement me")
}
def none[A]: GOption[A] = new GOption[A] {
def cata[B](n: => B, s: A => B): B = sys.error("Implement me")
}
}
trait GOption[+A] {
@oxbowlakes
oxbowlakes / gist:1869377
Created February 20, 2012 14:06
Simple scala collection examples
scala> (1 to 20).toList
res1: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
//Simple side effects
scala> res1 take 3 foreach (i => println(i))
1
2
3
scala> res1 take 3 foreach println