Skip to content

Instantly share code, notes, and snippets.

View arschles's full-sized avatar
🎯
Focusing

Aaron Schlesinger arschles

🎯
Focusing
View GitHub Profile
@arschles
arschles / wrapAndTransform.scala
Created October 18, 2012 22:34
wrapping a container purely in a FlowState, and transforming it in a ResT
/**
* wrap a container purely (ie: with no effects) in a FlowState and transform it in a ResT
* @param container the container to wrap
* @param transformer the function to transform the container into a Res[U]
* @tparam M the container
* @tparam T the type inside the container
* @tparam U the type inside the resulting Res
* @return the Res[U] returned by the transformer wrapped purely in a FlowState and transformed by a ResT
*/
private def wrapPure[M[_], T, U](container: M[T])(transformer: M[T] => Res[U]): ResT[FlowState, U] = {
@arschles
arschles / reqRespStateCreators.scala
Created October 18, 2012 23:07
creating ReqRespState[Res[T]] values
/**
* transform a type purely (no effects) into a ReqRespState[T].
* note that a ReqRespState[T] is the same as a scalaz.State[ReqRespData, T]
* @param t the type to transform
* @tparam T the type
* @return the resulting ReqRespState[T]
*/
protected def reqRespStatePure[T](t: T): ReqRespState[T] = {
t.pure[ReqRespState]
}
@arschles
arschles / stateMap.scala
Created November 13, 2012 02:09
Map Over a State[ReqRespData, Res[A]]
import scalaz._
import Scalaz._
import Probably.A.Lot.More.Shit
//this is how you construct a State manually by the way
val myState: State[ReqRespData, Res[String]] = state { d: ReqRespData =>
val myRes: Res[String] = ValueRes("hello world")
(d, myRes)
}
@arschles
arschles / mapRes.scala
Created November 13, 2012 02:10
build a mapRes for any State[ReqRespData, Res[T]]
//build an extension to any State[ReqRespData, Res[Something]]
trait StateResW[T] {
protected def state: State[ReqRespData, Res[T]]
def mapRes[U](f: T => U): State[ReqRespData, Res[U]] = state.map { res: Res[T] =>
res match {
case ValueRes(t) => ValueRes(f(t))
case other => other: Res[U]
}
}
}
@arschles
arschles / resTExample.scala
Created November 13, 2012 02:11
example scalamachine usage of ResT
import scalaz._
import Scalaz._
import scalamachine.core._
import scalamachine.scalaz._
val myState: State[ReqRespData, Res[String]] = state { d: ReqRespData =>
val myRes: Res[String] = ValueRes("hello world")
(d, myRes)
}
@arschles
arschles / newtype.scala
Created November 17, 2012 22:31
some of NewType
object TaggedTypes {
sealed trait TaggedType[T] {
def underlying: T
override def toString = underlying.toString
}
//implicitly extract the primitive value from a TaggedType[T]
implicit def underlying[T](w: TaggedType[T]): T = {
w.underlying
}
@arschles
arschles / lift-json-easier.scala
Created November 18, 2012 06:25
making lift-json easier
import scalaz._
import Scalaz._
import net.liftweb.json._
import net.liftweb.json.scalaz.JsonScalaz._
def json[T](serialize: T => JValue, deserialize: JValue => Result[T]): JSON[T] = new JSON[T] {
def write(t: T) = serialize(t)
def read(jval: JValue): Result[T] = deserialize(jval)
}
@arschles
arschles / liftscalaz.scala
Created December 7, 2012 02:16
Scalaz Lift Compatability
package liftscalaz {
import scalaz._
type ScalazFailure = Failure
def ScalazFailure[E, S](e: E): ScalazValidation[E, S] = Failure[E, S](e)
type ScalazSuccess = Success
def ScalazSuccess[E, S](s: S): ScalazValidation[E, S] = Success[E, S](s)
type ScalazValidation = Validation
import scalaz._
import Scalaz._
//here we say that the default value for List[String] is "hello" :: "world" :: Nil
implicit val listZero = new Zero[List[String]] = {
override lazy val zero = List("hello", "world")
}
//scalaz uses Zero in its unary ~ operator on Option
val mbListString = none[List[String]]
trait PolyFunc[Out] {
def apply[In]: In => Out
}
sealed trait HList {
}
//the end of an HList
sealed class HNil extends HList {