Skip to content

Instantly share code, notes, and snippets.

View mjhopkins's full-sized avatar

Mark Hopkins mjhopkins

View GitHub Profile
@mjhopkins
mjhopkins / AndPatterns.hs
Created May 14, 2018 09:12
And patterns in Haskell
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}
module AndPatterns where
pattern DivBy3 :: Integral i => i
pattern DivBy3 <- ((`mod` 3) -> 0)
pattern DivBy5 :: Integral i => i

Keybase proof

I hereby claim:

  • I am mjhopkins on github.
  • I am mjhopkins (https://keybase.io/mjhopkins) on keybase.
  • I have a public key whose fingerprint is CDD5 00B6 B918 11BB D027 5160 0888 F607 EE67 1CE8

To claim this, I am signing this object:

@mjhopkins
mjhopkins / EvenOdd.idr
Created June 8, 2017 01:55
Idris code from Sydney Type Theory meeting 5 June 2017
module EvenOdd
data Even : (n : Nat) -> Type where
ZeroIsEven : Even 0
SuccSuccEven : Even n -> Even (S (S n))
data Odd : (n : Nat) -> Type where
OneIsOdd : Odd 1
SuccSuccOdd : Odd n -> Odd (S (S n))
@mjhopkins
mjhopkins / roles.scala
Last active February 7, 2017 03:21
Demonstrates a scenario where Scala fails to refine a type
sealed trait Role[N <: Entity]
case object VIP extends Role[Customer]
case object Shopper extends Role[Customer]
case object Manager extends Role[Staff]
case object Employee extends Role[Staff]
sealed trait Entity
@mjhopkins
mjhopkins / Options.scala
Last active August 29, 2015 14:27
Experiments with typesafe apis
package api
import shapeless.{Succ, _0, Nat}
import shapeless.nat._2
trait BasicDefinitions {
// type level booleans
sealed trait Flag
trait On extends Flag
trait Off extends Flag
object CreateSequence {
import com.github.nscala_time.time.Implicits._
import Sequences._
val seq: Seq3[Int, String, Double] = an[Int].within(5.minutes)[String].within(10.minutes)[Double]
}
object Sequences {
def an[A] = new SeqBuilder[A]
def a[A] = new SeqBuilder[A]
@mjhopkins
mjhopkins / FlattenTupleExample.scala
Created January 22, 2015 00:13
FlattenTupleExample
object FlattenTuple {
import shapeless._
import ops.tuple.FlatMapper
import syntax.std.tuple._
trait LowPriorityFlattenTuple extends Poly1 {
implicit def default[T] = at[T](Tuple1(_))
}
import scala.util.matching.Regex
import scalaz._
import Scalaz._
import java.io.File
/*
Process a file containing <key, string, float> columns and print some stats on it.
Let's assume the columns are separated by tab characters.
*/
@mjhopkins
mjhopkins / TypeclassDemo.scala
Last active December 26, 2015 17:59
Typeclass demo (with inheritance)
import com.google.protobuf.GeneratedMessage
import org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos.{RenameRequestProto, AppendResponseProto}
import org.apache.hadoop.hdfs.protocol.proto.DataTransferProtos.ClientReadStatusProto
import scalaz.{Tag, @@}
/*
Type class usage demo
*/
object byteArrayable {