Skip to content

Instantly share code, notes, and snippets.

@dcbriccetti
dcbriccetti / WeightedQuickUnion.scala
Created August 13, 2012 05:06
A Scala version of the weighted quick-union algorithm from Sedgewick’s Coursera Algorithms class
val unions = Seq(4->3, 3->8, 6->5, 9->4, 2->1, 5->0, 7->2, 6->1, 1->0)
val parents = (0 to 9).toArray
val depths = Array.fill(10)(1)
def connected(p: Int, q: Int) = root(p) == root(q)
def union(p: Int, q: Int) {
val i = root(p)
val j = root(q)
val (sm, lg) = if (depths(i) < depths(j)) (i, j) else (j, i)
@iron9light
iron9light / MixedTypeArrayJsonSuite.scala
Created July 19, 2012 05:51
lift json serializing mixed type array
package net.liftweb.sandbox
import org.scalatest.FunSuite
import net.liftweb.json._
import org.scalatest.matchers.ShouldMatchers
/**
* @author IL
* @see <a href="https://groups.google.com/forum/?fromgroups#!topic/liftweb/GK5DbzCtWdA">forum</a>
*/
@retronym
retronym / implicitly-either.scala
Created April 20, 2012 22:24
implicitly-either
scala> def implicitlyEitherImpl[A: c.TypeTag, B: c.TypeTag](c: reflect.makro.Context): c.Expr[Either[A, B]] = {
| def i[X](implicit X: c.TypeTag[X]): c.Tree = c.inferImplicitValue(X.tpe, silent = false)
| i[A] match {
| case c.mirror.EmptyTree =>
| i[B] match {
| case c.mirror.EmptyTree =>
| sys.error("Neither A nor B are available implicitly")
| case b =>
| c.reify(Right[A, B](c.Expr[B](b).eval))
| }
@viktorklang
viktorklang / minscalaactors.scala
Last active March 25, 2024 19:01
Minimalist Scala Actors
/*
Copyright 2012-2021 Viktor Klang
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
@raichoo
raichoo / gist:2345414
Created April 9, 2012 18:50
Monads vs. implicit values in Scala
case class Config(host: String, port: Int) {
def prettyPrint(prefix: String, msg: String): String =
List(prefix, ": ", msg, " on ", host, ":", port.toString).mkString
}
/**
* Passing a configuration with implicits is like
* working in the state monad. You can "put" a
* new configuration even though we don't want that
* to happen in this particular example. We don't
@Shadowfiend
Shadowfiend / ReloadOnSessionLoss.scala
Created April 6, 2012 03:04
Drop-in reloading of a client on session loss based on calling a local window.serverReload function.
package bootstrap.liftweb {
import net.liftweb.common._
import net.liftweb.http._
import js._
import LiftRules._
import net.liftweb.util.Helpers._
/**
* Provides automatic reloading of the client on session loss.
*
@heralight
heralight / WebSpec2.scala
Created March 29, 2012 08:43
lift:WebSpec2 + Unit test
package code.mockweb
/*
* Copyright 2011 WorldWide Conferencing, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
@chrislewis
chrislewis / comprehensible.scala
Created March 11, 2012 23:32
general type and implicit conversion to add support for using a general Bind instance in a for comprehension
trait Bind[M[_]] {
def fmap[A, B](ma: M[A], f: A => B): M[B]
def bind[A, B](ma: M[A], f: A => M[B]): M[B]
}
trait Monad[M[_]] extends Bind[M]{
def unit[A](a: => A): M[A]
@pedroteixeira
pedroteixeira / SpamLord.java
Created March 11, 2012 23:15
coursera nlp pa1
// Modified SpamLord.java to call clojure code
public List<Contact> processFile(String fileName, BufferedReader input) {
List<Contact> contacts = new ArrayList<Contact>();
// for each line
Matcher m;
String email;
try {
List results = new spamlord().process(input);
@tjweir
tjweir / gist:1259467
Created October 3, 2011 16:04 — forked from mccv/gist:1259343
OAuth with finagle streaming
import java.net._
import java.util.UUID
import com.twitter.conversions.time._
import com.twitter.finagle.builder.ClientBuilder
import com.twitter.util._
import java.nio.charset.Charset
import org.jboss.netty.buffer.{ChannelBuffers, ChannelBuffer}
import org.jboss.netty.handler.codec.http._
import com.twitter.finagle.stream.Stream