Skip to content

Instantly share code, notes, and snippets.

import sbt._
import Keys._
object SinksBuild extends Build {
// Custom keys -- things don't work well if they are on .sbt files
lazy val scalazVersion = settingKey[String]("Scalaz Version")
lazy val scodecVersion = settingKey[String]("Scodec Version")
lazy val unfilteredVersion = settingKey[String]("Unfiltered Version")
lazy val scalacheckVersion = settingKey[String]("Scalacheck Version")
lazy val specs2Version = settingKey[String]("Specs2 Version")
import scalaz._
import Scalaz.{merge => _, _}
import scalaz.concurrent._
import scalaz.stream._
import scala.concurrent.duration._
implicit val defaultScheduler = scalaz.stream.DefaultScheduler
val flag = async.signal[Boolean]
val reset = Process.eval(flag set true).drain
### Keybase proof
I hereby claim:
* I am dcsobral on github.
* I am dcsobral (https://keybase.io/dcsobral) on keybase.
* I have a public key whose fingerprint is C049 F080 3A32 FC38 4487 9042 5F35 D49A D3BE CD47
To claim this, I am signing this object:
@dcsobral
dcsobral / gist:7890914
Created December 10, 2013 13:55
JSoup demo
scala> Jsoup.parse("<B>bold, <I>bould italic, </b>italic, </i>normal text")
res0: org.jsoup.nodes.Document =
<html>
<head></head>
<body>
<b>bold, <i>bould italic, </i></b>
<i>italic, </i>normal text
</body>
</html>
@dcsobral
dcsobral / Getter.scala
Last active December 30, 2015 21:49
Anternative findLinks for coursera Reactive
def findLinks(body: String): Iterator[String] = {
val document = Jsoup.parse(body)
val links = document.select("a[href]")
for {
link <- links.iterator().asScala
} yield link.attr("href")
}
@dcsobral
dcsobral / REPLinator.scala
Last active December 22, 2015 20:39
Macro that replaces an expression with a REPL-like (minus REPL's "toString") evaluation of the expression.
/**
* Replaces an expression with a REPL-like evaluation of it. For example,
* the expression `2 + 2` would be replaced with this:
*
* {{{
* scala> 2 + 2
* Int: 4
* }}}
*
* Clients of REPLinator *must* be compiled with -Yrangepos, until and unless
@dcsobral
dcsobral / Printer.scala
Created September 10, 2013 19:15
Prints a string as if it was produced by REPL. However, it has none of the magic the REPL has for printing types like arrays, or using quotes for empty strings on collections, etc.
object Printer {
import scala.reflect.runtime.currentMirror
import scala.tools.reflect.ToolBox
val toolBox = currentMirror.mkToolBox()
def print(s: String) = {
val tree = toolBox parse s
val typedTree = toolBox typeCheck tree
val tpe = typedTree.tpe.widen
val result = toolBox eval tree
// At the external API level, use TypeTag
def extract[T: TypeTag](jsv: JsValue): T =
extractImpl[T](jsv, typeOf[T])
// But use Type for everything else
def extractImpl[T](jsv: JsValue, tpe: Type): T = {
// no need for m
val ex = jsv match {
case JsNumber(n) => n
case JsString(s) => s
scala> type T = List[String]
defined type alias T
scala> val tpe = implicitly[TypeTag[T]].tpe.normalize
tpe: reflect.runtime.universe.Type = List[String]
scala> val tpeSymbol = tpe.typeSymbol.asType
tpeSymbol: reflect.runtime.universe.TypeSymbol = class List
scala> val args = tpeSymbol.typeParams.map(_.asType.toTypeIn(tpe))
package com.youdevise.lofty
import org.specs2.mutable._
import scala.language.dynamics
trait State[S, A] {
val runState: Function[S, (S, A)]
def flatMap[B](f: A => Function[S, (S, B)]):Function[S, (S, B)] = (state:S) => {
val (newState, value) = runState(state)
f(value)(newState)