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
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. |
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
// 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) |
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
// 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) |
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 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) |
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
object Application{ | |
def run(config: Config): Unit = { | |
val db = connectToDb( config.dbHost, config.dbPort ) | |
... | |
} | |
} | |
case class Config( | |
dbHost: String, | |
port: 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
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) = { |
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
object DynamicConfig{ | |
def main(args: Array[String]): Unit = { | |
Application.run( | |
Config( | |
sys.props.get("db.host").get, | |
10000 | |
) | |
} | |
} |
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
object PartiallyDynamicConfig{ | |
def main(args: Array[String]): Unit = { | |
Application.run( | |
Config( | |
sys.props.get("db.host") getOrElse "db.test-server.com", | |
10000 | |
) | |
} | |
} |
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
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 ) | |
} | |
} |
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
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 ) | |
} | |
} |