Skip to content

Instantly share code, notes, and snippets.

@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 } },
...
@drstevens
drstevens / TestHelpersSuite.scala
Created November 27, 2012 06:01
Provide more type safe equalizer implementation and provide a way to disable the implicit === conversion
package com.bifflabs
import org.scalatest.FunSuite
import scalaz._
import scalaz.Scalaz._
class TestHelpersSuiteWithScalazSupport extends FunSuite with IgnoringDefaultEqualizer {
import AssertionsWithScalazSupport._
test("1 should equal 1") {
@drstevens
drstevens / gist:4438371
Last active December 10, 2015 13:08 — forked from OleTraveler/gist:4347051
This demonstrates some of the ways to combine scalaz6 Validaitons. It is the result of answers to question http://stackoverflow.com/questions/13961940/chaining-scalaz-validation-functions-function1a-validatione-b. See answers for more, including combining functions using Kliesi in scalaz6 and scalaz7.
def allDigits: (String) => ValidationNEL[String, String]
def maxSizeOfTen: (String) => ValidationNEL[String, String]
def toInt: (String) => ValidationNEL[String, Int]
val validInt: String => ValidationNEL[String, Int] = s =>
for {
validStr <- (allDigits(s) |@| maxSizeOfTen(s))((_,x) => x)
i <- toInt(validStr)
@drstevens
drstevens / Futures.scala
Created January 21, 2013 21:28
Define Apply[Future] in order to sequence. Written for Akka 1.3.1 (old, I know) and Scalaz 6.0.4. I imagine similar could be written for Akka 2.1.x and Scalaz 7. I've used this in many places so far without issue, but I've only used it for sequencing through something like `Validation[S, Future[Validation[S, Future[B]]]`. If I'm breaking some la…
object FuturePimps {
/**
* Use this instead of new AlreadyCompletedFuture[T] to prevent accidental use of default Akka timeout
**/
def alreadyCompletedFuture[T](v: T)(implicit timeout: Timeout): AlreadyCompletedFuture[T] =
new AlreadyCompletedFuture[T](Right(v), timeout.duration.toMillis)
class FutureFuture[T](f: () => Future[Future[T]]) {
def flatten: Future[T] = f().flatMap(identity)
@drstevens
drstevens / OptionMap.scala
Last active December 15, 2015 19:49
This example demonstrates benefits of using fold/cata instead of map and getOrElse on Option[T]. There are, of course, occasions when pattern matching is preferable as well.
Welcome to Scala version 2.10.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_15).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val opt: Option[Int] = Some(10)
opt: Option[Int] = Some(10)
scala> val typeCheckFail = opt map (_ + "2") getOrElse (3)
typeCheckFail: Any = 102
@drstevens
drstevens / gist:5579549
Last active December 17, 2015 08:19
Attempt to sequence List[StateT[Trampoline,A,B]]] with Trampoline in Scalaz 7. Unable to get this to work in scala 2.9.2 REPL. Works in 2.10.1 REPL. See https://gist.github.com/larsrh/5579789 also
import scalaz.std.anyVal._
import scalaz.std.tuple._
import scalaz.std.option._
import scalaz.StateT
import scalaz.State
import scalaz.Free
import scalaz.Free._
import scalaz.State._
import scalaz.Trampoline._
import scalaz.syntax.traverse._
@drstevens
drstevens / Foo.scala
Created December 16, 2013 21:32
Experiments with scalaz-stream. Attempt to maintain state by calls to next.
import scala.collection.immutable
import scalaz.std.string._
import scalaz.std.list._
import scalaz.std.anyVal._
import scalaz.std.option._
import scalaz.syntax.show._
import scalaz.concurrent.Task
import scalaz.stream.{process1, io, Process}
import scalaz.stream.Process._