Skip to content

Instantly share code, notes, and snippets.

import scala.language.higherKinds
trait SomeLike[+A] {
def value: A
}
trait OptionSig {
type Option[+_]
type Some[+A] <: Option[A] with SomeLike[A]
type None <: Option[Nothing]
def combos(numDice: Int, sides: Int): List[List[Int]] = {
if (numDice > 0) {
for {
h <- (1 to sides).toList
t <- combos(numDice - 1, sides)
} yield {
h :: t
}
} else {
List(Nil)
package algebra.benchmark
import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.Param
import org.openjdk.jmh.annotations.Scope
import org.openjdk.jmh.annotations.Setup
import org.openjdk.jmh.annotations.State
import org.openjdk.jmh.annotations.{ Fork, Warmup, Measurement }
@Warmup(iterations = 5)
sealed trait Point2[@mb A] {
def x: A
def y: A
...
}
case class IntPoint2(x: Int, y: Int) extends Point2[Int]
case class DoublePoint2(x: Double, y: Double) extends Point2[Double]
case class GenPoint2[A](x: A, y: A) extends Point2[A]
--langdef=scala
--langmap=scala:.scala
--regex-scala=/^[ \t]*((abstract|final|sealed|implicit|lazy)[ \t]*)*((private|protected)[ \t]*(\[[^\]]+\])?)?[ \t]*((abstract|final|sealed|implicit|lazy)[ \t]*)*class[ \t]+([a-zA-Z0-9_]+)/\8/c,classes/
--regex-scala=/^[ \t]*((abstract|final|sealed|implicit|lazy)[ \t]*)*((private|protected)[ \t]*(\[[^\]]+\])?)?[ \t]*((abstract|final|sealed|implicit|lazy)[ \t]*)*case class[ \t]+([a-zA-Z0-9_]+)/\8/c,case classes/
--regex-scala=/^[ \t]*((abstract|final|sealed|implicit|lazy)[ \t]*)*((private|protected)[ \t]*(\[[^\]]+\])?)?[ \t]*((abstract|final|sealed|implicit|lazy)[ \t]*)*trait[ \t]+([a-zA-Z0-9_]+)/\8/t,traits/
--regex-scala=/^[ \t]*((abstract|final|sealed|implicit|lazy)[ \t]*)*((private|protected)[ \t]*(\[[^\]]+\])?)?[ \t]*((abstract|final|sealed|implicit|lazy)[ \t]*)*object[ \t]+([a-zA-Z0-9_]+)/\8/c,objects/
--regex-scala=/^[ \t]*((abstract|final|sealed|implicit|lazy)[ \t]*)*((private|protected)[ \t]*(\[[^\]]+\])?)?[ \t]*((abstract|final|sealed|implicit|lazy)[ \t]*)*case o
resolvers += "Pellucid Bintray" at "http://dl.bintray.com/pellucid/maven"
libraryDependencies += "com.pellucid" %% "framian" % "0.3.3"
package example
import argonaut._
import Argonaut._
sealed trait WidgetType
object WidgetType {
case object Sprocket extends WidgetType
case object Cog extends WidgetType
case object Doowazzle extends WidgetType
// This prints the top 10 alternative bases, ordered by the (# of factors, excluding 1 & base) / base.
// So, the last number printed has the highest number of factors relative to the size of the base.
//
// Turns out that 12 is a great base!
object Main extends App {
// 7560 is the smallest number that is divisible by all number between 1 and 10.
val facMap = (1 to 7560).foldLeft(Map.empty[List[Int], Int]) { (acc, n) =>
val factors = (2 to (n / 2)).filter(n % _ == 0).toList
if (acc contains factors) acc
else acc + (factors -> n)
/**
* Something similar to Kleisli[Future, A, B], but doesn't require an
* ExecutionContext up front.
*/
case class AsyncFn[A, B](fn: ExecutionContext => A => Future[B]) {
def map[B0](f: B => B0): AsyncFn[A, B0] =
AsyncFn[A, B0] { implicit ec => a =>
fn(ec)(a).map(f)
}
{
a = "abc"
xyz {
x = 1
y = "xyz"
z = 2.3
}
}