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
@channingwalton
channingwalton / FirstSome.scala
Last active December 14, 2015 14:58
Iterate a list of functions A => Option[B] returning the first Some
object play {
import scalaz._
import Scalaz._
def foo(i: Int) = if (i > 0) Some(i) else None
def boo(i: Int) = if (i <= 0) Some(0) else None
val l = foo _ :: boo _ :: Nil
object MonadWriterExample extends App {
import scalaz._
import Scalaz._
implicit val monadWriter = EitherT.listenableMonadWriter[Writer, String, String]
case class Person(firstName: String, age: Int, car: Option[String])
def notEmpty(s: String) = Option(s).filter(x ⇒ !x.isEmpty && !x.forall(_.isWhitespace)) match {
case Some(r) ⇒ monadWriter.right[String](r)
@channingwalton
channingwalton / gist:4683045
Created January 31, 2013 13:59
Setting multiple values with lenses
def setL[T, A, B](la: Lens[T, A], lb: Lens[T, B])(a:A, b: B)(t: T): T = la.set(lb.set(t, b), a)
def setL[T, A, B, C](la: Lens[T, A], lb: Lens[T, B], lc: Lens[T, C])(a:A, b: B, c: C)(t: T): T = lc.set(setL(la, lb)(a,b)(t), c)
def setL[T, A, B, C, D](la: Lens[T, A], lb: Lens[T, B], lc: Lens[T, C], ld: Lens[T, D])(a:A, b: B, c: C, d: D)(t: T): T = ld.set(setL(la, lb, lc)(a,b, c)(t), d)
trait FooComponent {
def bar: BarComponent#Bar
case class Foo(foofy: String)
}
trait BarComponent {
def foo: FooComponent#Foo
@channingwalton
channingwalton / FunctionThisUp.scala
Created November 14, 2012 22:23
Shapeless FunctionThisUp
package stereotypes
import shapeless._
import Searchable._
object FunctionThisUp {
// pretend we have some case classes - they needn't be ours of course
case class Name(name: String)
case class Description(description: String)
@channingwalton
channingwalton / FizzBuzz.scala
Created October 4, 2012 20:03
FizzBuzz with Scalaz
// Inspired by http://dave.fayr.am/posts/2012-10-4-finding-fizzbuzz.html
object FizzBuzz extends App {
import scalaz._
import Scalaz._
def fizzbuzz(i: Int) = ((i % 3 == 0) option "fizz") |+| ((i % 5 == 0) option "buzz") | i.shows
for (i <- 1 to 100) {println(i + " " + fizzbuzz(i))}
}
@channingwalton
channingwalton / gist:3739531
Created September 17, 2012 20:18
Book recommendations
@channingwalton Recently read »I, Robot« from Asimov. The short stories are entertaining and very thoughtful.
@channingwalton I would like a wizard where I tell it I like Asimov (especially Daneel and Giskard) and Clarke, and it recommends others.
@channingwalton pretty much anything by Alastair Reynolds. House of Suns is particularly great.
@channingwalton I’ve enjoyed the Laundry novels by Charles Stross.
@channingwalton "Wool" series by Hugh Howey.
@channingwalton
channingwalton / gist:3240547
Created August 2, 2012 20:51
Kleisli problems
object KleisliValidation extends App {
import scalaz._
import Scalaz._
import scala.util.control.Exception._
implicit def vm = Validation.validationMonad[String]
type EValidation[+T] = Validation[String, T]
def toDouble(s: String): EValidation[Double] = allCatch.either(s.toDouble).fold(_.toString.fail, _.success)
@channingwalton
channingwalton / gist:3230464
Created August 1, 2012 20:31
Example of Kleisli composition of Validation
object KleisliValidation extends App {
import scalaz._
import Scalaz._
import scala.util.control.Exception._
type EValidation[+T] = Validation[String, T]
implicit val binding = new Bind[EValidation] {
def map[A, B](fa: EValidation[A])(f: A => B): EValidation[B] = fa.map(f)
def bind[A, B](fa: EValidation[A])(f: A => EValidation[B]): EValidation[B] = fa.flatMap(f)
}
@channingwalton
channingwalton / StateMonad.scala
Created May 31, 2012 21:28
State Monad and Validation with Scalaz
import scalaz._
import Scalaz._
/**
* Use the state monad to 'process a trade' and store the new trade.
* As well as processing the trade, handle validation errors.
*/
object StateMonad extends App {
case class Trade(info: String)