Skip to content

Instantly share code, notes, and snippets.

@davidandrzej
davidandrzej / csv-cyclic-tags.py
Created July 11, 2014 15:16
Use Python Pandas to append cycling tags to rows in a CSV file
import pandas as pd
import itertools
# Tag every row in a CSV with repeating 3 tags
df = pd.read_csv('source.csv')
tags = ['foo','baz','buzz']
df['tag'] = [tag for (tag, row) in zip(itertools.cycle(people), df['targetcolumn'])]
df.to_csv('destination.csv')
@davidandrzej
davidandrzej / map-with-default.scala
Created November 25, 2013 20:29
Unexpected (by me, at least) behavior of Scala `Map` when defined using `withDefaultValue`. Of course this is all documented in various places and probably makes sense to someone with a sophisticated understanding of the Scala Collections Library, but these are certainly potential "gotchas" in Map behavior. Credit: pointed out to me by Yuchen Zhao.
//
// 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
//
@davidandrzej
davidandrzej / key-union.scala
Created October 31, 2013 19:37
Get union of all keys from a collection of Maps, the scalaz way.
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)
@davidandrzej
davidandrzej / sampleCsv.py
Created September 17, 2013 21:12
Subsample CSV rows in Python
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()
@davidandrzej
davidandrzej / mathjax.html
Created August 29, 2013 17:46
Snippets for adding MathJax to a website, see [this discussion](https://github.com/ruhoh/ruhoh.rb/issues/46#issuecomment-6988308).
<!-- 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']
},
@davidandrzej
davidandrzej / LazyRetry.scala
Created June 19, 2013 18:04
Example illustrating "lazy val" initialization when the RHS fails by throwing an Exception.
//
// 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
@davidandrzej
davidandrzej / combine-maps.scala
Last active December 16, 2015 09:29
Combining two Map[String,Int] with key-wise addition.
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
}
}
@davidandrzej
davidandrzej / TrickyMapKeys.scala
Last active December 14, 2015 20:29
Scala 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.
// 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 {
@davidandrzej
davidandrzej / TrickyMapValues.scala
Last active February 22, 2024 09:42
Scala Map.mapValues returns a (lazy) view. Hilarity ensues.
//
// 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
//
@davidandrzej
davidandrzej / seq-union.scala
Created December 9, 2012 18:25
Scala Seq.union behavior - come on!
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)