Skip to content

Instantly share code, notes, and snippets.

View ShahOdin's full-sized avatar
🎋

Shah Saraei ShahOdin

🎋
View GitHub Profile
/*
* In a service-based architecture, often multiple components have common dependencies. However, each is usually interested in
* a fraction of the provided functionality in a dependency. As such, when testing a dependency, the components tend to mock the individual
* functionalities in place (in their spec file). This means that one usually ends up with a lot of mock duplication in spec files.
* This is bad because these are hard to maintain. If a service's API were to change, the spec files mocking a functionality could
* fail and one has to go through the tests on a one by one basis and update them individually. Moreover, this duplication means
* that a high level component, needs to potentially combine the provided mocks if one of its dependencies shared a dependency with itself.
* Below is a demonstration of a suggested way service components (http-services, actors, etc.) should mock their dependencies.
* They need to define a MockAPI which is a list of components they need, in order to pr
@ShahOdin
ShahOdin / dependencyInjection.sc
Last active October 6, 2017 08:49
a demonstration of the problems with dependency injection in OO and how these problems can be avoided with trait mix-ins in Scala.
// Hidden dependency makes inheriting from implementation dangerous.
object BadObjectOrientation {
trait Interface {
protected def resourceA: Int
protected def resourceB: String
//only supposed to use A
sealed trait Bool
sealed trait True extends Bool
sealed trait False extends Bool
//////////////////////////////////////////////////
sealed trait Comparison {
type Match[IfLT <: Up, IfEQ <: Up, IfGT <: Up, Up] <: Up
type gt = Match[False, False, True, Bool]
sealed trait NaturalNumber
sealed trait _0 extends NaturalNumber
sealed trait _1 extends _0
sealed trait _2 extends _1
sealed trait _3 extends _2
sealed trait _4 extends _3
sealed trait _5 extends _4
sealed trait _6 extends _5
sealed trait _7 extends _6
sealed trait _8 extends _7
object FSM {
sealed trait State
object State {
sealed trait State1 extends State
sealed trait State2 extends State
sealed trait State3 extends State
}
trait A
trait B
trait C
trait D
class E(a:A,b:B,c:C,d:D)
////////////////////////////////////////////////////////
//(I)function declaration.
////////////////////////////////////////////////////////
@ShahOdin
ShahOdin / methodToFinagleServiceConversion.scala
Last active June 28, 2018 10:35
implicit conversion of methods to Finagle Services
import com.twitter.util.Future
import com.twitter.finagle.Service
//the method that we want to pass around
def foo(x:Int): Future[String] = Future.value(x.toString)
//the construct that depends on Services
def bar(service: Service[Int,String]) = service
@ShahOdin
ShahOdin / Par.scala
Created September 21, 2018 09:10 — forked from orium/Par.scala
Par
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, ExecutionContext, Future}
object ParImpl {
sealed trait Par[A] {
def toFuture(implicit ec: ExecutionContext): Future[A] = {
this match {
case Par.Unit(f) => Future(f())
case x:Par.Map2[A,_,_] =>
for {
object CirceDecoderCombination {
sealed trait Base
case class StringField(field1: String) extends Base
case class IntField(field2: Int) extends Base
case class FloatField(field3: Float) extends Base
import io.circe.{Decoder, Encoder}
import io.circe.generic.auto._
@ShahOdin
ShahOdin / dependencyInjection.scala
Last active September 27, 2018 13:52
dependency injection by passing parameterised functions
trait Pen
type Drawing = Unit
trait Side
case object Right extends Side
case object Left extends Side
object Drawing {
//pens everywhere, distracting us from the main drawing logic