Skip to content

Instantly share code, notes, and snippets.

import $ivy.`com.chuusai::shapeless:2.3.3`
import shapeless._
import shapeless.poly._
import shapeless.ops.hlist._
import shapeless.UnaryTCConstraint._
trait ColumnType[T] {
@IainHull
IainHull / MaybeVect.idr
Created October 20, 2015 07:26
My first Idris function, maybeVect
module MaybeVect
import Data.Vect
maybeVect : (n: Nat) -> List a -> Maybe (Vect n a)
maybeVect Z [] = Just []
maybeVect n [] = Nothing
maybeVect Z xs = Nothing
maybeVect n (x :: xs) = let S(k) = n in
map (\ xs' => x :: xs') (maybeVect k xs)
@IainHull
IainHull / PathDependentTypes.scala
Last active August 29, 2015 14:17
PathDependentTypes
package org.iainhull.types
/**
* Created by iain.hull on 17/03/2015.
*/
package pdt {
import java.io.OutputStream
@IainHull
IainHull / AmountsAndRates.scala
Last active August 29, 2015 14:16
Improve your correctness with Types - Scala Days SF 2015
case class Order(orderId: String,
customerId: String,
items: Vector[OrderItem],
amount: MoneyAmount,
submittedAt: DateTime)
case class Customer(name: String,
preferredCurrency: Currency)
class MoneyAmount(val amount: BigDecimal) extends AnyVal {
@IainHull
IainHull / Readme.md
Created November 5, 2014 09:59
Scala Type-safe Wrapper

Something small I have been working on to enhance basic types with type-safe wrappers to enforce invariants at compile time. Reading valid values from configuration was the initial driver for me to experiment with this.

I have just seen that @bvenners is working on something similar in Scalactic

I really like his approach, here is a quick comparison with Scalactic's version

  • it includes versions for Long, Float and Double; I just include Int
  • it uses macros to ensure that basic construction only uses a valid Literal (this is an awesome idea!!!!)
package org.iainhull.akka
import scala.concurrent.duration._
import akka.actor._
import akka.event.Logging
import akka.pattern.ask
import akka.util.Timeout
import akka.persistence.{PersistentView, AtLeastOnceDelivery, PersistentActor}
sealed trait Validation[A] {
def map[B](f: A => B): Validation[B] = this match {
case Success(a) => Success(f(a))
case Failures(errors) => Failures(errors)
}
def flatMap[B](f: A => Validation[B]): Validation[B] = this match {
case Success(a) => f(a)
case Failures(errors) => Failures(errors)
}
def filter(p: A => Boolean) = this match {
@IainHull
IainHull / DeathReportActorTest.scala
Last active August 29, 2015 13:58
Demonstrate intermittent InvalidActorNameException using akka DeathWatch
import akka.actor.ActorSystem
import akka.testkit.TestKit
import org.scalatest.Suite
import org.scalatest.junit.ShouldMatchersForJUnit
import akka.testkit.ImplicitSender
import akka.actor.Actor
import akka.testkit.TestProbe
import org.junit.Test
import akka.actor.ActorRef
import akka.actor.Props
@IainHull
IainHull / AkkaCake.scala
Created November 25, 2013 17:41
Applying Akka's recommended practices for actor creation (See http://doc.akka.io/docs/akka/snapshot/scala/actors.html#Recommended_Practices) to the Cake pattern example taken from answer to this question on StackOverflow (See http://stackoverflow.com/questions/15996098/akka-and-cake-pattern). I have added the props method to the `ServiceActor` o…
trait DBComponent {
def db: DB
type K
type V
trait DB {
def put(key: K, value: V): Unit
def get(key: K): Option[V]
}