Skip to content

Instantly share code, notes, and snippets.

View debasishg's full-sized avatar
🏠
Working from home

Debasish Ghosh debasishg

🏠
Working from home
View GitHub Profile
@timcowlishaw
timcowlishaw / Trie.scala
Created November 14, 2011 10:06
A Trie (Prefix-tree) implementation in Scala
package uk.ac.ucl.cs.GI15.timNancyKawal {
class Trie[V](key: Option[Char]) {
def this() {
this(None);
}
import scala.collection.Seq
import scala.collection.immutable.TreeMap
import scala.collection.immutable.WrappedString
@HarryHuang
HarryHuang / cake_pattern_di.scala
Created November 19, 2011 11:54
Scala Dependency Injection: an improved cake pattern
//harry huang [huanghui.huang@gmail.com]
//
//After reading the original post [http://jboner.github.com/2008/10/06/real-world-scala-dependency-injection-di.html, ]
//the original cake pattern seems quite verbose for me, and it is quite invasive, so I spent a bit time
//and come up with an improved version of cake pattern, which I call it "Auto Cake DI". It is working
//well with any POST(plain old scala trait)/POSO(plain old scala object) which means that anything can be
//injected without modification or introducing new traits.
/*---------inject trait---------*/
trait Inject[+T] { def inject: T }
@retronym
retronym / config.scala
Last active May 9, 2018 05:47
Styles of config propagation: Manual, Implicits, DynamicVariable, Reader
package scalaz.example
object Reader extends App {
/**
* Manual propagation of the environment (in the example, `contextRoot`.)
*/
object Config0 {
def fragment1(contextRoot: String) = <a href={contextRoot + "/foo"}>foo</a>
@pchiusano
pchiusano / microbenchmark.markdown
Created December 2, 2011 13:49
Simple microbenchmarks comparing Scala vs Java mutable map performance

I was curious about the results reported here, which reports that Scala's mutable maps are slower than Java's: http://www.infoq.com/news/2011/11/yammer-scala

In my tests, Scala's OpenHashMap equals or beats java's HashMap:

Insertion 100k elements (String keys) time in ms:

  • scala HashMap: 92.75
  • scala OpenHashMap: 14.03125
  • java HashMap: 15.78125
@retronym
retronym / stept-alternative.scala
Created February 12, 2012 23:11
alternative encoding for StepT
package scalaz
package iteratee
import Iteratee._
/**
* The current state of an Iteratee, one of:
* - '''cont''' Waiting for more data
* - '''done''' Already calculated a result
* - '''err''' Error, unable to calculate a result
@retronym
retronym / thunk-reuse.scala
Created February 13, 2012 07:24
thunk-reuse
object StepT extends StepTFunctions with EnumeratorTInstances {
private val ToNone: ((=> Any) => None.type) = x => None
private def AnyToNone[A, X]: ((=> A) => Option[X]) = ToNone
private val ToNone1: ((Any) => None.type) = x => None
private def AnyToNone1[A, X]: (A => Option[X]) = ToNone1
private val ToNone2: ((=> Any, => Any) => None.type) = (x, y) => None
private def AnyToNone2[A, B, X]: ((=> A, => B) => Option[X]) = ToNone2
object Err {
def unapply[X, E, F[_], A](s: StepT[X, E, F, A]): Option[X] = {
@weavejester
weavejester / gist:1982807
Created March 6, 2012 01:47
Initial thoughts on Datomic

Initial thoughts on Datomic

Rich Hickey (of [Clojure][1] fame) has released a cloud-based database called [Datomic][2] that has some interesting properties.

Datomic is an log of assertions and retractions of "facts", much as a DVCS like [Git][3] is a log of code diffs. The state of the database at any one time is the sum of all the assertions and retractions up to that date.

@paulmillr
paulmillr / mapreduce.scala
Created March 10, 2012 16:03
Why functional programming matters (aka MapReduce for humans)
import com.cloudera.crunch._
import com.cloudera.scrunch._
class ScrunchWordCount {
def wordCount(inputFile: String, outputFile: String) = {
val pipeline = new Pipeline[ScrunchWordCount]
pipeline.read(from.textFile(inputFile))
.flatMap(_.toLowerCase.split("\\W+"))
.filter(!_.isEmpty())
.count
@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);
scala> trait TF {
| type ![A]
| }
defined trait TF
scala> type Curried2[F[_, _]] = TF {
| type ![X] = TF {
| type ![Y] = F[X, Y]
| }
| }