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 / MonoidHomomorphism.scala
Created March 7, 2014 17:52
Turning nulls into zeroes, zeroes into empties
object MonoidHomomorphism extends App {
import scalaz._
import Scalaz._
/**
* Turn any null into the Zero value for its type
*/
def nullAsZero[T: Monoid : Equal](a: T) = if (a == null) implicitly[Monoid[T]].zero else a
@channingwalton
channingwalton / shapelessy.scala
Last active August 29, 2015 14:00
Replacing boilerplate with shapeless
case class Foo[T](x: T) {
def map[B](f: T => B) = Foo(f(x))
}
object OldWay {
def combineLatest[T1, T2](e1: Foo[T1], e2: Foo[T2]): Foo[(T1, T2)] = Foo((e1.x, e2.x))
def combineLatest[T1, T2, T3](e1: Foo[T1], e2: Foo[T2], e3: Foo[T3]): Foo[(T1, T2, T3)] =
combineLatest(combineLatest(e1, e2), e3) map {
@channingwalton
channingwalton / LensFu.scala
Created May 19, 2014 09:48
Path-aware Lens
import scalaz._
object LensFu extends App {
case class FieldValue[T](name: String, v: T)
case class Field[T, F](name: String, lens: Lens[T, F]) {
def >=>[G](field: Field[F, G]): Field[T, G] = Field(name + "." + field.name, lens >=> field.lens)
def get(v: T): FieldValue[F] = FieldValue(name, lens.get(v))
def set(a: T, b: F): T = lens.set(a, b)
@channingwalton
channingwalton / Reverse.idr
Created September 29, 2014 21:15
Reverse an Idris Vect
module Main
rev : Vect n a -> Vect n a
rev Nil = Nil
rev (x :: xs) = (rev xs) ++ (x :: Nil)
main : IO ()
main = putStrLn $ show $ rev [1,2,3]
fails to compile with
21:15:22.745 ERROR akka://ENSIME/user/$b o.e.s.SocketHandler - Error in socket reader:
java.lang.NumberFormatException: For input string: "new Wo"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:1.8.0_31]
at java.lang.Integer.parseInt(Integer.java:580) ~[na:1.8.0_31]
at java.lang.Integer.valueOf(Integer.java:740) ~[na:1.8.0_31]
at org.ensime.server.FramedStringProtocol$class.readString(FramedStringProtocol.scala:39) ~[server_2.11-0.9.10-SNAPSHOT.jar:0.9.10-SNAPSHOT]
at org.ensime.server.SwankProtocol.readString(SwankProtocol.scala:10) ~[server_2.11-0.9.10-SNAPSHOT.jar:0.9.10-SNAPSHOT]
at org.ensime.server.S≈wankProtocol.read(SwankProtocol.scala:15) ~[server_2.11-0.9.10-SNAPSHOT.jar:0.9.10-SNAPSHOT]
at org.ensime.server.SocketHandler$$anon$1.run(SocketHandler.scala:43) ~[server_2.11-0.9.10-SNAPSHOT.jar:0.9.10-SNAPSHOT]
@channingwalton
channingwalton / gist:1552195
Created January 2, 2012 21:28
A little scalaz IO action
import scalaz._
import Scalaz._
import scalaz.effects._
import java.io._
/**
* Background reading:
* http://www.stackmob.com/2011/12/scalaz-post-part-2/
* http://blog.sigfpe.com/2007/11/io-monad-for-people-who-simply-dont.html
@channingwalton
channingwalton / gist:1613219
Created January 14, 2012 22:51
Variation on Miles's increasing seq, this is a decreasing seq
object Generics {
import shapeless.HList
import shapeless.Nat
import shapeless.Nat._
import shapeless._
trait <[A <: Nat, B <: Nat]
implicit def lt1[B <: Nat] = new <[_0, Succ[B]] {}
implicit def lt2[A <: Nat, B <: Nat](implicit lt: A < B) = new <[Succ[A], Succ[B]] {}
@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 / 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: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.