Skip to content

Instantly share code, notes, and snippets.

View twolfe18's full-sized avatar

Travis Wolfe twolfe18

  • Google
  • San Francisco
View GitHub Profile
File f = new File("foo.txt");
// You're not allowed to make a Path using raw strings,
// that would be dangerous!
Path p = FileSystems.getDefault().getPath("foo.txt");
// Both of these type check :)
f.getName().endsWith(".txt"); // true
p.getFileName().endsWith(".txt"); // false
@twolfe18
twolfe18 / SoABenchmark.java
Last active January 18, 2021 21:16
Does SoA (structure of arrays) beat AoS (array of structures) for Java?
package sandbox;
import java.util.Arrays;
import java.util.Random;
public class SoABenchmark {
public static Random rand = new Random(9001);
public static int NUM_LABELS = 10;
public static Struct[] aos;
@twolfe18
twolfe18 / gist:24d3ffcb332a9021460d
Last active August 29, 2015 14:17
One more reason I hate Java serialization...
package sanbox;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class SerializationBugMWE {
@twolfe18
twolfe18 / playing_with_features.scala
Last active December 30, 2015 20:59
how to write neat feature functions in scala
object IDontCare {
def main(args: Array[String]) {
example
example2
example3
}
def conjoin[I,P,O](f1: I => P, f2: I => P, compose: (P, P) => O): I => O = {
@twolfe18
twolfe18 / gist:7207779
Last active December 26, 2015 20:19
optimization package interfaces
trait Func0[V, P] {
def value(p: P): V
def dimension: Int
}
trait Func1[V, G, P] extends Func0[V, P] {
def grad(p: P): G = compute(p)._2
def compute(p: P): (V, G)
}
@twolfe18
twolfe18 / BetterLogging.scala
Last active December 22, 2015 11:18
a simple logging scheme in scala (that i can understand). i know there are a million logging frameworks, most of which probably have more features than this. but the way i see it, the time that it takes to learn one of those is about the same as the time it takes to write this one. the nice benefit of doing it this way is that i can always just …
// NOTE: I actually made a mistake in this implementation
// Logging needs to have a redirect with type Logging, not Logger
// this is so that if a->b and b gets redirected, things logged via a.log()
// actually get forwarded along (regardless of what came first: redirecting
// b somewhere else or the call to a.log()).
// the only thing to worry about is loops, but i'm willing to take that risk for now.
// i have an implementation of this in Parma called Logging2
import util.Random
@twolfe18
twolfe18 / gist:5767545
Created June 12, 2013 17:49
private[this] vs private in scala
// Foo.scala
class Foo {
private var i = 0
private[this] var j = 0
def getJ = j
def iThing = i * i
def jThing = j * j
}
@twolfe18
twolfe18 / sort-compression.txt
Created March 14, 2013 16:08
a hack i've been considering for compressing log files: sort them first.
time gzip -c <orig >orig.gz
real 0m0.311s
user 0m0.289s
sys 0m0.005s
time bzip2 -c <orig >orig.bz2
real 0m3.358s
user 0m2.981s
sys 0m0.367s
@twolfe18
twolfe18 / google.py
Created January 23, 2012 02:32
command line tool to get URLs for a google search
#!/usr/bin/python
import sys, httplib, re
if len(sys.argv) < 2:
print 'provide a query'
exit(0)
c = httplib.HTTPConnection('www.google.com')
c.request('GET', '/search?q=%s' % ('_'.join(sys.argv[1:])))
r = c.getresponse()
@twolfe18
twolfe18 / diag_len.py
Created January 3, 2012 16:43
get the dimensions of a video screen given its diagonal length
import sys, math
if len(sys.argv) != 2:
print 'provide a diagonal length'
exit(0)
diag = float(sys.argv[1])
# x*x + y*y = diag^2
# x*ar = y