Skip to content

Instantly share code, notes, and snippets.

View channingwalton's full-sized avatar
🏠
Working from home

Channing Walton channingwalton

🏠
Working from home
View GitHub Profile
@krasserm
krasserm / MonadTransformerExamples.scala
Created July 14, 2011 10:32
Scalaz 7 monad transformer examples
import scalaz._
import Scalaz._
object MonadTransformerExamples {
def main(args: Array[String]) = run
def run {
// ------------------------------------------------------
// Combined Option/Option
// ------------------------------------------------------
@larsrh
larsrh / gist:3240574
Created August 2, 2012 20:54 — forked from channingwalton/gist:3230464
Example of Kleisli composition of Either
object KleisliValidation extends App {
import scalaz._
import Scalaz._
import scala.util.control.Exception._
type EEither[+T] = Either[String, T]
def toDouble(s: String): EEither[Double] = allCatch.either(s.toDouble).fold(_.toString.left, _.right)
def sqrt(d: Double): EEither[Double] = if (d >= 0) math.sqrt(d).right else "sqrt(%s) is too complex for me".format(d).left
@puffnfresh
puffnfresh / WriterTExample.scala
Created August 14, 2012 01:57
Example of scalaz' WriterT for Atlassian (pure functional logging)
import scalaz.WriterT
import scalaz.NonEmptyList
import scalaz.syntax.id._
import scalaz.std.option._
import scalaz.syntax.std.option._
type OptionLogger[A] = WriterT[Option, NonEmptyList[String], A]
val two: OptionLogger[Int] = WriterT.put(2.some)("The number two".wrapNel)
@etorreborre
etorreborre / gist:3870064
Created October 11, 2012 03:54
Unboxed union types with a context bound
/**
* this is an experiment to create unboxed union types with a phantom type and a context bound.
* All the good ideas come from @milessabin post, comments and links: http://www.chuusai.com/2011/06/09/scala-union-types-curry-howard/#comment-22
*/
/** trait for anything that can be A or B */
trait Or[A, B] {
// a phantom type, there will be no instance of this type that we'll use
type l[T]
// an alias for l[t]
@travisbrown
travisbrown / needle-in-haystack.scala
Created November 13, 2012 22:57
Digging through arbitrarily nested case classes, tuples, and lists
/**
* Digging through arbitrarily nested case classes, tuples, and lists
* by Travis Brown
*
* In response to this question by Channing Walton on the Shapeless dev list:
*
* https://groups.google.com/d/msg/shapeless-dev/hn7_U21tupI/Zm9h3uNb51gJ
*
* Tested with Scala 2.9.2 and Shapeless 1.2.3. Should work on 1.2.2 with minor edits.
*/
@etorreborre
etorreborre / gist:4187596
Created December 2, 2012 07:27
Monoids for the option of a function
import scalaz._
import Scalaz._
trait A {
def increment: A
}
case class A1(i: Int) extends A {
def increment = A1(i+20)
}
@chrislewis
chrislewis / gist:4409778
Created December 29, 2012 22:45
A Reader Either monad transformer for java.util.Properties with Scalaz7
/*
* This is a rendition of https://gist.github.com/3416494. Scalaz7 provides the
* Reader and we take it a step further by constructing and using a Reader Either
* monad transformer with pure error handling, instead of the original impure Reader
* monad that would throw exceptions on parse errors.
*/
import scalaz._
import Scalaz._
import java.util.Properties
@stew
stew / kleisliexample.scala
Created January 16, 2013 14:07
example of kleisli in kleisli
import scalaz._
import Scalaz._
case class Person(name: String, score: Int)
object Main extends App {
type Environment = Map[String, Person]
type EnvReader[+A] = Kleisli[Option,Environment,A]
type UserReader[+A] = Kleisli[EnvReader,String,A]
@przemek-pokrywka
przemek-pokrywka / ReaderMonadDIWhenFunsDependOnVariousThings.scala
Created January 21, 2013 22:46
Reader Monad dependency injection when each of injected functions depends on something other. The goal is to assess, how good does Reader Monad fit into this context.
case class Reader[Conf, Res](obtainResult: Conf => Res) {
def map[NewRes](transform: Res => NewRes) =
Reader {
(config: Conf) =>
transform(this obtainResult config)
}
def flatMap[NewRes](createReader: Res => Reader[Conf, NewRes]) =
Reader {
@mattdenner
mattdenner / gist:4977805
Created February 18, 2013 14:28
Me attempting to get my head around monads.

My biggest problem with monads is that my brain can't grasp them, obviously! One of the issues I've got is that people teach them as part of a somewhat bigger context: they start showing how Option[T] works and then suddenly declare ''Option[T] is a monad''! Or worse, they talk about something completely unrelated in an attempt to give you some real world perspective onto them. It's not working for me and it might not be working for you; what I'll write here is to help me understand monads, and it probably won't work for you, but it might.

I'm going to start with an extremely simple case class and build it up from there:

final case class Holder[+T](value: T)

It's really simple in that it holds a value of a type T. One thing to realise is that case class in Scala automatically gives you a companion object for Holder[T] called Holder which gives you an apply method for creating instances. I'm going to make a similar object, which I'll abitrarily call HolderMonad, that will have