Skip to content

Instantly share code, notes, and snippets.

@stew
stew / adjunction.scala
Created November 22, 2016 19:18
don't know if I can make line 89 work
package adjunction
import cats._
/**
* An Adjunction is a relationship between two functors:
* - `F`, the "left-adjoint"
* - `G`, the "right-adjoint"
*
@stew
stew / adjunction.scala
Last active March 15, 2016 15:22
Play with adjunctions, specifically with composing Writer -| Reader
adjunction
import cats._
abstract class Adjunction[F[_], G[_]] { self =>
def left[A,B](a: A)(f: F[A] => B): G[B]
def right[A,B](fa: F[A])(f: A => G[B]): B
def unit[A](a: A): G[F[A]] =
package dogs
import Predef._
import dogs.Order.{GT, EQ, LT, Ordering}
import dogs.std.intOrder
import scala.annotation.tailrec
import scala.math
sealed abstract class BinaryTree[A] {
import BinaryTree._
@stew
stew / gist:245037c48848f80eb5ca
Created January 21, 2016 23:45
runTraverseRWST.scala
def runTraverseRWST[F[_], G[_], R, W: Monoid, S, A, B](fa: F[A])(f: A => RWST[G,R,W,S,B])(r: R, s: S)(implicit F: Traverse[F], G: Monad[G]): G[(W,F[B],S)] = {
F.traverse[({type λ[α]=RWST[G,R,W,S,α]})#λ,A,B](fa)(f)(RWST.rwstMonad[G,R,W,S]).run(r,s)
}
class Elipse(val width: Double, val height: Double)
// circle is a special case of elispse where width and height are the same
class Circle(r: Double) extends Elipse(r, r)
// typeclass to calculate the Area of a T
trait Area[-T] {
def area(t: T): Double
}
@stew
stew / Comp.scala
Last active August 29, 2015 14:27
this flatmap is probably not stacksafe
package cats
package effect
import java.util.concurrent.CountDownLatch
/**
* A computation of an A value
*/
sealed trait Comp[A] {
import Comp._
@stew
stew / gist:e979eaa13bca2d5e8d61
Created April 15, 2015 15:30
fix the zine for double sided printing
convert -quality 100 -density 300x300 strace-zine.pdf'[0,2,4,6]' 'strace-single%d.gif
convert -quality 100 -density 300x300 strace-zine.pdf'[1,3,5,7]' -rotate 180 'strace-single%d.gif'
convert strace-single<0-8>.gif strace-fixed.pdf #<0-8> is probably a zsh only thing, * would probably work ⏎
@stew
stew / either.py
Last active February 9, 2016 08:50
from abc import ABCMeta, abstractmethod
import sys
# lets get one thing straight right out of the box:
def fail(e):
import traceback
with open("/tmp/tb", "w") as f:
traceback.print_exc(e, f)
import scalaz._
import Maybe._
case class CofreeNEL[A](cf: Cofree[Maybe, A]) {
def +:(a: A): CofreeNEL[A] = CofreeNEL(Cofree(a, just(cf)))
def head: A = cf.head
def tail: Maybe[CofreeNEL[A]] = cf.tail.map(CofreeNEL.apply)
def map[B](f: A=>B): CofreeNEL[B] = CofreeNEL(cf map f)
def flatMap[B](f: A => CofreeNEL[B]): CofreeNEL[B] = CofreeNEL(Bind[({type l[A]=Cofree[Maybe, A]})#l].bind(cf)(f andThen (_.cf)))
import scalaz._
import Scalaz._
object IsItAProfunctor extends App {
case class Foo(f: Double)
val validateDouble: Double ⇒ Validation[String,Double] = {d ⇒
if(d < 0) "less than zero".fail
else d.success