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
// | |
// CASE 1: Map without default value | |
// Result: behavior seems reasonable | |
// | |
val boo = Map("a" -> 1, "b" -> 2) | |
// boo: scala.collection.immutable.Map[String,Int] = Map(a -> 1, b -> 2) | |
boo("c") // java.util.NoSuchElementException: key not found: c | |
boo.get("c") //res1: Option[Int] = None | |
// |
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
import scalaz._, Scalaz._ | |
val data = Vector(Map(1 -> "a", 2 -> "b"), Map(2 -> "c", 3 -> "d")) | |
data.foldMap(_.keys.toSet) | |
// res0: scala.collection.immutable.Set[Int] = Set(1, 2, 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
import csv | |
import random | |
outf = open('output.csv','w') | |
writer = csv.writer(outf) | |
for row in csv.reader(open('input.csv', 'rb')): | |
if(random.randint(0,1) == 0): # eg, drop 50% | |
writer.writerow(row) | |
outf.close() |
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
<!-- INSERT MATHJAX CONFIGURATION FROM MATH.STACKEXCHANGE --> | |
<script type="text/x-mathjax-config"> | |
MathJax.Hub.Config({ | |
jax: ["input/TeX", "output/HTML-CSS"], | |
tex2jax: { | |
inlineMath: [ ['$', '$'] ], | |
displayMath: [ ['$$', '$$']], | |
processEscapes: true, | |
skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code'] | |
}, |
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
// | |
// If a "lazy val" initialization fails, the val is not initialized | |
// to anything and subsequent calls re-evaluate the RHS | |
// | |
object LazyRetry extends App { | |
class ServiceFactory() { | |
// Only returns a value after two failures | |
// (simulate waiting for service availability) | |
var tries = 0 |
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 CombineMaps { | |
type Counts = Map[String,Int] | |
def combine(x: Counts, y: Counts): Counts = { | |
val x0 = x.withDefaultValue(0) | |
val y0 = y.withDefaultValue(0) | |
val keys = x.keys.toSet.union(y.keys.toSet) | |
keys.map{ k => (k -> (x0(k) + y0(k))) }.toMap | |
} | |
} |
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
// Map.keys returns an Iterable that happens to be backed by a Set so | |
// if you map() over it you get Set-like behavior. Good times. | |
// | |
// $ scalac TrickyMapKeys.scala | |
// $ scala TrickyMapKeys | |
// keys mod 2 (VERSION A): Set(0, 1) | |
// keys mod 2 (VERSION B): List(0, 0, 1, 0) | |
// | |
object TrickyMapKeys extends App { | |
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
// | |
// TIL Scala Map.mapValues returns a lazy view | |
// | |
// This has the horrific consequence that supplying a non-pure function | |
// can yield a result map with unexpected / undesired behavior (see below) | |
// | |
// Further discussion here: | |
// http://stackoverflow.com/questions/14882642/scala-why-mapvalues-produces-a-view-and-is-there-any-stable-alternatives | |
// https://issues.scala-lang.org/browse/SI-4776 | |
// |
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
scala> val x = Seq(1,2) | |
x: Seq[Int] = List(1, 2) | |
scala> val y = Seq(2,3) | |
y: Seq[Int] = List(2, 3) | |
scala> x.union(y) | |
res0: Seq[Int] = List(1, 2, 2, 3) |