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.util.parsing.combinator.RegexParsers | |
sealed trait Command | |
case object FlushCommand extends Command | |
case class LookupKey(key: String) extends Command | |
case class SetKey(key: String, blob: String) extends Command | |
object Grammar extends RegexParsers { | |
val id: Parser[String] = """[a-zA-Z][a-zA-Z0-9]*""".r |
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
package sandbox; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.EnumSet; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Set; | |
import java.util.function.BiConsumer; | |
import java.util.function.BinaryOperator; |
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
scala> def yearlyImprove(dailyImprove: Double): Double = | |
| Seq.fill(365)(1 + dailyImprove).product - 1 | |
yearlyImprove: (dailyImprove: Double)Double | |
scala> yearlyImprove(0.001) | |
res0: Double = 0.44025131342952095 |
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
package lines | |
import java.io.File | |
import scala.annotation.tailrec | |
object LineCount { | |
def of(file: File): Int = { | |
val source = scala.io.Source.fromFile(file) | |
try of(source.toStream) |
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
package distro | |
import scala.annotation.tailrec | |
import scala.util.Random | |
trait Distro[A] { | |
def eventSet: Set[A] | |
def probOf(event: A): BigDecimal | |
def map[B](f: A => B): Distro[B] | |
def flatMap[B](f: A => Distro[B]): Distro[B] |
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
class Sieve(maxNumber: Int) { | |
private val candidates = Array.fill(maxNumber + 1)(true) | |
def run(): Unit = { | |
for (index <- 2 to maxNumber) { | |
if (candidates(index)) { | |
crossOutMultiples(index) | |
} | |
} | |
} |
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 akka.actor._ | |
object BackPressureSieve extends App { | |
val DefaultChunkSize = 1024 | |
val MinChunkSize = DefaultChunkSize * 3 / 4 | |
val MaxNumber = 500000 | |
case object GetCandidates | |
case class Candidates(values: Vector[Int]) |
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 akka.actor._ | |
object SimpleSieve extends App { | |
val MaxNumber = 500000 | |
case object NoMoreCandidates | |
class Filter extends Actor { |
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
package bowling | |
class Game { | |
trait Line { | |
def addRoll(pins: Int): Unit | |
def score: Int | |
} | |
class Frame(next: Line) extends Line { |
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 scalaz.syntax.std.boolean._ | |
boolExpression.option(computingSomethingComplicated) |