Skip to content

Instantly share code, notes, and snippets.

View vmarquez's full-sized avatar

Vincent Marquez vmarquez

  • Irvine, CA
View GitHub Profile
@vmarquez
vmarquez / gist:74353ca4c1ce264f02c0
Created November 3, 2014 01:28
Option to State
def OtoS[A](o: Option[A => A]): State[A,A] =
o match {
case Some(f) => State[A,A](a => {
val na = f(a)
(na,na)
})
case None => State[A,A](a => (a,a))
}
def sepSeq[A, B, F[_], G[_]](g: G[EitherT[F, A, B]])(implicit F: Monad[F], G: Foldable[G], M: MonadPlus[G]): EitherT[F, A, (G[A],G[B])] =
EitherT(G.foldRight(g, F.point((M.empty[A],M.empty[B])))( (a, l) =>
for {
tup <- l
e <- a.run
} yield
e.fold(le => (M.plus(M.point(le),tup._1), tup._2), re => (tup._1, M.plus(M.point(re), tup._2)))
).map(_.right[A])
)
@vmarquez
vmarquez / BlowUpScalac.scala
Created December 18, 2014 23:52
Scala compiler exception
val eitherK = EitherT[({ type l[a] = Kleisli[Option, String, a]})#l, String, Int]( Kleisli((s:String) => (1.right[String]).some ) )
class MyBlah[A, B, F[_], G[_]](value: G[EitherT[F, A, B]])
new MyBlah(List(eitherK))
@vmarquez
vmarquez / gist:1adda2c0c981a25d05e3
Created February 9, 2015 18:54
Scalaz dependencies
This file has been truncated, but you can view the full file.
<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet type='text/xsl' href='reportXMLtoHTML.xsl'?>
<classycle title='scalaz-core_2.11-7.1.0.jar' date='2015-02-09'>
<cycles>
<cycle name="scalaz.NaturalTransformation et al." size="2" longestWalk="0" girth="2" radius="1" diameter="1" bestFragmentSize="1">
<classes>
<classRef name="scalaz.NaturalTransformation" eccentricity="1" maximumFragmentSize="1"/>
<classRef name="scalaz.NaturalTransformations" eccentricity="1" maximumFragmentSize="1"/>
</classes>
<centerClasses>
object Test {
trait BlahConfig[A <: BlahConfig[A]] {
val server: Int
def updateCache(l: List[Int]): A
}
def doBlahStuff[A <: BlahConfig[A]](i: Int) = State[A, Int]( c => (c.updateCache(List(i)), c.server) )
trait FooConfig[A <: FooConfig[A]] {
def password: String
object Test {
import scalaz.{Lens,State}
case class BlahConfig(server: Int, l: List[Int])
def doStuff(i: Int) = State[BlahConfig,Int]( c => (c.copy(l = i :: c.l), c.server) )
case class FooConfig(val password: String)
def doSomeStuff(i: Int) = State[FooConfig, String](c =>(c, c.password))
@vmarquez
vmarquez / Coverage.scala
Last active August 29, 2015 14:18
Coverage.scala
case class Shift(start: DateTime, end: DateTime)
case class Coverage(time: DateTime, amount: Int)
def findCoverages(increment: Int, shifts: List[Shift]): List[Coverage] = {
shifts.map(s => generateDates(increment, s.start, s.end))
.flatten //Going from List[List[DateTime]] to List[DateTime]
.groupBy(dt => dt) //just grouping by date, groupBy always returns a Map[Key, List[Value]]
.map(kv => Coverage(kv._1, kv._2.size)) //when mapping over a Map/Dictionary, the parameter is a tuple, with _1 being the key and _2 being the value
.toList
package stackotalk
import scalaz._
import Scalaz._
import org.joda.time._
object Dates {
def generateDates(sd: DateTime, ed: DateTime): List[DateTime] = {
if (sd isBefore ed)
sd :: generateDates(sd.plusDays(1), ed)
object StateApply {
import scalaz._
implicit def applyState[F[_], A](implicit F: Applicative[F], S: Semigroup[A]): Applicative[({ type l[a] = StateT[F, A, a]})#l] = new Applicative[({ type l[a] = StateT[F, A, a]})#l] {
def point[B](b: => B): StateT[F, A, B] = StateT[F, A, B]((a:A) => F.point((a,b)))
def ap[B, C](fa: => StateT[F, A, B])(f: => StateT[F, A, B => C]): StateT[F, A, C] = {
StateT[F, A, C]((s: A) => {
val sa = fa.run(s)
val sb = f.run(s)
[core] λ test:console
[info] Starting scala interpreter...
[info]
Loading...
Welcome to the Ammonite Repl 0.4.7
(Scala 2.10.4 Java 1.8.0_60)
@ "hello"
java.lang.NullPointerException
com.sqality.scct.IO$class.relativePath(IO.scala:74)
com.sqality.scct.IO$.relativePath(IO.scala:6)