Skip to content

Instantly share code, notes, and snippets.

View mads-hartmann's full-sized avatar

Mads Hartmann mads-hartmann

View GitHub Profile
@mads-hartmann
mads-hartmann / gist:3023885
Created June 30, 2012 14:10
Weird Mountain Lion / MongoDB issue

Trying to boot my Lift application it fails and mongod logs

Sat Jun 30 16:05:12 [conn4]  authenticate: { authenticate: 1, user: "mads", nonce: "...", key: "..." }
Sat Jun 30 16:05:12 [conn4] auth: couldn't find user mads, cataalog.system.users

Now, I have previously added a user to my cataalog database

> use cataalog
switched to db cataalog

> db.system.users.find()

concurrentMap3 :: Int -> (a -> b) -> [a] -> IO [b]
concurrentMap3 k f xs = do
mvars <- sequence $ replicate k newEmptyMVar
_ <- sequence $ map spark (ys `zip` mvars)
results <- sequence $ map takeMVar mvars
return $ concat $ results
where len = fromIntegral $ length $ xs -- Hope you're happy now GHC.
q = ceiling $ (len / (fromIntegral k))
ys = every q xs
spark = \tup -> forkIO $ do
@mads-hartmann
mads-hartmann / gist:2019152
Created March 12, 2012 01:23
monad transformers confusion
package com.sidewayscoding
import scalaz._
import Scalaz._
import scalaz.EitherT._
import scalaz.StateT._
import scala.collection.immutable.{ HashMap }
object PhonebookMonadTransformerApp extends App {
@mads-hartmann
mads-hartmann / ast-transformation.scala
Created February 17, 2012 19:43
AST Transformation
/*
* A PROBLEM.
*
* EXECUTIVE SUMMARY
*
* I have an AST. I want to map over it with _one_ function. The function
* should produce a valid AST i.e. the function should return an instance
* of the same class as the one it receives.
*
*/
@mads-hartmann
mads-hartmann / scala-class-definitions.scala
Created December 19, 2011 22:01
Scala class definition example to use when defining syntax hightlighting
class C
class C{}
class C(){}
class C(c: String, i: Integer)
class C(val c: String)
class C(private val c: String)
class C(private[this] val c: String)
class C(val c: Pair[A,List[B]], b: String)
class C[A]
private class C(private[this] val c: String)
@mads-hartmann
mads-hartmann / Fun-with-type-functions.scala
Created November 24, 2011 15:21
Simple Example from "Fun with type functions" in Scala
/*
Simple Example from "Fun with type functions"
class Add a b where
type SumTy a b
add :: a -> b -> SumTy a b
instance Add Integer Double where
type SumTy Integer Double = Double
@mads-hartmann
mads-hartmann / wish.scala
Created October 28, 2011 10:24
wish you could use extractors in arguments in scala
(for {
x <- 0 to 10
} yield (42 -> x)).foldLeft(HashMap[Int,HashSet[Int]]()) {
(acc,current) =>
val (y,z) = current
acc.updated(y, acc.getOrElse(y, HashSet[Int]()) + z)
}
// I wish the folding function could be defined like this
//
@mads-hartmann
mads-hartmann / SCC.scala
Created October 13, 2011 12:30
Strongly Connected Components algorithm implemented in Scala
/*
This is implemented following the instructions in "The Design and Analysis of
Computer Algorithms, AHO Hopcroft Ullman, 1974".
The implementation uses a DFS to find the strongly connected components (SCCs)
of a graph. During the DFS the vertices are placed on a stack in the order
they are visited. Whenever a root is found, all vertices of the corresponding
SSC are on the top of the stack and are popped.
@mads-hartmann
mads-hartmann / SCC-scalaz.scala
Created October 13, 2011 12:27
Strongly Connected Components algorithm implemented in Scala using ScalaZ
/*
This is implemented following the instructions in "The Design and Analysis of
Computer Algorithms, AHO Hopcroft Ullman, 1974".
The implementation uses a DFS to find the strongly connected components (SCCs)
of a graph. During the DFS the vertices are placed on a stack in the order
they are visited. Whenever a root is found, all vertices of the corresponding
SSC are on the top of the stack and are popped.
@mads-hartmann
mads-hartmann / scala.scala
Created September 23, 2011 08:17
More statements in the for-comprehension wanted
val op1 = Some("test")
val op2 = Some("test")
val l1 = List(1,2,3)
val l2 = List(1,2,3)
val product: Option[List[(Int,Int)]] = for {
t1 <- op1
t2 <- op2
} yield l1.flatMap { l => l2.map { l2 => (l,l2) }}