Skip to content

Instantly share code, notes, and snippets.

@purefn
purefn / io.scala
Created March 18, 2011 00:01
A start on a package for doing monadic IO in scala
package object io {
sealed trait IO[A] {
def unsafePerformIO: A
}
object IO {
def apply[A](a: => A): IO[A] = new IO[A] {
def unsafePerformIO = a
}
}
import scalaz._
import Scalaz._
import effects._
trait Request
trait Response
case class WebState(_request: Request, _response: Response)
sealed trait Web[A] extends NewType[Web.T[A]]
@purefn
purefn / Cat.scala
Created May 8, 2011 17:16
cat in scala with Iteratees from scalaz-nio
package test
import scalaz.{Failure => _, _}
import Scalaz._
import effects._
import iteratees._
import java.io._
object ScalazIter {
@purefn
purefn / Head.scala
Created May 20, 2011 20:31
print the first 10 chunks of input
import scalaz.{Failure => _, _}
import Scalaz._
import effects._
import iteratees._
import java.io._
object Head {
def main(args: Array[String]) {
val enum = enumStream[Seq[Byte], IO]((1 to 50).toStream.map(i => ("line " + i + "\n").getBytes.toSeq))
@purefn
purefn / MVar.scala
Created July 13, 2011 05:07
Start of port of Haskell MVar to Scala
package scalaz
package concurrent
import Scalaz._
import Promise._
import Strategy._
import java.util.concurrent._
import locks._
import atomic._
@purefn
purefn / gist:1105553
Created July 25, 2011 23:41
Introduction to Functional Programming in Scala - abstract
Scala is billed as a multi-paradigm language, supporting object oriented programming and functional programming.
What does it mean to say that Scala is a functional programming language? What is functional programming?
What problems does it solve? What problems does it introduce and how can we solve them? In this presentation
we'll cover these questions and walk through an example of taking a traditional, imperative piece of code and
converting it into an example of traditional functional code. We'll iterate on the functional code, introducing
language features of Scala that will make our solution more generic, concise and provide better separation of concerns.
Finally, we'll talk about the one problem that traditionally confuses more functional programming newcomers, how to
do IO when you aren't allowed to do side-effects.
@purefn
purefn / gist:1115012
Created July 30, 2011 00:14
Introduction to Scala - outline
* What is Scala?
** OO
*** example class
** Functional (sorta)
*** example map and foldLeft
* Some basics
** Defining values/variables
*** examples with/without type inference
** Defining functions
*** examples with/without return value
@purefn
purefn / gist:1340475
Created November 4, 2011 20:59
IO except handling
scala> import scalaz._, Scalaz._, effects._
import scalaz._
import Scalaz._
import effects._
scala> val hello = putStrLn("Hello")
hello: scalaz.effects.IO[Unit] = scalaz.effects.IO$$anon$2@401c1b5f
scala> val bad:IO[Unit] = throwIO(new Exception("bad thing"))
bad: scalaz.effects.IO[Unit] = scalaz.effects.IO$$anon$2@1d296aa3
trait Pointed[F[_]] {
def point[A](a: => A): F[A]
}
trait Functor[F[_]] {
def fmap[A, B](fa: F[A])(f: A => B): F[B]
}
trait Show[A] {
def shows(a: => A): String
@purefn
purefn / gist:2284779
Created April 2, 2012 16:27
Encoding Product and Coproduct in Scala
type Product[F[_], G[_], A] = (F[A], G[A])
trait Prod[F[_], G[_]] {
type and[H[_]] = Prod[F, ({type λ[α] = Prod[G, H, α]})#λ]
type apply[A] = (F[A], G[A])
}
type product[F[_], G[_]] = Prod[F, G]
type Coproduct[F[_], G[_], A] = Either[F[A], G[A]]