Skip to content

Instantly share code, notes, and snippets.

@drstevens
drstevens / gist:9201268
Created February 25, 2014 02:03
Map.withDefaultValue is a bad idea
Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_25).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val map0 = Map.empty[Int, Int] withDefaultValue 0
map0: scala.collection.immutable.Map[Int,Int] = Map()
scala> val map1 = map0 mapValues(identity)
map1: scala.collection.immutable.Map[Int,Int] = Map()
@drstevens
drstevens / ApplicativeIdentityFail.scala
Created February 26, 2014 21:27
Applicative for play.api.libs.json.JsResult
Welcome to Scala version 2.10.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_25).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import play.api.libs.json._; import play.api.libs.functional.Applicative;
import play.api.libs.json._
import play.api.libs.functional.Applicative
scala> val expected = JsSuccess(10, JsPath(List(KeyPathNode("somekey"))))
expected: play.api.libs.json.JsSuccess[Int] = JsSuccess(10,/somekey)
@drstevens
drstevens / trampolined-state.scala
Last active August 29, 2015 14:02 — forked from travisbrown/trampolined-state.scala
Trampolining the state monad via applicative combinator appears to work
import scalaz._, Scalaz._
def setS(i: Int): State[List[Int], Unit] = modify(i :: _)
val s = (1 to 10000).foldLeft(state[List[Int], Unit](()).lift[Free.Trampoline]) {
case (st, i) => st *> setS(i).lift[Free.Trampoline]
}
s(Nil).run
@drstevens
drstevens / gist:2b6a9686881c1183ed55
Created September 18, 2014 18:31
Reduce boilerplate when not using primitives
import scalaz.Id.Id
import scalaz.BijectionT._
import scalaz.Lens
trait Bijectionz[A, C] {
def apply(str: C): A
def unapply(a: A): Option[C]
@drstevens
drstevens / gist:77db6bab6b1e995dac13
Created December 18, 2014 23:29
rough timing of alternate implementation of Rng.fill
import com.nicta.rng.Rng
import org.joda.time.DateTime
import scalaz._
import Scalaz._
object Foo extends App {
def time[A](f: => A): (Long, Long, Long) = {
val t0 = System.currentTimeMillis
val a = f
trait Writes[T] {
def writes(o: T): JsValue
}
trait Reads[T] {
def reads(json: JsValue): T
}
trait Format[T] extends Writes[T] with Reads[T]
@drstevens
drstevens / gist:1409829
Created November 30, 2011 17:05
Globally handle fatal exceptions in Rx.Net subscribers
/* This is how I am globally handling fatal exceptions thrown from Rx subscribers.
* It is what I came up with in response to my stackoverflow question here
* http://stackoverflow.com/questions/7210051/catching-exceptions-which-may-be-thrown-from-a-subscription-onnext-action
* This is far from ideal. From what I understand, exception handling has been improved greately in Rx for .NET 4.5
*/
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
@drstevens
drstevens / ReDeclaringRabbitAdmin.scala
Created January 23, 2012 19:34
Simple Implementenation of AmqpAdmin which maintains all declared Amqp objects in memory allowing automatic redeclaration of them on reconnect
package com.bostontechnologies.cscore.amqp
import com.weiglewilczek.slf4s.Logging
import collection.JavaConversions._
import collection.mutable.{SynchronizedSet, HashSet, ConcurrentMap}
import java.lang.String
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.AtomicBoolean
import org.springframework.amqp.core.{Queue, Binding, AmqpAdmin, Exchange}
import org.springframework.amqp.rabbit.core.RabbitAdmin
@drstevens
drstevens / Validationz.scala
Created April 19, 2012 20:42
Convert List[Validation[A, B]] to Validation[List[A], List[B]]
package com.mypackage
import org.specs.SpecificationWithJUnit
import scalaz._
import Scalaz._
class ValidationTests extends SpecificationWithJUnit {
"I should be able to convert List[Validation[A, B]] to Validation[List[A], List[B]]" >> {
@drstevens
drstevens / Default (OSX).sublime-keymap
Created November 5, 2012 20:15
Fixing Home/End behavior in sublime. Add the following to the top of default of user keybindings 'Sublime Text 2' -> Preferences -> Keybindings
/*
On OS X, basic text manipulations (left, right, command+left, etc) make use of the system key bindings,
and don't need to be repeated here. Anything listed here will take precedence, however.
*/
[
{ "keys": ["home"], "command": "move_to", "args": {"to": "bol"} },
{ "keys": ["end"], "command": "move_to", "args": {"to": "eol"} },
{ "keys": ["shift+end"], "command": "move_to", "args": {"to": "eol", "extend": true} },
{ "keys": ["shift+home"], "command": "move_to", "args": {"to": "bol", "extend": true } },
...