Skip to content

Instantly share code, notes, and snippets.

View noelmarkham's full-sized avatar

Noel Markham noelmarkham

  • 47 Degrees
View GitHub Profile
@noelmarkham
noelmarkham / CatsMocking.scala
Last active May 26, 2022 14:32
Mocking using Cats
package nm
import cats._
import cats.implicits._
import cats.data.WriterT
object Mocking {
@noelmarkham
noelmarkham / imports.md
Last active January 5, 2017 18:00
Cats imports cheat sheet
import... What it imports
cats.std.type Typeclass instances for standard library type (think List, Option)
cats.syntax.type “Enhanced” methods for type (.toRightXor etc)
cats.data.type Imports a type not part of the standard library (think Xor, Kleisli) and its typeclass instances
@noelmarkham
noelmarkham / keybase.md
Created March 7, 2015 19:12
keybase.md

Keybase proof

I hereby claim:

  • I am noelmarkham on github.
  • I am noel (https://keybase.io/noel) on keybase.
  • I have a public key whose fingerprint is 89A2 B6E9 B309 3F3A 752B 705A 2660 0E7B 872A 52AB

To claim this, I am signing this object:

@noelmarkham
noelmarkham / AddOne.scala
Created January 15, 2015 22:16
Scala coding dojo: Adding one to the first parameter of a case class, when that parameter is an integer
import shapeless._
import shapeless.ops.hlist.IsHCons
object ShapelessDojo {
def addOneToCaseClass[C, H <: HList, E, T <: HList]
(c: C)
(implicit gen: Generic.Aux[C, H],
h: IsHCons.Aux[H, E, T],
ev: E =:= Int,
@noelmarkham
noelmarkham / scalacheck-arbitrary-function.scala
Last active August 29, 2015 14:11
Scalacheck generated functions return the same output for any input
scala> import org.scalacheck._
import org.scalacheck._
scala> val generatedFunc = Gen.resultOf[String => Int, String => Int](identity).sample.get
generatedFunc: String => Int = <function1>
scala> generatedFunc("hello") == generatedFunc("goodbye")
res0: Boolean = true
@noelmarkham
noelmarkham / shapeless.scala
Created December 12, 2014 21:24
Swapping the first two values of a case class, when their type is the same
// Given a case class where the first two elements have the same type, swap those values:
def swap[T, U <: HList, H1, H2, T1 <: HList, T2 <: HList](v: T)(
implicit // proofs:
// Case class T can be represented by U
// (U is an output, must be used as an output before using it as an input in the proofs below)
gen: Generic.Aux[T, U],
// U is composed of H1 :: T1
isc1: IsHCons.Aux[U, H1, T1],
@noelmarkham
noelmarkham / shapeless-quickcheck.scala
Created December 11, 2014 20:37
Automatic typeclass (scalacheck arbitrary here) generation using Shapeless
scala> :paste
// Entering paste mode (ctrl-D to finish)
import org.scalacheck.Prop.forAll
import org.scalacheck.Properties
import shapeless.contrib.scalacheck._
case class WebServerConfig(hostname: String, port: Int)
case class ApplicationConfig(webServer: WebServerConfig)
@noelmarkham
noelmarkham / scalazMemo.scala
Created December 3, 2014 16:14
Questionable use of Scalaz Memo
object FlakyFunctionTest {
import scalaz._
import Scalaz._
var state = false
val function: Unit => String = scalaz.Memo.immutableHashMapMemo { _ =>
println(s"We received a call")
@noelmarkham
noelmarkham / futuresAndLenses.scala
Last active August 29, 2015 14:10
Futures and lenses
scala> usernameToUserIdLens.get("Alan").foreach(println)
** SELECT ID FROM USERS WHERE NAME = Alan
1
scala> userIdToStreetNameLens.get(1).foreach(println)
** SELECT STREET_NAME FROM ADDRESS WHERE USER_ID = 1
High Street
scala> val usernameToStreetNameLens = usernameToUserIdLens >=> userIdToStreetNameLens
@noelmarkham
noelmarkham / composing.scala
Last active August 29, 2015 14:08
Composing Functors
scala> val FL = Functor[List]
FL: scalaz.Functor[List] = scalaz.std.ListInstances$$anon$1@575f20e3
scala> val FF = Functor[Future]
FF: scalaz.Functor[scala.concurrent.Future] = scalaz.std.FutureInstance@678bc1a9
scala> val FF_FL = FF compose FL
FF_FL: scalaz.Functor[[α]scala.concurrent.Future[List[α]]] = scalaz.Functor$$anon$1@49487384
scala> val values = Future(List(1, 2, 3, 4, 5))