Skip to content

Instantly share code, notes, and snippets.

@lazyval
lazyval / Example.scala
Created November 26, 2019 13:22
Scalacheck example for trivial scalatest violation
package com.spotify
import org.scalatest.prop.GeneratorDrivenPropertyChecks
import org.scalatest.{FlatSpec, Matchers}
class ExampleTest extends FlatSpec with Matchers with GeneratorDrivenPropertyChecks {
implicit override val generatorDrivenConfig =
PropertyCheckConfig(minSize = 100, maxSize = 200)
"strings" should "have same length when lowercased / uppercased" in {
@lazyval
lazyval / values.scala
Created November 25, 2019 18:44
Value classes example
class Meter(val value: Double) extends AnyVal {
def toFeet = new Feet(value * 3.281)
}
class Feet(val value: Double) extends AnyVal {
def toMeter = new Meter(value / 3.281)
}
object Example {
def convert(meter: Meter): Feet = {
meter.toFeet
@lazyval
lazyval / Example.scala
Created November 27, 2018 15:37
RecordMatcher failure example
import java.math.MathContext
import shapeless.datatype.record._
object Example extends App {
case class BigBoy(x: BigDecimal)
implicit def compareBigDecimals(x: BigDecimal, y: BigDecimal): Boolean = {
println("custom bigdecimal comparison invoked")
x.compareTo(y) == 0
@lazyval
lazyval / HelloWorld.scala
Created July 19, 2017 08:05
hello scala!
object Hello extends App {
println("hello scala!")
}
@lazyval
lazyval / Emitter.java
Created June 23, 2016 08:42
Map Reduce 101
/**
* Collects results of map phase, persists it to temporary storage
* results with same key (globally) can be accessed together
*/
interface Emitter<M> {
void emit(String key, M result);
}
import kafka.api.FetchRequestBuilder
import kafka.cluster.Broker
import kafka.javaapi.{FetchRequest, TopicMetadataRequest}
import kafka.javaapi.consumer.SimpleConsumer
import kafka.message.Message
import scala.collection.JavaConverters._
class Reader(anyBroker: String, topic: String, partition: Int, offset: Int) {
val ClientId = "kafka-fetch-size-client"
class Bar<B> {}
@lazyval
lazyval / pid.py
Last active August 29, 2015 14:20
PID controller made in python, see also http://www.csimn.com/CSI_pages/PIDforDummies.html
kp = 1.2
ki = 0.9
kd = 0.3
set_point = 50
prev_error = 0
cumulative_moving_average = 0
iteration = 0
def make_iteration(measured):
@lazyval
lazyval / count_files.bash
Created September 2, 2014 13:44
Utility that can be used to count files in a given directory
#!/bin/bash
countFiles () {
# call the recursive function, throw away stdout and send stderr to stdout
# then sort numerically
countFiles_rec "$1" 2>&1 >/dev/null | sort -nr
}
countFiles_rec () {
local -i nfiles
@lazyval
lazyval / PaintingFence.scala
Created July 22, 2014 20:37
optimization of fence painting
import scala.util.control.TailCalls._
def solve(l: List[(Int, Int)]): Int = {
def go(from: Int, to: Int, prevHeight: Int): TailRec[Int] = {
val max = to - from
val currHeight = l.slice(from, to).minBy(_._1)._1
val hStrokes = currHeight - prevHeight
// not sure it's legal, but in practice it seems that you always have only one item here
val List(split) = l.slice(from, to).filter(_._1 - currHeight == 0).map(_._2)
val indices: List[Int] = from :: split :: split + 1 :: to :: Nil