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 / 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
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))
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
@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>
@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))
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 / 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))
}
import scala.concurrent.Future
import scalaz._
import Scalaz._
import scala.concurrent.ExecutionContext.Implicits.global
val fa = OptionT( Future { 1.some } )//Returns an OptionT[Future,Int]
val fb = OptionT( Future { 2.some } )
//we don't need to nest for comprehensions since the OptionT unwraps the Option
// automatically in addition to whatever monad it's abstracting over
for {
@vmarquez
vmarquez / TrampolineIssue.scala
Last active August 29, 2015 14:04
Trampoline overflow
import scalaz._
import Scalaz._
import scalaz.Free._
//calling a.run(0) blows up obviously due to stack issues
val a = (0 to 10000).map(ii => State[Int,Int](i => (i,ii)) ).foldLeft( State[Int,Int](i => (i,0)) )( (s,a) => s.flatMap(i => a.map(ii => (ii+i) )))
//calling b.run(0) blows up, not stack safe.
val b = (0 to 10000).map(ii => StateT[Free.Trampoline,Int,Int](i => Trampoline.done((i,ii))) ).foldLeft( StateT[Free.Trampoline, Int,Int](i => Trampoline.done((i,0))) )( (s,a) => s.flatMap(i => a.map(ii => (ii+i) )))
@vmarquez
vmarquez / Writer.cs
Created December 1, 2013 20:48
Writer Monad in C#
using System;
using System.Collections.Generic;
using System.Linq;
namespace XSharpx {
public struct Writer<A, B>
{
public readonly A a;
public readonly B b;
public readonly Semigroup<A> semigroup;