Skip to content

Instantly share code, notes, and snippets.

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)
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)
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 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._
// Publish is performed on an existing post and if no post exists a new one will be created
case class Publish(postRef: PostRefs.ByPermalink, requesterRef: UserRef) extends
WriteAction[(PostRefs.ByPermalink, ModerationStatuses.ModerationStatus, PublicationStatuses.PublicationStatus)] {
// TODO: Test that this user has the permission to publish this draft
override def validate = requesterRef.validate ++ postRef.validate
def implementation = for {
isPublished <- PostActions.IsPublished(postRef).eval
p <- DraftStore.publish(postRef)
@lazyvalue
lazyvalue / gist:4432284
Created January 2, 2013 04:54
Demonstration of the Reaper Pattern for Actor Shutdown
package com.knollop.migrate
import java.sql._
import scala.collection.mutable.ArrayBuffer
import com.mongodb.casbah.Imports._
import com.mongodb.casbah.WriteConcern
import akka.dispatch.Dispatchers
>>> myList = [4, 3, 5, 9, 12]
>>> map(lambda x: x + 2, myList)
[6, 5, 7, 11, 14]