This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 => |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// client orders executed at market by broker & allocated to client accounts | |
kleisli(clientOrders) >=> kleisli(execute(market)(broker)) >=> kleisli(allocate(clientAccounts)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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._ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 .. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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{ |