Skip to content

Instantly share code, notes, and snippets.

/*
This doesnt compile because foo is expecting a type
class which takes one type parameter but Function1
takes two
*/
object DoesntCompile {
def foo[F[_], A](fa: F[A]): String = fa.toString
foo { x: Int => x * 2 }
trait DataReader {
def readName(): String
}
trait DummyDataReader extends DataReader {
def readName(): String = "bEllA"
}
class GreeterService {
self: DataReader =>
/*
For naming thread pools (which I like to do so I can search my thread dumps quickly but you really don't have to)
libraryDependencies += "com.google.guava" % "guava" % "12.0"
*/
object Par {
type Par[A] = ExecutorService => Future[A]
private case class UnitFuture[A](get: A) extends Future[A] {
object QuickSort {
def quickSort(arr: Array[Int]): Array[Int] = {
val copy = arr.clone()
go(copy, 0, copy.length - 1)
copy
}
private def go(a: Array[Int], l: Int, r: Int): Unit = {
if (l < r) {
object Rawr extends App {
val namedThreadFactory: ThreadFactory = new ThreadFactoryBuilder()
.setNameFormat("meow-%d")
.build()
val es: ScheduledExecutorService = Executors.newScheduledThreadPool(1, namedThreadFactory)
def cat() = new Runnable {
override def run(): Unit = {
println(Thread.currentThread().getName)
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;
public class Main {
private static final AtomicInteger COUNTER = new AtomicInteger();
private static final int THRESHOLD = 5;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class Main {
public interface Cat {
}
object Main extends App {
/*
Covariant: F[B] is a subtype of F[A] if B is a subtype of A
*/
trait Shape {}
case class Circle(radius: Double) extends Shape
object Main extends App {
/*
Contravariant: F[B] is a subtype of F[A] if A is a subtype of B
*/
trait Shape {}
case class Circle(radius: Double) extends Shape
// eg union of non empty sets
trait MySemigroup[A] {
// must be associative
def combine(a1: A, a2: A): A
}
// eg union of sets, where the empty set is the identity element
trait MyMonoid[A] extends MySemigroup[A] {
// is an identity element
def empty: A