Skip to content

Instantly share code, notes, and snippets.

View larsrh's full-sized avatar
🏝️
On hiatus.

Lars Hupel larsrh

🏝️
On hiatus.
View GitHub Profile
@larsrh
larsrh / oo.sh
Created January 5, 2012 10:19
Object oriented bash
### from <http://lab.madscience.nl/oo.sh.txt>
#!/bin/bash
# ---------------------------------------------------------------------------
# OO support functions
# Kludged by Pim van Riezen <pi@madscience.nl>
# ---------------------------------------------------------------------------
DEFCLASS=""
CLASS=""
@larsrh
larsrh / HStream.scala
Created April 9, 2012 20:46
Infinite heterogeneous list
// Context: https://github.com/larsrh/scalaz/tree/scalaz-seven/typelevel/src/main/scala/scalaz/typelevel
package scalaz
package typelevel
object HStream {
def fromForall[T[_]](elems: Forall[T]): HStream[T] = new HStream[T] {
def apply[N <: Nat](n: N) = elems[N]
}
@larsrh
larsrh / index.scala
Created April 12, 2012 14:48
An alternative approach to index-access in HLists
// Context <https://gist.github.com/2346456>
val hlist = 1 :: "foo" :: 3.0f :: HNil
val nones = HStream.const(None)
val wrapSome = new (Id ~> Some) { def apply[T](t: T) = Some(t) }
val somelist = hlist.transform(wrapSome)
val stream = somelist +: nones
@larsrh
larsrh / union.scala
Created April 13, 2012 11:12
(disjoint) unions
// Situation now
object UnionTypes {
type ![A] = A => Nothing
type !![A] = ![![A]]
trait Disj { self =>
type D
type t[S] = Disj {
@larsrh
larsrh / AEither.scala
Created April 13, 2012 15:09
(disjoint) unions, second attempt
trait AEither[L <: TList] {
// an `HList` where all elements are functions producing `R`
type Fold[R] = L#ToKList[({ type λ[α] = α => R })#λ]#Down
def fold[R](list: Fold[R]): R
}
case class AEitherCons[H, T <: TList](either: Either[H, AEither[T]]) extends AEither[TCons[H, T]] {
@larsrh
larsrh / nameplicitly.scala
Created July 3, 2012 12:29
Attempting a by-name implicitly
import language.experimental.macros
import scala.reflect.makro.Context
object Name {
def nameplicitly[T](implicit t0: T): Name[T] = macro nameplicitly_impl[T]
def nameplicitly_impl[T : c.TypeTag](c: Context)(t0: c.Expr[T]): c.Expr[Name[T]] =
c.reify(new Name[T] { def t = t0.splice })
@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
@larsrh
larsrh / stackoverflow.scala
Created August 30, 2012 20:31
map as function
// <http://stackoverflow.com/q/12204869>
def map[T, U, X, CC[X] <: Traversable[X]](cct: CC[T], f: T => U)(implicit cbf: CanBuildFrom[CC[T], U, CC[U]]): CC[U] =
cct.map(t => f(t))(collection.breakOut(cbf))
/*
scala> map(List(1,2,3), (x: Int) => x.toString)
res1: List[java.lang.String] = List(1, 2, 3)
*/
@larsrh
larsrh / RequestHeader.scala
Created September 19, 2012 21:43 — forked from bblfish/RequestHeader.scala
Claim Monad for X509 Certificates - having trouble running map on it
import java.net.URI
import scalaz.concurrent.Promise
import scalaz._
import Scalaz._
import language.implicitConversions
case class Cert(cn: String, pubKey: BigInt, webids: List[URI] )
trait RequestHeader {
@larsrh
larsrh / klist.scala
Created November 7, 2012 11:16
Compiler failure in 2.10.x
sealed trait GenericList[+M[_]] {
final def :^:[A, N[X] >: M[X]](elem: N[A]): GenericCons[N, A, this.type] =
GenericCons[N, A, this.type](elem, this)
}
case class GenericCons[M[_], H, +T <: GenericList[M]](
head: M[H],
tail: T