This file contains 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
// Akka HTTP and Streams notes: | |
// Working with akka.http.scaladsl.Http: | |
Http().bind( | |
interface: String, | |
port: Int | |
): Source[Http.IncomingConnection, Future[ServerBinding]] | |
//- creates an akka.stream.scaladsl.Source[Http.IncomingConnection, Future[ServerBinding]] | |
//- DOESN'T ACTUALLY START HANDLING REQUESTS. You have to compose the Source in a Flow to a Sink and RUN IT. |
This file contains 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
case class Player(name: String, deck: List[Int]) { | |
def play(n: Int): (Player, List[Int]) = (Player(name, deck.drop(n)), deck.take(n).reverse) | |
def add(pile: List[Int]) = Player(name, deck ++ pile) | |
} | |
trait GameState { val turn: Int } | |
case class Idle(p1: Player, p2: Player, turn: Int) extends GameState | |
case class AtWar(p1: (Player, List[Int]), p2: (Player, List[Int]), pile: List[Int], turn: Int) extends GameState | |
case class Done(winner: Option[Player], turn: Int) extends GameState |
This file contains 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 scala.annotation.tailrec | |
val primes: Stream[Long] = 2L #:: Stream.from(3, 2).map(_.toLong).filter(i => primes.takeWhile(j => j * j <= i).forall(k => i % k > 0)) | |
def primesLTEQ(n: Long) = primes.takeWhile(_ <= n).map(_.toLong).toList | |
def primeFactors(n: Long): List[Long] = { | |
@tailrec | |
def loop(ps: List[Long], divs: List[Long], rem: Long): List[Long] = (rem, ps) match { |
This file contains 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 scala.annotation.tailrec | |
def order(n: Int, m: Int): Option[Int] = { | |
@tailrec | |
def loop(acc: Int, iters: Int): Option[Int] = { | |
(acc % m) match { | |
case 0 => None | |
case 1 => Some(iters) | |
case _ => loop((acc * n) % m, iters + 1) | |
} | |
} |
This file contains 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 cats._ | |
import cats.data._ | |
import cats.std.all._ | |
import cats.syntax.apply._ | |
object ValidationExamples { | |
type WhyNot = List[String] |
This file contains 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 scala.language.implicitConversions | |
import scala.language.postfixOps | |
sealed trait Permutation[+A] | |
case object Id extends Permutation[Nothing] | |
case class Mapping[A](mappings: Map[A, A]) extends Permutation[A] | |
object Permutation { | |
def id = Id |
This file contains 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
// Let a "fragment" be a connected subgraph of a minimum spanning tree. | |
// To calculate a graph's entire minimum spanning tree: | |
// "while there are mutually nearest fragments, merge them" | |
// "merge": given fragments F and G: | |
// combine F and G's vertices | |
// combine F and G's mst edges, plus their shared external edge | |
// combine external edges, but filter out those which only connect F and G | |
// take max of levels if different, or add one if same |
This file contains 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 math._ | |
def airDistance(lat1: Double, lon1: Double, lat2: Double, lon2: Double) = { | |
val R = 3958.75 | |
val dLat = (lat2 - lat1).toRadians | |
val dLon = (lon2 - lon1).toRadians | |
val a = pow(sin(dLat/2),2) + pow(sin(dLon/2),2) * cos(lat1.toRadians) * cos(lat2.toRadians) | |
R * 2 * asin(sqrt(a)) | |
} |