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
@debasishg
debasishg / gist:1143727
Created August 13, 2011 10:37
bite sized code gives 100X performance on a huge computation set ..
def onTrades[T](trades: List[Trade])(fn: Trade => T) =
Future.traverse(trades) {trade =>
Future { fn(trade) }
}
@debasishg
debasishg / gist:1168338
Created August 24, 2011 15:41
Using rank-2 types to eliminate constructor calls (Runar's code from http://paste.pocoo.org/show/463277/)
import scalaz._
import Scalaz._
type G[A] = Forall[({type f[B] = ((A, B) => B, B) => B})#f]
val cons: Forall[({type f[A] = (A, Stream[A]) => Stream[A]})#f] =
new Forall[({type f[A] = (A, Stream[A]) => Stream[A]})#f] {
def apply[A] = (x, xs) => { println("called cons"); x #:: xs }
}
@debasishg
debasishg / gist:1168340
Created August 24, 2011 15:42
Encoding of List using rank-3 types (Runar's code from http://paste.pocoo.org/show/463632/)
import scalaz._
import Scalaz._
type List[A] = Forall[({type f[B] = ((A, B) => B, B) => B})#f]
val nil: Forall[List] = new Forall[List] {
def apply[A] = new List[A] {
def apply[B] = (c, n) => n
}
}
@debasishg
debasishg / gist:1235335
Created September 22, 2011 16:57
domain model versioning in CQRS
http://twitter.com/#!/dgiffone/status/116857796024213504
http://twitter.com/#!/zeit_geist/status/116847468192333825
http://twitter.com/#!/aloyer/status/116858699934470144
http://twitter.com/#!/mich_ham/status/116878614460514305
http://twitter.com/#!/graven/status/116890930707050496
@debasishg
debasishg / gist:1261857
Created October 4, 2011 14:58
a brief rant about |@| in scalaz
/**
* |@| is a helper function that helps you accumulate applicative functors. It gives you an ApplicativeBuilder (it's part of
* the implementation though) through which you accumulate your applicatives just as you would using the Builder pattern of
* the GoF in the OO world. Once you have all the applicatives you can pass it a function that will be applied to all the
* values that you have accumulated so far. e.g.
*/
scala> (1.some |@| 2.some) apply {_ + _}
res1: Option[Int] = Some(3)
@debasishg
debasishg / gist:1682366
Created January 26, 2012 11:34
some typeclass love for sjson
val m1 = Map(1 -> Map(1 -> "a", 2 -> "b", 3 -> "c"))
val m2 = Map(1 -> Map(2 -> "x"), 2 -> Map(3 -> "d", 5 -> "e"))
tojson(m1) |+| tojson(m2) should equal(tojson(m1 |+| m2))
@debasishg
debasishg / gist:1787464
Created February 10, 2012 07:41
New Cake pattern (from @etorreborre)
trait Cake {
type Config
def config: Config
}
trait FooConfig {
// ...
}
trait FooComponent extends Cake {
@debasishg
debasishg / gist:3555396
Created August 31, 2012 16:25
State monad in scalaz
scala> state[Int, Int](10)
res9: scalaz.State[Int,Int] = scalaz.package$State$$anon$1@3bbe242c
scala> res9.run(0)
res10: (Int, Int) = (0,10)
scala> res9 map (_ * 2)
res11: scalaz.StateT[scalaz.Id.Id,Int,Int] = scalaz.StateT$$anon$7@31a9253
scala> res11.run(0)
@debasishg
debasishg / gist:3668131
Created September 7, 2012 17:48
Changes in scalaz-core_2.10.0-M7 ver 7.0.0.M3 ?
Welcome to Scala version 2.10.0-M7 (Java HotSpot(TM) Client VM, Java 1.7.0_01).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import scalaz._
import scalaz._
scala> import Scalaz._
import Scalaz._
@debasishg
debasishg / gist:3735459
Created September 17, 2012 03:52
trying to use Either in place of Validation
def validAge(age: Int): \/[String, Int] = //..
def validName(name: String): \/[String, String] = //..
(validAge(10).validation.toValidationNEL |@| validName("xxx").validation.toValidationNEL) { (a, n) => //.. }
/**
I am using the above snippet to accumulate errors using scalaz.Either and Validation. Is there any way to do the above without using Validation, just using Either. I don't like the conversion to Validation which I have to do just to lift the errors into a List for accumulation.
**/