Skip to content

Instantly share code, notes, and snippets.

@dorsev
dorsev / gist:2101f40a5fd29fa284598242db737a56
Last active September 1, 2018 08:03
Person class with toJson method
public class Person {
public String toJson() {
return "{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", nicknames=" + Arrays.toString(nicknames) +
'}';
}
@dorsev
dorsev / ConfusionMatrixInterface.java
Last active September 19, 2018 21:57
Confusion matrix trait and implementation with some sample app written using java 10 features
interface ConfusionMatrixInterface {
Stream<String> getClassNames();
int truePositive(String className);
int trueNegative(String className);
int falsePositive(String className);
@dorsev
dorsev / IslandState.scala
Last active September 19, 2018 22:16
Wolf sheep cabbage riddle solver in scala using backtracking
case class IslandState(destShore: ShoreSide, initialShore: ShoreSide) {
override def toString: String = s"dest:$destShore,init:$initialShore"
import IslandState._
val isGameOver: Boolean = isFinalStateReached(destShore)
def getTheOtherShore(ss: ShoreSide): ShoreSide = if (destShore == ss) initialShore else destShore
@dorsev
dorsev / gist:b35957d3d9d65b0ca7a7b1e70c46c6ea
Created November 3, 2018 21:53
Eta Expansion in scala - p1
scala>val emphasisFunc = (str:String) = s"$str!!!""
emphasisFunc: (str: String)String
scala>emphasisFunc
def emphasisFunc(str: String): String
scala> emphasisFunc("abc")
res0: String = abc!!!
scala> List("hello","world").map(emphasisFunc)
@dorsev
dorsev / gist:c0f4e372d4d209473b3f6e538e1a4a7e
Created November 3, 2018 21:59
Eta Expansion in scala - p2
scala> val etaexplained = emphasisFunc _
etaexplained: String => String = $$Lambda$1239/712726821@5a5b394
//what happens on multiple paramters list ?
//they get curried away.
//for example
scala> def strAddByNumAndMultiplty(str:String)(numToAdd:Int)(numToMultiply:Int) = str.toInt + numToAdd * numToMultiply
strAddByNumAndMultiplty: (str: String)(numToAdd: Int)(numToMultiply: Int)Int
def offsetMonitor[F[_]: Functor, E, H[_]: Functor, T](
monitor: H[{def set(offset:Long):Unit}]
): Flow[EnvT[E, F, ProducerResult[T]], EnvT[E, F, ProducerResult[T]], NotUsed] =
Flow[EnvT[E, F, ProducerResult[T]]].map { elem =>
elem.map { e =>
monitor.map { m =>
m.set(e.env.offset)
}
}
trait Trackable {
def setOffset(value: Long): Unit
}
trait Gauge {
def set(off: Long): Unit
}
val gauge: Gauge = ???
val counter: AtomicLong = ???
trait Trackable[T] {
def trackValue(off: Long, item: T): Unit
}
trait Gauge {
def set(off: Long): Unit
}
trait Gauge {
def set(off: Long): Unit
}
val gauge: Gauge = ???
val counter: AtomicLong = ???
def offsetMonitor[A](monitor: Long=>Unit) =
monitor(12)
def offsetMonitor[F[_]: Functor, E, H[_]: Functor, T](
monitor: H[AtomicLong]
): Flow[EnvT[E, F, ProducerResult[T]], EnvT[E, F, ProducerResult[T]], NotUsed] =
Flow[EnvT[E, F, ProducerResult[T]]].map { elem =>
elem.map { e =>
monitor.map { m =>
m.set(e.env.offset)
}
}