Skip to content

Instantly share code, notes, and snippets.

package com.knollop.wharton.store.mongo
import play.api.libs.json._
import com.mongodb._
import com.knollop.wharton.store._
import org.bson.types.ObjectId
trait MCollection extends DBObjectTransformer {
def mongoDb: DB
val collectionName: String
val filename = "conf/my_file.txt"
val lines = scala.io.Source.fromFile(filename).getLines.toList
var linePtr = 0
def sendLine {
if(linePtr == lines.length) {
clock.cancel()
}
else {
testStream ! Message(lines(linePtr))
@lazyvalue
lazyvalue / gist:af7cd9562937e75c2eaf
Created September 21, 2014 06:59
Monad transformers are pretty good at type inference
import Control.Monad.Identity
import Control.Monad.Error
type Scott = ErrorT String Identity Int
myf :: Int -> Scott
myf i = return i
lar :: Scott
lar = do
import Data.List
mylst = [(1,3), (2,4), (5,6), (5,7), (6,8)]
findOverlaps :: [(Int,Int)] -> [(Int,Int)]
findOverlaps xs =
let sorted = sortBy (\x y -> if ((fst x) < (fst y)) then LT else GT) xs
foldF acum elem =
let (doneLst, (blockMin,blockMax)) = acum
(elemMin, elemMax) = elem
@lazyvalue
lazyvalue / gist:26135790dea751883a87
Created January 24, 2015 22:24
scala ctags config
--langdef=scala
--langmap=scala:.scala
--regex-scala=/^[ \t]*((abstract|final|sealed|implicit|lazy)[ \t]*)*(private|protected)?[ \t]*class[ \t]+([a-zA-Z0-9_]+)/\4/c,classes/
--regex-scala=/^[ \t]*((abstract|final|sealed|implicit|lazy)[ \t]*)*(private|protected)?[ \t]*object[ \t]+([a-zA-Z0-9_]+)/\4/c,objects/
--regex-scala=/^[ \t]*((abstract|final|sealed|implicit|lazy)[ \t]*)*(private|protected)?[ \t]*case class[ \t]+([a-zA-Z0-9_]+)/\4/c,case classes/
--regex-scala=/^[ \t]*((abstract|final|sealed|implicit|lazy)[ \t]*)*(private|protected)?[ \t]*case object[ \t]+([a-zA-Z0-9_]+)/\4/c,case objects/
--regex-scala=/^[ \t]*((abstract|final|sealed|implicit|lazy)[ \t]*)*(private|protected)?[ \t]*trait[ \t]+([a-zA-Z0-9_]+)/\4/t,traits/
--regex-scala=/^[ \t]*type[ \t]+([a-zA-Z0-9_]+)/\1/T,types/
--regex-scala=/^[ \t]*((abstract|final|sealed|implicit|lazy)[ \t]*)*def[ \t]+([a-zA-Z0-9_]+)/\3/m,methods/
--regex-scala=/^[ \t]*((abstract|final|sealed|implicit|lazy)[ \t]*)*val[ \t]+([a-zA-Z0-9_]+)/\3/l,constants/
@lazyvalue
lazyvalue / gist:fce34d1962d00074235c
Last active August 29, 2015 14:14
subtyping will each you alive
// I ran into this gotcha while working with scalaz.EitherT.
// I was trying to do a flatMap that changed the left of an EitherT from one type to another.
// In the below type definition the left is the 'A' type.
// EitherT[F[_], A, B]
// flatMap[C](f: B => EitherT[F, A, C])(implicit F: Monad[F]): EitherT[F, A, C]
import scalaz._
import scala.concurrent.Future
@lazyvalue
lazyvalue / gist:09a72f1ac5461578a2ea
Created February 18, 2015 07:41
Nasty little jdbc wrapper
import java.sql._
import scala.util.Try
object DbEnv {
trait Closeable {
def close: Unit
}
@lazyvalue
lazyvalue / gist:95e0b905cb5a2122aa20
Last active August 29, 2015 14:15
scala: using adts and dealing with invariance in parameterized types
// EitherT moved from being covariant in scalaz 7.0 to being invariant in scalaz 7.1
// I heavily used sealed traits for ADTs. This solves the problem, I just need to write a whole lot more bullshit.
sealed trait Person {
val name: String
val job: String
}
trait Plumber extends Person
object Plumber {
import scala.util.{Try, Success, Failure}
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits._
object FutureTests {
/*
* Success callback example.
*/
def test1 {
@lazyvalue
lazyvalue / gist:8831270f8b3377e59f49
Created March 11, 2015 20:22
Module examples worksheet
// Case classes
// Algebraic Data Types
// Objects
// Currying
// Traits
// Classes
// The Cake
// CASE CLASSES