Skip to content

Instantly share code, notes, and snippets.

// 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.
@jayhutfles
jayhutfles / war.scala
Last active January 30, 2021 19:44
war
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
@jayhutfles
jayhutfles / primes.scala
Last active May 19, 2016 21:54
Recursive lazy impl of the Sieve of Eratosthenes, and using that to factor an arbitrary Int
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 {
@jayhutfles
jayhutfles / MultiplicativeOrder.scala
Last active April 15, 2016 19:07
Finding the order of integer n in the multiplicative group of integers mod m
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)
}
}
@jayhutfles
jayhutfles / ValidationExamples.scala
Last active November 20, 2015 15:45
Differences between Xor and Validated in Cats
import cats._
import cats.data._
import cats.std.all._
import cats.syntax.apply._
object ValidationExamples {
type WhyNot = List[String]
@jayhutfles
jayhutfles / gist:ac60fd87e686dd8f82a3
Last active January 1, 2024 22:08
Scala implementation of a finite permutation group
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
@jayhutfles
jayhutfles / MinSpanningTree
Last active April 13, 2016 18:22
GHS MinSpanningTree algorithm. Not as clean as Prim's or Kruskal's. But it scales, dood.
// 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
@jayhutfles
jayhutfles / airDistance
Last active August 29, 2015 14:08
Air distance function in Scala
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))
}