Skip to content

Instantly share code, notes, and snippets.

View benhutchison's full-sized avatar

Ben Hutchison benhutchison

View GitHub Profile
@benhutchison
benhutchison / contravariant.scala
Created October 31, 2012 01:38 — forked from eed3si9n/contravariant.scala
implementation of Equal typeclass using contravariance - difficulties resolved
object Test extends App {
trait CanEqual[-A] {
def equals(a1: A, a2: A): Boolean
}
object CanEqual {
def apply[A](implicit ev: CanEqual[A]): CanEqual[A] = ev
def equals[A](f: (A, A) => Boolean): CanEqual[A] = new CanEqual[A] {
def equals(a1: A, a2: A): Boolean = f(a1, a2)
}
@benhutchison
benhutchison / initScalajsCrossProject.sh
Last active August 29, 2015 14:07
Script to init a new Scala JS/JVM cross-built project with an Intellij friendly layout. After running, just Open the directory in Intellij and Import. Uses https://github.com/lihaoyi/utest#jscrossbuild
#!/bin/bash
[ $# -eq 0 ] && { echo "Usage: $0 <projectName>"; exit 1; }
PROJECT=$1
echo "Creating project in ./$PROJECT"
mkdir -p $PROJECT/{js,jvm,project}
mkdir -p $PROJECT/shared/src/{main,test}/{scala,resources}
cd $PROJECT
// The following, models a HAL Resource based on HAL specification:
// http://stateless.co/hal_specification.html
// And provides Argonaut JSON encoders for that model
// (Argonaut is a purely functional Scala JSON library)
// http://argonaut.io/
import shapeless._
import shapeless.ops.hlist.{ToTraversable, Mapper}
import argonaut._, Argonaut._
import scala.language.existentials
import scala.language.higherKinds
@benhutchison
benhutchison / gist:9a1b6d891211ea18d699
Created April 23, 2015 06:18
Property-based test of Queue.cycle(n: Int)
package com.github.benhutchison.richutil
import org.specs2.ScalaCheck
import org.specs2.mutable.Specification
import org.scalacheck.Arbitrary
import spire.math.Natural
class QueueUtilSpec extends Specification with ScalaCheck {
import collection.immutable.Queue
import ScalacheckIntegration._
@benhutchison
benhutchison / FirstNonEmpty-MonoidLawCheck-AmmoniteRepl
Created February 11, 2016 13:04
Is the firstNonEmpty Monoid lawful? Lets get an opinion from the LawBot that Cats & friends use...
Welcome to the Ammonite Repl 0.5.1
(Scala 2.11.7 Java 1.8.0_66)
ben_hutchison-ben_hutchison@ load.ivy("org.typelevel" %% "cats" % "0.4.1")
ben_hutchison-ben_hutchison@ load.ivy("org.typelevel" %% "alleycats-core" % "0.1.3")
ben_hutchison-ben_hutchison@ import cats._, cats.std.all._
import cats._, cats.std.all._
ben_hutchison-ben_hutchison@ import alleycats._
import alleycats._
@benhutchison
benhutchison / Ammonite Repl session
Last active August 16, 2016 12:11
Shapeless Poly woes when combining default case with case over sealed hierarchy (and a solution)
Welcome to the Ammonite Repl 0.5.2
(Scala 2.11.7 Java 1.8.0_51)
@ load.ivy("com.chuusai" %% "shapeless" % "2.2.5")
@ import shapeless._
import shapeless._
@ sealed trait Country
defined trait Country
@ case object Germany extends Country
defined object Germany
@benhutchison
benhutchison / EffPlay.scala
Last active April 26, 2016 08:20
Playing with Eff [https://github.com/atnos-org/eff-cats]. Discovered a cute syntax where effects can be written as context-bounds on the stack type (eg `def rollDice[E: RNG: Log]`). Also, burned some time discovering that type aliases seem essential when defining effect stacks.
import org.atnos.eff._
import cats.syntax.all._
import cats.data._
import Eff._
object EffPlay extends App {
import Effects._
import WriterEffect._
import ReaderEffect._
> effJVM/bench:compile
[warn] Credentials file /Users/ben/.sbt/specs2.credentials does not exist
[info] Updating {file:/Users/ben/others-code/eff-cats/}effJVM...
[info] Resolving jline#jline;2.12.1 ...
[info] Done updating.
[info] Compiling 1 Scala source to /Users/ben/others-code/eff-cats/jvm/target/scala-2.11/bench-classes...
[error] /Users/ben/others-code/eff-cats/jvm/src/bench/scala/EffBenchmark.scala:21: type mismatch;
[error] found : scalaz.Name[V]
[error] required: org.atnos.eff.EvalEffect.Eval[?]
[error] (which expands to) cats.Eval[?]
@benhutchison
benhutchison / gist:9353b7ff96ace04f488f7fb884c8b7e1
Created June 20, 2016 23:37
Alleycats SBT> whatDependsOn Scalacheck 0.12.x vs 0.13.x
> testsJVM/whatDependsOn org.scalacheck scalacheck_2.11 1.12.5
[info] org.scalacheck:scalacheck_2.11:1.12.5 (evicted by: 1.13.1)
[info] +-org.spire-math:algebra-laws_2.11:0.4.2 [S]
[info] | +-org.typelevel:cats-laws_2.11:0.5.0 [S]
[info] | +-org.typelevel:alleycats-laws_2.11:0.1.6-SNAPSHOT [S]
[info] | +-org.typelevel:alleycats-tests_2.11:0.1.6-SNAPSHOT [S]
[info] |
[info] +-org.typelevel:alleycats-laws_2.11:0.1.6-SNAPSHOT [S]
[info] | +-org.typelevel:alleycats-tests_2.11:0.1.6-SNAPSHOT [S]
[info] |
@benhutchison
benhutchison / AmmoniteReplSession
Last active July 5, 2016 05:06
Eff: Can a Member type that takes a type parameter be expressed as a context bound of the effect type?
~$> ./amm
Loading...
Welcome to the Ammonite Repl 0.6.2
(Scala 2.11.8 Java 1.8.0_51)
@ load.plugin.ivy("org.spire-math" %% "kind-projector" % "0.8.0")
@ load.ivy(("org.atnos" %% "eff-cats" % "1.7.4"))
@ import cats.data._
import cats.data._