Skip to content

Instantly share code, notes, and snippets.

View oxbowlakes's full-sized avatar

Christopher Marshall oxbowlakes

View GitHub Profile
@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 / 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 / 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
// ^ ^ ^^
import scalaz._;import Scalaz._; import concurrent._
/* Imagine you have some expensive calculations you wish to make: */
trait RawApi {
def expensiveInt: Int = Int.MaxValue
def expensiveString: String = "expensive"
}
/* You wish to write a program using this API */
case class Result(i: Int, s: String)
@oxbowlakes
oxbowlakes / OxbowSolarizedDark2.icls
Last active December 19, 2015 14:09
OxbowSolarizedDark2
<?xml version="1.0" encoding="UTF-8"?>
<scheme name="OxbowSolarizedDark2" version="124" parent_scheme="Default">
<option name="LINE_SPACING" value="1.2" />
<option name="EDITOR_FONT_SIZE" value="13" />
<option name="EDITOR_FONT_NAME" value="Consolas" />
<colors>
<option name="ADDED_LINES_COLOR" value="" />
<option name="ANNOTATIONS_COLOR" value="2b36" />
<option name="ANNOTATIONS_MERGED_COLOR" value="" />
<option name="CARET_COLOR" value="dc322f" />
import java.math.BigInteger
/**
* Clients can use this class instead, which means they only need fill in the
* V compute(K) method
*/
abstract class ConcCache2<K, V> {
private final ConcCache<K, V> cache = new ConcCache<>();
@oxbowlakes
oxbowlakes / fewchaz.scala
Created January 28, 2014 11:45
Construct a scala future on top on a standard Java Executor service
package fewchaz
import scala.util.{Failure, Success, Try}
import scala.concurrent.{CanAwait, ExecutionContext}
import java.util.concurrent._
import scala.util.control.NonFatal
trait Fewcha[T] extends scala.concurrent.Future[T] {
def futr: Future[T]
private[this] final def toTry: Try[T] = try util.Success(futr.get()) catch { case NonFatal(t) => util.Failure(t)}
@oxbowlakes
oxbowlakes / furnace.scala
Last active January 4, 2016 22:19
For FPX 2014
//requires scala 2.10.x
//requires scalaz-2.10-7.0.x
package fpx
// Furnace types (in reality, these come from a Java API and are real!)
trait FurnaceService {
def createSession(user: String, password: String): FurnaceSession
@oxbowlakes
oxbowlakes / BrokenTraversableOnce.scala
Last active January 13, 2016 20:04
Demonstration of a broken TraversableOnce implementation of reduceLeft
//I have a Java interface which looks like this
trait ConsumableResource[A <: AnyRef] extends AutoCloseable {
/** returns `null` when we are ended */
def take(): A
}
//I would like to make this viewable as a scala `TraversableOnce` where the resource is automatically closed at the end of the traversal
scala> val it = Traversable(1); it foreach (i => println(s"$i and ${it.isEmpty}"))
1 and false
scala> val it = Iterator(1); it foreach (i => println(s"$i and ${it.isEmpty}"))
1 and true