Skip to content

Instantly share code, notes, and snippets.

@Mortimerp9
Mortimerp9 / ReaderM.scala
Created April 16, 2013 23:20
a monad transformer for Reader presented in this gist: https://gist.github.com/Mortimerp9/5400194 This is a companion code for the following post: https://coderwall.com/p/ibrhta
import scala.concurrent.Future
import scala.concurrent.ExecutionContext
/**
* a ReaderM is a tool for Readers containing other monads, such as Reader[_, Option[_]] or Reader[_, Future[_]]
*/
case class ReaderM[-C, A, M[+A]](val read: Reader[C, M[A]])(implicit canMap: CanMap[A, _, M]) {
def apply(conn: C): M[A] = read(conn)
/**
@Mortimerp9
Mortimerp9 / CanMap.scala
Created April 16, 2013 22:27
A generic implementation of Reader with a naive adhoc implementation of a Monad typeclass. This is a companiong code for this post: https://coderwall.com/p/-egcfq
import scala.collection._
import scala.collection.generic._
import scala.concurrent.{ Future, ExecutionContext }
/**
* a Typeclass representing a class that can map and flatMap (collections, Option, Future..).
* effectively, it's a Monad without enforcing the axioms of a Monad.
*/
trait CanMap[A, B, M[_]] {
def map(l: M[A])(f: A => B): M[B]
@Mortimerp9
Mortimerp9 / readerwithtooling.scala
Created April 14, 2013 22:14
An implementation of the Reader Monad in scala, with correct type variance and some implicit utils to simplify the daily use of Readers, In particular with Future.
/**
* A monad to abstract dependencies in the code, see https://coderwall.com/p/kh_z5g
*/
object Reader {
/**
* an implicit to convert a function A => B in a Reader[A, B]
*/
implicit def reader[C, R](block: C => R): Reader[C, R] = Reader(block)
@Mortimerp9
Mortimerp9 / ReaderCovariance.scala
Last active October 13, 2017 16:12
Sample code for the following post https://coderwall.com/p/pdrz7q
object test {
/**
* The companion object
*/
object Reader {
/**
* automatically wrap a function in a reader
*/
@Mortimerp9
Mortimerp9 / variantworksheet.scala
Last active December 16, 2015 04:39
Sample code for the following post https://coderwall.com/p/pdrz7q
object test {
//the companion object, with functions to help in creating readers
object Reader {
implicit def reader[C, R](block: C => R) = Reader[C, R](block)
def pure[From, To](a: To) = Reader((c: From) => a)
}
@Mortimerp9
Mortimerp9 / gist:5364769
Created April 11, 2013 16:13
example custom matcher
trait ID
case class SequentialID(id: Int) extends ID
case class OtherID(id: Int) extends ID
case class User(val id: ID, val b: Int, val c: Int)
object UserId {
def unapply(w: User) = w match {
case u @ User(SequentialID(id), _, _) => Some(u, id)
@Mortimerp9
Mortimerp9 / asSoonAsPossibleEnumeratee
Created March 31, 2013 02:24
an experiment in pushing the next Future that is ready in an enumerator to serve results as they are computed with Play Framework.
import scala.concurrent._
import scala.concurrent.{ Future, Await }
import scala.util.Random
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import scala.util.{ Try, Success, Failure }
import scala.concurrent.duration._
import play.api.libs.iteratee._
def takeOrDeadline[E](count: Int, deadline: Deadline): Enumeratee[E, E] = new Enumeratee.CheckDone[E, E] {
@Mortimerp9
Mortimerp9 / ConfigString.scala
Created March 26, 2013 20:38
Shortcuts for configuring Play2 Applications in scala
package utils
import scala.concurrent.duration.FiniteDuration
import scala.concurrent.duration._
import play.api.Application
import collection.JavaConversions._
object ConfigString {
implicit class ConfigStr(s: String) {
def configOrElse(default: FiniteDuration)(implicit app: Application): FiniteDuration =
@Mortimerp9
Mortimerp9 / gist:4985630
Last active December 13, 2015 22:39
Java vs Scala list operation from Raúl Raja Martínez + dealing with an exception

JAVA

List<Integer> even = new ArrayList<Integer>();
for (String num : numbersAsStrings) {
 try {
  int parsedInt = Integer.parseInt(num);
  if (parsedInt % 2 == 0) {
    ints.add(parsedInt);
 }
@Mortimerp9
Mortimerp9 / rgbafallback.pl
Created November 8, 2012 02:08
This is a very rough perl script that will go through a css file and find the possible rgba statements, it will then insert a fallback hex value before the rgba statement. As this was tailored to a particular use case, it only deals with background color
#!/usr/bin/perl
use strict;
sub min {
my ($a, $b) = @_;
if($a < $b) { return $a; }
else { return $b; }
}