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
makeTrade :: [(String, Maybe String)] -> Maybe Trade
-- using applicative
makeTrade alist =
Trade <$> lookup1 "account" alist
<*> lookup1 "instrument" alist
<*> (read <$> (lookup1 "market" alist))
<*> lookup1 "ref_no" alist
<*> (read <$> (lookup1 "unit_price" alist))
<*> (read <$> (lookup1 "quantity" alist))
@debasishg
debasishg / gist:703647
Created November 17, 2010 17:01
Examples of Applicative Functors
// Option as functor which gets pimped by map defined in MA.scala
// map takes a pure function & lifts it within the functor
Some(10) map {x: Int => x * 2} should equal(Some(20))
// List as functor : works the same way as Option as functor
List(10, 20, 30, 40) map {x: Int => x * 2} should equal(List(20, 40, 60, 80))
// List as applicative
List(10, 20, 30) <*> (List(1, 2, 3) map ((_: Int) * (_: Int)).curried) should equal(List(10, 20, 30, 20, 40, 60, 30, 60, 90))
scala> import scalaz._
import scalaz._
scala> import Scalaz._
import Scalaz._
scala> implicitly[Applicative[Option]]
res0: scalaz.Applicative[Option] = scalaz.Applicative$$anon$1@78b973
scala> val f = ((_: Int) * (_: Int)).curried
scala> import collection.mutable._
scala> val m = new HashMap[Int, Set[String]] with MultiMap[Int, String]
m: scala.collection.mutable.HashMap[Int,scala.collection.mutable.Set[String]] with scala.collection.mutable.MultiMap[Int,String] = Map()
scala> m += ((1, Set("rc", "tb")))
res10: m.type = Map((1,Set(tb, rc)))
scala> m += ((2, Set("tb")))
res11: m.type = Map((2,Set(tb)), (1,Set(tb, rc)))
// for comprehension
scala> for {
| l <- List(2,5,10)
| m <- List(8,10,11)
| } yield l * m
res17: List[Int] = List(16, 20, 22, 40, 50, 55, 80, 100, 110)
// map a pure function into applicatives
scala> List(8,10,11) <*> (List(2,5,10) map (((_: Int) * (_: Int)).curried))
res18: List[Int] = List(16, 20, 22, 40, 50, 55, 80, 100, 110)
scala> val l = List(1,2,3)
l: List[Int] = List(1, 2, 3)
// pattern matching
scala> l match {
| case s if s.forall(even(_)) => Some(l)
| case _ => None
| }
res37: Option[List[Int]] = None
scala> case class Foo(f1: String, f2: Int, f3: Int)
defined class Foo
scala> def validF2(f: Int): Validation[String, Int] = {
| if (f < 0) "f2 must be >= 0".fail
| else f.success
| }
validF2: (f: Int)scalaz.Validation[String,Int]
scala> def validF3(f: Int): Validation[String, Int] = {
sealed trait Validation[+E, +A] {
//..
}
final case class Success[E, A](a: A) extends Validation[E, A]
final case class Failure[E, A](e: E) extends Validation[E, A]
// validate trade quantity
def validQuantity(qty: BigDecimal): Validation[String, BigDecimal] =
try {
if (qty <= 0) "qty must be > 0".fail
else if (qty > 500) "qty must be <= 500".fail
else qty.success
} catch {
case e => e.toString.fail
}
scala> import scalaz._
import scalaz._
scala> import Scalaz._
import Scalaz._
scala> import net.debasishg.domain.trade.Trades._
import net.debasishg.domain.trade.Trades._
// a Map for trade attributes