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:761970
Created January 1, 2011 20:12
Lens can be generated from case classes through compiler plugins ..
scala> import scalaz._
import scalaz._
scala> import Scalaz._
import Scalaz._
scala> case class Address(no: Int, street: String, zip: String)
defined class Address
// this can be generated
@debasishg
debasishg / gist:762328
Created January 2, 2011 05:47
loop and label implementation of the Essence of the Iterator Pattern
import scalaz._
import Scalaz._
val l = List(10, 20, 30, 40)
// loop from The Essence of the Iterator Pattern
// accumulates elements effectfully, but modifies elements purely and independently of accumulation
def loop[T[_]:Traverse, A, B](f: A => B, t: T[A]) =
t.traverse[({type λ[x] = State[Int,x]})#λ, (B, Int)](a =>
@debasishg
debasishg / gist:762736
Created January 2, 2011 18:56
wordcount example in scalaz using State monad
import scalaz._
import Scalaz._
def charLineCount[T[_]:Traverse](t: T[Char]) =
t.traverse[({type λ[x] = State[(Int, Int),x]})#λ, (Int, Int)](a =>
state((counts: (Int, Int)) =>
((counts._1 + 1, counts._2 + (if (a == '\n') 1 else 0)), (counts._1, counts._2)))) ! (1,1)
println(charLineCount("the cat in the hat\n sat on the mat\n".toList).last) // (35, 2)
// disperse implementation corresponding to the same function in section 4.2 of the paper
// Essence of the Iterator Pattern
def disperse[T[_]:Traverse, A, S, B](t: T[A], s: A => State[S, B]) =
t.traverse[({type λ[x] = State[S,x]})#λ, B](s)
// implementing a labeling function, also from section 4.2
// labeling every element with its position in order of traversal
def label_[T[_]:Traverse, A](t: T[A]) = disperse(t, ((a: A) => state((i: Int) => (i+1, i)))) ! 0
println(label_(List(102, 207, 876, 14)) // List(0,1,2,3)
@debasishg
debasishg / gist:769776
Created January 7, 2011 17:25
Trade generation pipeline from client orders
// client orders executed at market by broker & allocated to client accounts
kleisli(clientOrders) >=> kleisli(execute(market)(broker)) >=> kleisli(allocate(clientAccounts))
package akka.persistence.redis
import org.scalatest.junit.JUnitSuite
import org.junit.{Test, Before}
import org.junit.Assert._
import akka.actor.{Actor, ActorRef}
import akka.actor.Actor._
import akka.stm._
// works for Validation
scala> val a: Validation[String, Int] = 1.success
a: scalaz.Validation[String,Int] = Success(1)
scala> val b: Validation[String, Int] = 2.success
b: scalaz.Validation[String,Int] = Success(2)
scala> val c: Validation[String, Int] = 3.success
c: scalaz.Validation[String,Int] = Success(3)
scala> val d: Validation[String, Int] = 12.success
d: scalaz.Validation[String,Int] = Success(12)
scala> val e: Validation[String, String] = "ghosh".success
e: scalaz.Validation[String,String] = Success(ghosh)
scala> (d, e)
res18: (scalaz.Validation[String,Int], scalaz.Validation[String,String]) = (Success(12),Success(ghosh))
// I want to get Validation[String, (Int, Int)] out of this. The closest I could get is ..
@debasishg
debasishg / gist:818421
Created February 9, 2011 12:51
sjson gets some applicative & monad love
val jsonString =
"""{
"lastName" : "ghosh",
"firstName" : "debasish",
"age" : 20,
"address" : { "no" : 12, "street" : "Monroe Street", "city" : "Denver", "zip" : "80231" },
"phone" : { "no" : "3032144567", "ext" : 212 },
"office" :
{
"name" : "anshinsoft",
@debasishg
debasishg / gist:846226
Created February 27, 2011 14:37
Justin's example refactored for currying ..
// like to keep domain objects pure
// hence moved repository injection to the service module
case class User(name: String)
trait UserRepository{
def save(user: User): Unit
}
class RiakUserRepository extends UserRepository{