This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Arrays; | |
| import java.util.Collection; | |
| import java.util.List; | |
| public class Main { | |
| public interface Cat { | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 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] { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| trait DataReader { | |
| def readName(): String | |
| } | |
| trait DummyDataReader extends DataReader { | |
| def readName(): String = "bEllA" | |
| } | |
| class GreeterService { | |
| self: DataReader => |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sealed trait InMyPocket[-A] | |
| sealed trait Cuddler | |
| final case class Puppy(name: String) extends Cuddler | |
| object VarianceExample { | |
| var dog: InMyPocket[Puppy] = new InMyPocket[Puppy] {} | |
| var cuddler: InMyPocket[Cuddler] = new InMyPocket[Cuddler] {} |