Skip to content

Instantly share code, notes, and snippets.

View cvogt's full-sized avatar

Jan Christopher Vogt cvogt

  • Symbiont.io
View GitHub Profile
@cvogt
cvogt / gist:58256526507e8f26a942
Last active April 11, 2017 19:01
Stop timewarner spam letters
To: TWC.Privacy@twcable.com
Subject: Stop all advertising mail
I hereby request that you immediately stop any kind of mailing for
advertisment or any other purpose to the following address
<YOUR STREET ADDRESS>
Please confirm.
@cvogt
cvogt / defaults-and-copy-ideas.scala
Last active February 22, 2017 13:51
Scala language proposals for defaults, .copy and overrides
// DEFAULT VALUES
def foo(str: String = "test") = ???
assert( foo.str == "test" ) // new: named access to default values
case class Bar(str: String = "test")
assert( Bar.apply.str == "test" ) // same for case classes via .apply method
// REPETIION-FREE .copy (aka lenses light)
@cvogt
cvogt / breakout.scala
Last active January 19, 2017 11:20
Scala collections breakOut
// a List isn't a Map
scala> val x: Map[Int,Int] = List(1,2,3).map(i => i -> i)
<console>:16: error: type mismatch;
found : List[(Int, Int)]
required: Map[Int,Int]
val x: Map[Int,Int] = List(1,2,3).map(i => i -> i)
^
// first create List, then create Map. Intermediate collection overhead
scala> val x: Map[Int,Int] = List(1,2,3).map(i => i -> i).toMap
x: Map[Int,Int] = Map(1 -> 1, 2 -> 2, 3 -> 3)
package tut
import java.io.{ OutputStream, FilterOutputStream }
// FilterOutputStream that removes ANSI escapes. This should be fairly efficient, I guess, since
// it's all case switches on constants. Can inline more if needed but it seems fine. For the
// record, the format is ESC '[' digits (';' digits)* terminal
case class AnsiFilterStream(os: OutputStream) extends FilterOutputStream(os) {
case class State(apply: Int => State)
@cvogt
cvogt / Application.scala
Last active October 9, 2016 19:06
decoupled configuration code structure
object Application{
def run(config: Config): Unit = {
val db = connectToDb( config.dbHost, config.dbPort )
...
}
}
case class Config(
dbHost: String,
port: Int
)
@cvogt
cvogt / Application.scala
Last active October 9, 2016 19:05
configuration managed by the application
object Application{
def main(args: Array[String]): Unit = {
val config = Config.get( args(0) )
val db = connectToDb( config.dbHost, config.dbPort )
...
}
}
object Config{
def get(config: String) = {
object DynamicConfig{
def main(args: Array[String]): Unit = {
Application.run(
Config(
sys.props.get("db.host").get,
10000
)
}
}
object PartiallyDynamicConfig{
def main(args: Array[String]): Unit = {
Application.run(
Config(
sys.props.get("db.host") getOrElse "db.test-server.com",
10000
)
}
}
object SwitchableConfig{
def main(args: Array[String]): Unit = {
val config = System.getProperty("config") match {
case "test" => Config("db.test-server.com",10000)
case "production" => Config("db.production-server.com",12345)
case _ => throw new Exception("unknown config: "+config)
}
Application.run( config )
}
}
@cvogt
cvogt / ProductionConfig.scala
Last active October 6, 2016 23:35
serializing scala configuration
object ProductionConfig{
def main(args: Array[String]): Unit = {
val config = Config("db.production-server.com",12345)
if(args(0) == "serialize")
println( toJson( config ) )
else
Application.run( config )
}
}