Skip to content

Instantly share code, notes, and snippets.

package ground
// Show that println and Unit are not the same.
// Why pure IO?
// Trampoline
// IO values and interpreters
object UnitIsNotUnit {
// Side effects are extremely hard to reason about.
@lazyvalue
lazyvalue / gist:1d049cb3557469883286
Created May 27, 2015 23:13
Presentation on Scala Functional IO/Trampolines
package ground
// Why pure IO?
// Trampoline
// IO values and interpreters
object UnitIsNotUnit {
// Side effects are extremely hard to reason about.
// The only way you can tell when they are executed is by following imperative code
@lazyvalue
lazyvalue / gist:833b40ba538d4f0773b2
Created July 13, 2015 23:33
scala state and io stuff
package ground
// Why pure IO?
// Trampoline
// IO values and interpreters
object UnitIsNotUnit {
// Side effects are extremely hard to reason about.
// The only way you can tell when they are executed is by following imperative code
case class NGramGroup(name: String, NGrams: Seq[NGram]) {}
case class Experience(name: String, NGramGroups: Seq[NGramGroup]) {}
case class NGram(name: String, weight: Double)
object Parser {
def parseLine(line: String) = {
val spl = line.split(" ")
val (expName, emo) = (spl(0), spl(1))
val ng = NGram(spl(2), spl(3).toDouble)
object Labels {
val FIRST_PUBLISHED = "first published by"
val CURRENT_PUBLISHER = "current publisher"
val coverOrder = List("BB", "BA", "BC", "DG", "29").reverse
val formRankMap = Map(coverOrder.zipWithIndex:_*)
val PB = "PB"
val WORLD = "WORLD"
val US = "US"
val SR3 = "03"
val SR1 = "01"
def mkDisplayImage(cIsbn13: String, refs: Seq[ProductRefView]): Option[String] = {
val l = coverOrder
val (_, isbn13) = refs.foldLeft(0, None: Option[String])((acum, r: ProductRefView) => {
val (p, tIsbn13) = acum
if (r.hasImage || checkRights(r)) {
if (r.isbn13 == cIsbn13)
((l.size + 1), Some(r.isbn13))
else if (tIsbn13.isEmpty)
(l.indexOf(r.form), Some(r.isbn13))
else if (l.indexOf(r.form) > p)
def mkDisplayImage(cIsbn13: String, refs: Seq[ProductRefView]): Option[String] = {
val l = coverOrder
val (_, isbn13) = refs.foldLeft(0, None: Option[String])((acum, r: ProductRefView) => {
val (p, tIsbn13) = acum
if (r.hasImage || checkRights(r)) {
if (r.isbn13 == cIsbn13)
((l.size + 1), Some(r.isbn13))
else if (tIsbn13.isEmpty)
(l.indexOf(r.form), Some(r.isbn13))
else if (l.indexOf(r.form) > p)
def pascal(numRows: Int) = {
type Coord = (Int, Int)
def find(where: Coord, memo: Map[Coord, Int): (Int, Map[Coord, Int]) = memo.get(where) match {
case Some(v) => v
case None => {
val prev = find((where._1 - 1, where._2 - 1), memo)
val new = // do your calculation
memo.updated(where, new)
}
}
@lazyvalue
lazyvalue / gist:3866695
Created October 10, 2012 16:24
permissions checks demo
object Permissions {
import scalaz.syntax.pointed._
trait PermissionRequired[I, T] { self =>
type Myself = this.type
def action: I => Reader[T]
def apply(check: PermissionCheck[I, Myself]): Reader[T] = {
val checkAction: Reader[I] = check()
package com.knollop.migrate
import java.sql.{DriverManager, Driver}
import org.bson.types.ObjectId
import com.mongodb.casbah.Imports._
import com.novus.salat._
import com.novus.salat.global._
import com.knollop.store._