Skip to content

Instantly share code, notes, and snippets.

View seanparsons's full-sized avatar
💭
Setting A Status Message

Sean Parsons seanparsons

💭
Setting A Status Message
View GitHub Profile
[error] (myproject/compile:compile) scala.reflect.internal.FatalError: class Universe does not have a member Quasiquote
import org.apache.commons.codec.binary.Base64
import java.nio.file._
import java.nio.file.attribute._
import scala.collection.mutable.ListBuffer
val htmlTemplate = """
<html>
<head>
<title>Encoded Images!</title>
</head>
@seanparsons
seanparsons / KleisliEnhance.scala
Created July 31, 2013 16:35
Automatic tuple conversion powered by macros.
package myproject.util
import myproject.macros._
import scalaz._,Scalaz._,Kleisli._
object KleisliEnhance {
implicit class KleisliEnhancer[M[+_]: Monad, A, B](kleisli: Kleisli[M, A, B]) {
def localFrom[AA]: Kleisli[M, AA, B] = kleisli.local(TupleTransformer.tupleToFrom[A, AA]
}
}
def doThing[M[+_]: Monad, T, U, V]
(actionOne: () => ReaderT[M, T, Unit],
actionTwo: () => ReaderT[M, T, Unit],
actionThree: () => ReaderT[M, T, Unit],
actionFour: () => ReaderT[M, (T, U), Unit],
actionFive: () => ReaderT[M, T, Unit,
actionSix: () => ReaderT[M, V, Unit])
(): ReaderT[M, (T, U, V), Unit] = {
val tFromTriple: ((T, U, V)) => T = _._1
val tuFromTriple: ((T, U, V)) => (T, U) = tup => (tup._1, tup._2)
@seanparsons
seanparsons / gist:6010066
Created July 16, 2013 16:01
Idea for logging entry into methods.
def logEntry() = macro logEntryImpl
def method(param1: String, param2: Int): String = {
logEntry()
param1 * param2
}
// I'd want
// logEntry()
// to be replaced with
@seanparsons
seanparsons / gist:5941234
Created July 6, 2013 20:48
Possible route to make for comprehensions stricter and powered by scalaz.Monad.
def strictFor[M[+_], T](forExp: M[T])(implicit mMonad: Monad[M]): M[T] = macro strictForImpl[M, T]
def strictForImpl[M[+_]: c.WeakTypeTag, T: c.WeakTypeTag](c: Context)(forExp: c.Expr[M[T]])(mMonad: c.Expr[Monad[M]]): c.Expr[M[T]] = {
@seanparsons
seanparsons / LoopComparison.scala
Created April 12, 2013 22:09
Loops? No Thanks!
// This bit is mostly getting imports together and some simple types.
import scala.annotation.tailrec
import scala.collection.mutable.ListBuffer
import scalaz._,Scalaz._
case class User(id: Int, name: String, score: Long)
// EXAMPLE: Turn collection into another collection.
// With a for loop (in this case a foreach):
@seanparsons
seanparsons / gist:5361916
Last active December 16, 2015 02:19
Experimenting with sequenceU
scala> import scalaz._,Scalaz._,effect._
import scalaz._
import Scalaz._
import effect._
scala> val list = List(IO(1).liftM[OptionT])
list: List[scalaz.OptionT[scalaz.effect.IO,Int]] = List(OptionT(scalaz.effect.IOFunctions$$anon$5@52f3b59))
scala> list.sequenceU
res4: G.M[List[G.A]] = OptionT(scalaz.effect.IOFunctions$$anon$5@3beab0fc)
@seanparsons
seanparsons / gist:5229917
Last active December 15, 2015 08:19
Quick attempt at timeout support for Scalaz Futures.
import scalaz._
import Scalaz._
import scalaz.concurrent._
import scala.annotation.tailrec
import java.util.concurrent.locks.ReentrantReadWriteLock
trait Timeout
object Timeout extends Timeout
case class Timer(timeoutTickMs: Int = 100, workerName: String = "TimeoutContextWorker") {
@seanparsons
seanparsons / gist:5150889
Last active December 14, 2015 21:28
Experimenting with Scalaz Futures.
scala> import scalaz._,Scalaz._,scalaz.concurrent._
import scalaz._
import Scalaz._
import scalaz.concurrent._
scala> (1 to 10).map(n => Future.fork(Future.delay{Thread.sleep(1000); n})).toList.sequence.run // Takes 10 seconds.
res0: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
scala> Nondeterminism[Future].gather((1 to 10).map(n => Future.fork(Future.delay{Thread.sleep(1000); n})).toList).run // Takes 2 seconds.
res1: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)