Skip to content

Instantly share code, notes, and snippets.

@sam
sam / !.md
Last active December 15, 2015 12:49
Sublime Text 2 Preferences.

Sublime Text 2 Preferences.

NOTE: .json extensions added for Gist syntax highlighting. It should be stripped.

Open the following menu item: Sublime Text 2 -> Preferences -> Settings -- User and add the content of the Preferences.sublime-settings file. Tweak as you see fit. The save_on_focus_lost setting is really nice for Continuous Testing. For example, if you have an SBT ~test session running in a terminal, then when you're finish editing your code just cmd+tab over and your test starts compiling/executing. Saucy.

For the formatting key mappings, open the following menu item: Sublime Text 2 -> Preferences -> Key Bindings -- User and add the content of the Default (OSX).sublime-keymap -- User file. ctrl+f will reindent your current selection. ctrl+shift+f will reindent the whole file.

@sam
sam / !.md
Last active January 29, 2016 17:46
Quickstart for using SBT with IntelliJ IDEA.

Quickstart for using SBT with IntelliJ IDEA.

  • Create a new blank repository for your project
  • Create a project sub-folder
  • Add the following files to it (tweaking where necessary)

Now you can start sbt (at your project's root folder, not the ./project/ sub-folder), and run the gen-idea command to generate all your IntelliJ IDEA configuration files.

NOTE

@sam
sam / Tree.scala
Created March 21, 2013 16:27
Pseudo-code for the design of our NestedSet Tree representation.
case class Node(name:String, left:Int, children:Seq[Node] = Nil) {
def right = 1 + left + children.lastOption.fold(0)(_.right)
}
var root = Node("root", 1) // (name, initial-left-value)
assert(root.left, 1)
assert(root.right, 2)
root = root ++ Seq(Node("A", 0), Node("B", 4), Node("C", 27))
package util
object Slug {
def apply(input:String) = slugify(input)
def slugify(input: String): String = {
import java.text.Normalizer
Normalizer.normalize(input, Normalizer.Form.NFD)
.replaceAll("[^\\w\\s-]", "") // Remove all non-word, non-space or non-dash characters
.replace('-', ' ') // Replace dashes with spaces
@sam
sam / Model.scala
Created March 18, 2013 15:43
Definining a Generic JsonFormat. If SequentialId weren't a Generic, this would be easy. Since it is, and I want to serialize it directly to a String (not as a case-class containing an "id" field), I'm having trouble...
import spray.json._
class Model {
implicit object SequentialIdJsonFormat extends JsonFormat[SequentialId] {
def write(x: SequentialId) = JsString(x.id)
def read[T](value:JsValue):SequentialId = value match {
case JsString(x) => SequentialId[T](x)
case x => deserializationError("Expected SequentialId as JsString, but got " + x)
}
}
@sam
sam / Times.scala
Created March 15, 2013 21:17
Silly example implementing an Int.times helper method.
implicit class IntTimes(n:Int) {
def times(f: Int => Unit) { for(i <- 1 to n) f(i) }
// This one is optional. I'd leave it out personally.
// def times(f: => Unit) { for(_ <- 1 to n) f }
}
// Now you can:
10 times println
@sam
sam / Helpers.scala
Created March 13, 2013 20:05
ScalaTest Helpers for testing Futures. Scala, Currying, Pattern Matching and Functional Programming is awesome.
def whenReady[A](result:Future[A], timeout:Duration = 1 second)(expectation: A => Unit) = {
expectation(Await.result(result, timeout))
}
def tryWhenReady[A](result:Future[Try[A]], timeout:Duration = 1 second)
(failure:Throwable => Unit)
(expectation: A => Unit) = {
Await.result(result, timeout) match {
case Failure(e) => failure(e)
case Success(result:A) => expectation(result)
@sam
sam / Try.scala
Last active December 14, 2015 20:58
An example of using Try (Scala 2.10 or greater required) to encourage error handling without bubbling up exceptions. Because you have to write the pattern anyway, you might as well fill in the details on what to do if the result isn't what you expected.
import scala.util._
import concurrent._
import duration._
import ExecutionContext.Implicits.global
import java.lang.ArithmeticException
// Catch the error in your Future and return it as a Failure:
def f(divisor:Int) = future { 100 / divisor } map(Success.apply) recover {
case e:ArithmeticException => Failure(e)
}
@sam
sam / PatternMatching.scala
Last active December 14, 2015 09:50
Pattern Matching examples. Try them out in your REPL!
// One way to think about "match" is as a "map" method for arbitrary objects:
// So I can "map" a number:
10 match { case i => i * 4 }
// Mapping Tuples is especially awesome:
(1, "one") match { case (i,s) => i + 1 -> s"$s!!!" }
// Ok, maybe you're not all that impressed. Now that you know
// you can "map" a Tuple with Pattern Matching, it makes using
@sam
sam / NestedSet.scala
Created February 21, 2013 21:37
Trying to get a basic Nested Set structure defined using not terrible code.
package models
case class Channel(id:Int, title:String, hidden:Boolean, left:Int, children:Seq[Channel]) {
def right = 2
def ++(children:Seq[Channel]) = {
copy(children = children.scanLeft(this) { (previous, channel) =>
channel.copy(left = previous.right + 1)
}.tail)