Skip to content

Instantly share code, notes, and snippets.

View kevinmeredith's full-sized avatar

Kevin Meredith kevinmeredith

  • https://twitter.com/Gentmen
View GitHub Profile
@kevinmeredith
kevinmeredith / T.scala
Created September 2, 2016 19:37
dependent types with implicits
scala> sealed trait A { type T }
defined trait A
scala> case class AImpl(x: Int) extends A { override type T = Int }
defined class AImpl
scala> case class AImpl2(x: String) extends A { override type T = String }
defined class AImpl2
scala> def f(x: A)(implicit ev: x.T): x.T = ev
@kevinmeredith
kevinmeredith / X.scala
Last active August 19, 2016 14:08
Using ADT instead of having at least 1 un-used argument
sealed trait Result
case class Good(status: Int) extends Result
case class Bad(error: Int) extends Result
// assume there's a `String => Int` function baked into `f`
// note - I don't like `String` as a return type, but it's not the purpose
// of this exercise
def f(result: Result, handle: Result => String): String = ???
// assume there's a `String => Int` function baked into `f2`
[info] Running net.async.AsyncClientServer
[info] ROUTER: Received message from JILL.
[info] ROUTER: received empty:
[info] ROUTER: received message: READY
[info] DEALER: JILL received
[info] DEALER: JILL received WORLD
[info] DEALER: received ACK!
[info] ROUTER: Received message from JOE.
[info] ROUTER: received empty:
package net
import scala.util._
import shapeless._
object Test {
case class Person(name: String, age: Double)
case class PersonWrapper(person: Person, x: Int)
@kevinmeredith
kevinmeredith / file.md
Created July 9, 2016 17:32
DB Constraints to support smart constructor?

Smart Constructor

class Person private(name: String, age: Int)

object Person {
  def apply(name: String, age: Int): Option[Person] = 
    if (name.nonEmpty && age >= 0 && age < 115) Some( new Person(name, age) ) else None
}
@kevinmeredith
kevinmeredith / ProgScalaQueue.md
Created July 6, 2016 02:19
Type out example from Programming in Scala, 3rd edition - `Queue` in Listing 19.4

After pasting Listing 19.4 (trait Queue):

scala> :paste
// Entering paste mode (ctrl-D to finish)

trait Queue[T] {
def head: T
def tail: Queue[T]
def enqueue(x: T): Queue[T]
@kevinmeredith
kevinmeredith / ReadsExample.scala
Last active July 1, 2016 17:30
defining 'reads' in companion w/ private constructor
scala> :paste
// Entering paste mode (ctrl-D to finish)
import play.api.libs.json._
import play.api.libs.functional.syntax._
case class Person private(age: Int)
object Person {
def build(age: Int): Option[Person] =
@kevinmeredith
kevinmeredith / Test.md
Last active June 14, 2016 16:16
Return Code of `sbt test` When it Fails w/ ScalaTest
$cat build.sbt 
scalaVersion := "2.11.8"

libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.2.6"

# simply runs a test that does `assert(false)`
$sbt test
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=512M; support was removed in 8.0
[info] Set current project to work (in build file:/Users/Kevin/Workspace/)
@kevinmeredith
kevinmeredith / Test.md
Last active May 10, 2016 18:11
Looking at `docker events`

Terminal 1

$cat Dockerfile
FROM ubuntu

ADD ./test.sh /root/test.sh

ENTRYPOINT sh /root/test.sh
{
"swagger": "2.0",
"info": {
"version": "v111",
"title": "testing-april-26-kmm",
"description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification",
"termsOfService": "http://swagger.io/terms/",
"contact": {
"name": "Swagger API Team"
},