Skip to content

Instantly share code, notes, and snippets.

View pedrofurla's full-sized avatar

Pedro Furlanetto pedrofurla

View GitHub Profile
trait Bar { def bar(msg:String):Unit }
trait Qux extends Bar {
abstract override def bar(msg:String="Qux Says Hello") = {
super.bar(msg)
println("qux: " + msg)
}
}
class Foo extends Bar {
@pedrofurla
pedrofurla / snippet.scala
Created May 12, 2012 03:51
Side-effect free handling of exceptions.The three first expressions can throw something
def exp[T](t: => T) =
(scala.util.control.Exception.allCatch either t).right
def readJson(dataField:Option[String],json:JsValue):Either[Throwable,JsValue] = {
for (
contents <- exp( json.asJsObject.getFields("rsp")(0) );
stat <- exp( contents.asJsObject.getFields("stat")(0).convertTo[String] );
jsonObject <- exp (
dataField match {
case Some(field) => contents.asJsObject.getFields(field)(0)
@pedrofurla
pedrofurla / gist:2698782
Created May 15, 2012 03:03
Multibot scala part. Possibly missing imports
// https://github.com/lopex/multibot/blob/master/src/main/scala/org/multibot/Multibot.scala
// line 87
val scalaInt = scala.collection.mutable.Map[String, IMain]()
def scalaInterpreter(channel: String)(f: (IMain, ByteArrayOutputStream) => Unit) = this.synchronized {
val si = scalaInt.getOrElseUpdate(channel, {
val settings = new scala.tools.nsc.Settings(null)
settings.usejavacp.value = true
settings.deprecation.value = true
val si = new IMain(settings) { override def parentClassLoader = Thread.currentThread.getContextClassLoader }
@pedrofurla
pedrofurla / gist:2766557
Created May 22, 2012 04:26
point is to 0 as map is to 1 (in the context of scalaz, by dabblego)
dabblego
point is to 0 as map is to 1
point: A => F[A]
map: (A => B) => F[A] => F[B]
lift2: (A => B => C) => F[A] => F[B] => F[C]
point=lift0
01:23
map=lift1
@pedrofurla
pedrofurla / gist:3754885
Created September 20, 2012 09:24
Validation#flatmap is crazy
val a = fromEither(listing).toValidationNEL
val retr = { x:VideoListing => fromEither(lister.retrieveReport(x)).toValidationNEL }
val res0 = for (
l <- a;
rep <- retr(l)
) yield { rep }
@pedrofurla
pedrofurla / gist:4059099
Created November 12, 2012 12:31
Mapping oneToMany relationship
case class Music(id:ID, title:String, isrc:String)
case class Compilation(id:ID=None, title:String, musics: List[Music], author:User /* TODO Either[DJ,Programmer]? */)
case class User(id:ID=None, name:String, email:String, password:String)
object Musics extends Table[Music]("music") {
def id = this.autoIncPk("id")
def title = this.string("title")
def isrc = this.string("isrc")
@pedrofurla
pedrofurla / Function1.scala
Created November 13, 2012 04:05 — forked from tonymorris/Function1.scala
scala Function1
object Function1Stuff {
def flatMap[T, A, B]: Function1[T, A] => (A => Function1[T, B]) => Function1[T, B] =
sys.error("todo")
}
val query = Query(Users) drop 10 take 10
query.selectStatement
//> query : scala.slick.lifted.Query[querying.samples.model.Users.type,queryin
//| g.samples.model.User] = scala.slick.lifted.WrappingQuery@10f102d3
//> res0: String = select x2.x3, x2.x4, x2.x5, x2.x6, x2.x7 from (select x8."id
//| " as x3, x8."name" as x4, x8."email" as x5, x8."password" as x6, x8."roleId
//| " as x7 from "user" x8 limit 10 offset 10) x2
@pedrofurla
pedrofurla / gist:4225153
Created December 6, 2012 15:11
Alternative to Table.createFinder working!
def equalBy[B: BaseTypeMapper]
(proj:Entities.type => Column[B])(str:B):Query[Entities.type,Entity] = {
Query(Entities) where { x => proj(x) === str}
}
val q = Entities.equalBy({ _.name })("5")
//> q : scala.slick.lifted.Query[test.whereCreator.Entities.type,test.whereCrea
//| tor.Entity] = scala.slick.lifted.WrappingQuery@6401d98a
q.selectStatement
@pedrofurla
pedrofurla / gist:4464873
Created January 6, 2013 02:40
Implicit function arguments value
def index = nonCustomerAction { implicit u => implicit r =>
Logger.info("into index")
Ok(html.subscriptions(db withSession { Subscriptions.all.list }))
}