Skip to content

Instantly share code, notes, and snippets.

@scalolli
scalolli / Constant.hs
Created May 2, 2018 05:22
Queries on IRC on how Constant applicative works in Haskell
type C = Constant
(<*>) :: f (a->b)-> fa-> fb
(<*>) :: C e (a -> b) -> C e a -> C e b
pure::a-> fa
pure :: a -> C e a
Prelude> let f = Constant (Sum 1)
Prelude> let g = Constant (Sum 2)
Prelude> f <*> g
try ( Transaction tx = graphDb.beginTx() )
{
// Database operations go here
tx.success();
}
class AuthenticatedRequest[A](val ids: List[String], request: Request[A]) extends WrappedRequest[A](request)
object AuthenticationAction extends
ActionBuilder[AuthenticatedRequest] with ActionTransformer[Request, AuthenticatedRequest] {
def transform[A](request: Request[A]) = Future.successful {
new AuthenticatedRequest(List("1", "2"), request)
}
}
def AutorizationAction(itemId: String) = new ActionTransformer[Request, AuthenticatedRequest] {
@scalolli
scalolli / FunWithTypes.scala
Created June 27, 2013 18:24
Trying to better understand Scala's type system
object FunWithTypes {
def main(args: Array[String]) {
class Animal
class Goat extends Animal
val left: List[Animal] = List(new Animal)
val right: ElementMatcher[Goat] = contain(new Goat)
left should right
@scalolli
scalolli / Subtyping.scala
Created April 6, 2013 09:55
Strucutural sub-typing, I am not sure if we should be allowed to add Shark into the list of animals where I am refining that the food type be of type Grass
object RevisedAbstracts {
def main(args: Array[String]) {
val a = new Cow
a.eat(new Grass)
val pasture = new Pasture
val l = (new Shark) :: pasture.animals
val animal = l(0).asInstanceOf[Cow]
animal.eat(new Grass)
}
@scalolli
scalolli / Jorney.scala
Created March 30, 2013 06:29
Journey from Java =>Scala=>Linux=>Haskell via Monad !!
println(Some(Dev(Nil)) flatMap {
x => haskell(x) flatMap {
(y => linux(y) flatMap {
(z => scala(z) map {
java(_)
})
})
}
})
@scalolli
scalolli / Monads.scala
Created March 24, 2013 09:34
Monad rules using the List Monad using James Iry's blog on Monads are Elephants http://james-iry.blogspot.in/2007/10/monads-are-elephants-part-3.html
val l = List(1,2,3)
//The Functor/Monad Connection Law: The Zeroth Law
p(l map (_*2))
p(l flatMap(x => List(x*2)))
//Flatten revisited
val identity = (x:Int) => List(x)
p(s"Flatmap: ${l.flatMap(identity)}")
p(s"Flattening: ${l.map(identity).flatten}")
@scalolli
scalolli / Contravariance.scala
Created March 12, 2013 18:41
Why function parameters are contravariant?
def main(args: Array[String]) {
def getTitle(p: Publication): String = p.title
def getTitle2(p:Magazine) : String = p.title
printBookList(getTitle) //works because getTitle is a sub type
printBookList(getTitle2) //I know function parameters are contravariant but what will happen if this were to work?
}
class Publication(val title: String)
class Book(title: String) extends Publication(title)
@scalolli
scalolli / Folding.scala
Last active December 14, 2015 17:39
Reverse using foldLeft and foldRight and finally found a good use case to write a partially applied function :)
val p = println(_ : Any)
p((List[Int]() /: List(1,2,3))((x,y) => y::x))
p((List(1,2,3) :\ List[Int]())((x,y)=> y:::List(x)))
@scalolli
scalolli / Traits.scala
Created March 3, 2013 16:45
Traits with abstract override and an abstract filter now that adds more flavor to the example :)
trait Filtering extends IntQueue {
def filter(x:Int) : Boolean
abstract override def put(x:Int) {if (filter(x)) super.put(x)}
}
val filteringQueue = new BasicIntQueue with Filtering {
def filter(x: Int): Boolean = x > 0
}
filteringQueue.put(-3)
filteringQueue.put(2)