Skip to content

Instantly share code, notes, and snippets.

View kpmeen's full-sized avatar
👾

Knut Petter Meen kpmeen

👾
View GitHub Profile
@kpmeen
kpmeen / seeds.md
Created April 24, 2018 05:54 — forked from non/seeds.md
Simple example of using seeds with ScalaCheck for deterministic property-based testing.

introduction

ScalaCheck 1.14.0 was just released with support for deterministic testing using seeds. Some folks have asked for examples, so I wanted to produce a Gist to help people use this feature.

simple example

These examples will assume the following imports:

@kpmeen
kpmeen / scala path dependent serializers.scala
Created April 9, 2018 05:59 — forked from johnynek/scala path dependent serializers.scala
Scala's path dependent types can be used to prove that a serialized value can be deserialized without having to resort to Try/Either/Option. This puts the serialized value into the type, so we can be sure we won't fail. This is very useful for distributed compute settings such as scalding or spark.
import scala.util.Try
object PathSerializer {
trait SerDe[A] {
// By using a path dependent type, we can be sure can deserialize without wrapping in Try
type Serialized
def ser(a: A): Serialized
def deser(s: Serialized): A
// If we convert to a generic type, in this case String, we forget if we can really deserialize
@kpmeen
kpmeen / restricted-psp.yaml
Created November 23, 2017 10:31 — forked from tallclair/restricted-psp.yaml
Restricted PodSecurityPolicy
apiVersion: extensions/v1beta1
kind: PodSecurityPolicy
metadata:
name: restricted
annotations:
seccomp.security.alpha.kubernetes.io/allowedProfileNames: 'docker/default'
apparmor.security.beta.kubernetes.io/allowedProfileNames: 'runtime/default'
seccomp.security.alpha.kubernetes.io/defaultProfileName: 'docker/default'
apparmor.security.beta.kubernetes.io/defaultProfileName: 'runtime/default'
spec:
@kpmeen
kpmeen / server.scala
Created November 19, 2017 08:03 — forked from huntc/server.scala
A complete server using Akka streams that reads some source, batches its data and then publishes. If the data cannot be published then it backs off with a best-effort of sending that data again.
val (recycleQueue, recycleSource) =
Source
.queue[SoilStateReading](100, OverflowStrategy.dropTail)
.prefixAndTail(0)
.map(_._2)
.toMat(Sink.head)(Keep.both)
.run()
StreamConverters.fromInputStream(() => this.getClass.getClassLoader.getResourceAsStream("sensors.log"))
.via(SoilStateReading.csvParser)
.merge(Source.fromFutureSource(recycleSource))
@kpmeen
kpmeen / Test.scala
Created October 31, 2017 06:29 — forked from lihaoyi/Test.scala
Resolving jars with Coursier and compiling Scala with the Zinc incremental compiler, in a self-contained Ammonite script
import play.api.libs.json.Json
object Main{
def main(args: Array[String]): Unit = {
println(
Json.prettyPrint(
Json.toJson(Seq("Hello", "World", "Cow"))
)
)
}
@kpmeen
kpmeen / diff-viz.scala
Created October 16, 2017 21:19 — forked from mandubian/diff-viz.scala
Visualizing scala code diff in scala code (result is compiling Scala code with diffs annotated in comments)
// Build Scala source code
val tree = q"""
object Test {
val a = 2 + 5
def f(b: String) = { b + a.toString }
}
"""
val src = List(TreeNode(tree))
// Build Scala target code
@kpmeen
kpmeen / latency_numbers.md
Created October 9, 2017 21:44 — forked from GLMeece/latency_numbers.md
Latency Numbers Every Programmer Should Know - MarkDown Fork

Latency Comparison Numbers

Note: "Forked" from Latency Numbers Every Programmer Should Know

Event Nanoseconds Microseconds Milliseconds Comparison
L1 cache reference 0.5 - - -
Branch mispredict 5.0 - - -
L2 cache reference 7.0 - - 14x L1 cache
Mutex lock/unlock 25.0 - - -

How to GPG as a Scala OSS Maintainer

tl;dr Generate a GPG key pair (exercising appropriate paranoia). Send it to key servers. Create a Keybase account with the public part of that key. Use your keypair to sign git tags and SBT artifacts.

GPG is probably one of the least understood day-to-day pieces of software in the modern developer's toolshed. It's certainly the least understood of the important pieces of software (literally no one cares that you can't remember grep's regex variant), and this is a testament to the mightily terrible user interface it exposes to its otherwise extremely simple functionality. It's almost like cryptographers think that part of the security comes from the fact that bad guys can't figure it out any more than the good guys can.

Anyway, GPG is important for open source in particular because of one specific feature of public/private key cryptography: signing. Any published software should be signed by the developer (or company) who published it. Ideally, consu

#!/bin/bash
if [[ $# -ne 1 ]]; then
echo "usage: $0 doc.md"
echo
echo 'Output will be printed to stdout, so you probably want to pipe to'
echo 'pbcopy or to a file or something.'
exit 1
fi
@kpmeen
kpmeen / copy.scala
Created December 7, 2016 07:01 — forked from Baccata/copy.scala
import shapeless._
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// THE IMPLICIT CLASS THAT ALLOWS TO CALL #copy ON ANY SEALED TRAIT INSTANCE
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
object copySyntax {
implicit class CopySyntax[TypeToCopy](thingToCopy: TypeToCopy) {