Skip to content

Instantly share code, notes, and snippets.

View noelmarkham's full-sized avatar

Noel Markham noelmarkham

  • 47 Degrees
View GitHub Profile
import scalaz._
import Scalaz._
import \&/._
sealed trait Error
case class NotFatalError(reason: String) extends Error
case class FatalError(reason: String) extends Error
def function(i: Int): List[Error] \&/ Int = i match {
import scalaz._
import syntax.std.option._
import OptionT._
import scala.concurrent._
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
val o1 = 1.some
val o2 = 2.some
@noelmarkham
noelmarkham / gist:4e8e5b4c705793ee93c0
Last active August 29, 2015 14:01
Spot the difference
import scala.concurrent._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
def fut(name: String, waitTime: Duration, result: Int): Future[Int] = {
Future {
println(s"$name started")
Thread.sleep(waitTime.toMillis)
println(s"$name finished")
result
@noelmarkham
noelmarkham / gist:5771197
Last active December 18, 2015 10:49
My attempt at Tony Morris's refactoring puzzle - see http://tmorris.net/posts/refactoring-puzzle/index.html
object RefactorPuzzle {
case class IntRdr[+A](read: Int => A) {
def map[B](f: A => B): IntRdr[B] =
IntRdr(f compose read)
def flatMap[B](f: A => IntRdr[B]): IntRdr[B] =
IntRdr(n => f(read(n)).read(n))
}
object IntRdr {
@noelmarkham
noelmarkham / gist:5451442
Created April 24, 2013 11:24
Scala console power mode
scala> :power
** Power User mode enabled - BEEP BOOP SPIZ **
** :phase has been set to 'typer'. **
** scala.tools.nsc._ has been imported **
** global._ and definitions._ also imported **
** Try :help, vals.<tab>, power.<tab> **
scala> :paste
// Entering paste mode (ctrl-D to finish)
@noelmarkham
noelmarkham / Type variance test
Created March 16, 2013 14:53
Testing variance in Java's generics. Why on earth doesn't this compile? In IntelliJ, getClass on line 13 errors as 'cannot resolve symbol' Javac doesn't produce a logical error.
public class GenericVariance {
class A {}
class B extends A {}
@Test
public void checkTypes() {
assertTrue(new B() instanceof A);
final List<A> listOfA = new ArrayList<>();
@noelmarkham
noelmarkham / gist:5019450
Last active December 14, 2015 03:18
Scala FizzBuzz
scala> import scalaz._
import scalaz._
scala> import Scalaz._
import Scalaz._
scala> def check(i: Int, divisor: Int, s: String): Option[String] = if (i % divisor == 0) s.some else None
check: (i: Int, divisor: Int, s: String)Option[String]
scala> (1 to 50) map (i => (check(i, 3, "Fizz") |+| check(i, 5, "Buzz")).getOrElse(i))
@noelmarkham
noelmarkham / gist:3743931
Created September 18, 2012 16:03
Kleisli composition
scala> import scalaz._, Scalaz._
import scalaz._
import Scalaz._
scala> def str(x: Int): Option[String] = Some(x.toString)
str: (x: Int)Option[String]
scala> def toInt(x: String): Option[Int] = Some(x.toInt)
toInt: (x: String)Option[Int]