Skip to content

Instantly share code, notes, and snippets.

@bhudgeons
bhudgeons / AggregationFrameworkTest.scala
Created February 21, 2012 16:24
MongoDB Aggregation Framework Via Casbah
import com.mongodb.casbah.Imports._
/*
* Simple test of the MongoDB Aggregation Framework via Casbah
*
* [Note: only works on MongoDB 2.1+]
*
* The "students" collection consists of student test score records that [simplified] look like this:
*
{
@bhudgeons
bhudgeons / gist:3101404
Created July 12, 2012 22:06
Wunderground Weather API Integration with Databinder-Dispatch
import dispatch._
import dispatch.liftjson.Js._
import net.liftweb.json.JsonAST.JString
final val APIKEY = "XXXXXXXXX" // get a free key at http://www.wunderground.com/weather/api/
def getTemp(city: String, state: String) = {
val http = new Http
val url = :/("api.wunderground.com") / "api" / APIKEY / "geolookup" / "conditions" / "q" / state / (city + ".json")
http(url ># { json =>
@bhudgeons
bhudgeons / Evernote Folder Action.scpt
Created August 22, 2012 05:12
AppleScript that creates an Evernote "drop box" for files.
(*
Works in Evernote v.3.3.0.
(Note: I've read of people having problems with
AppleScript with the AppStore version of Evernote.
If you're having trouble, you might want to sync,
then delete the appstore version and download the
mac app directly from Evernote.)
Based on:
@bhudgeons
bhudgeons / gist:3855170
Created October 8, 2012 21:39
Casbah 3.0 MongoDBList Strangeness
scala> import com.mongodb.casbah._
import com.mongodb.casbah._
scala> val dbl = MongoDBList()
16:23:48.222 [Thread-35] DEBUG c.m.c.c.c.s.package$RegisterConversionHelpers$ - Registering Scala Conversions.
16:23:48.225 [Thread-35] DEBUG c.m.c.c.c.s.package$RegisterConversionHelpers$ - Deserializers for Scala Conversions registering
16:23:48.226 [Thread-35] DEBUG c.m.c.c.c.s.package$RegisterConversionHelpers$ - Serializers for Scala Conversions registering
16:23:48.227 [Thread-35] DEBUG c.m.c.c.c.s.package$RegisterConversionHelpers$ - Setting up OptionSerializer
16:23:48.232 [Thread-35] DEBUG c.m.c.c.c.s.package$RegisterConversionHelpers$ - Setting up ScalaJCollectionSerializer
16:23:48.238 [Thread-35] DEBUG c.m.c.c.c.s.package$RegisterConversionHelpers$ - Setting up ScalaRegexSerializers
@bhudgeons
bhudgeons / gist:4089284
Created November 16, 2012 17:37
Awesome Java Random WAT (via @joshbloch)
scala> import util.Random
import util.Random
scala> (1 to 1000) map (i => (new Random(i)).nextInt(2))
res25: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...
scala> (1 to 1000) map (i => (new Random(i)).nextInt(4))
res26: scala.collection.immutable.IndexedSeq[Int] = Vector
@bhudgeons
bhudgeons / MyRequestBuilder.scala
Created June 28, 2013 22:16
Scala port of com.ning.http.client.RequestBuilder so that I can override setUrl to overcome the standard RequestBuilder's attempts to "fix" non-standard URL query strings. See https://bhudgeons.telegr.am/blog_posts/handling-non-standard-urls-in-dispatch
package at.atsoft.util
import com.ning.http.client.RequestBuilder
import com.ning.http.client.Request
import com.ning.http.client.FluentCaseInsensitiveStringsMap
import java.util.ArrayList
import com.ning.http.client.Cookie
import java.io.InputStream
import com.ning.http.client.Request.EntityWriter
import com.ning.http.client.BodyGenerator
@bhudgeons
bhudgeons / FutureTest.scala
Created July 15, 2013 21:29
Populating an Abstract Trait's Requirements with Futures
import scala.concurrent._
import scala.util.{ Success, Failure }
import ExecutionContext.Implicits.global
import scala.concurrent.duration._
trait Calculator {
def hardToCalculate1: Int
def hardToCalculate2: Int
def hardToCalculate3: Int
@bhudgeons
bhudgeons / notin.scala
Last active October 26, 2017 13:27
Slick notIn example
// This is the query to find all of a user's
// non-academy courses. In other words...
// I want to list all of this user's courses,
// but exclude courses associated with
// academies in which this user is a member.
val user:User = ...
val excludeTheseAcademies = (for {
au <- AcadUser if au.userId === user.id
scala> val test = testcollections.testcoll
test: com.mongodb.casbah.MongoCollection = testtest
scala> import com.mongodb.casbah.Imports._
import com.mongodb.casbah.Imports._
scala> test.save(MongoDBObject("list" -> List("one", "two", "three"))) // save a list to mongodb
res1: com.mongodb.WriteResult = N/A
scala> val o = test.findOne.get // get back the thing we just saved
@bhudgeons
bhudgeons / MyClusterActor.scala
Created May 8, 2014 22:09
Demonstration of Akka Cluster Singleton Communication Problem
package sample.cluster.simple
import com.typesafe.config.ConfigFactory
import akka.actor.ActorSystem
import akka.actor.Props
import akka.actor._
import akka.contrib.pattern.ClusterSingletonManager
import akka.contrib.pattern.ClusterSingletonProxy
class MyClusterActor extends Actor {