Skip to content

Instantly share code, notes, and snippets.

rootDir = "/home/zcox/dev/20_newsgroups"
raise rootDir + " does not exist" unless File.directory? rootDir
counts = Hash.new(0) #0 will be the default value for non-existent keys
Dir["#{rootDir}/**/*"].reject{|file| File.directory? file}.each do |file|
IO.read(file).scan(/\w+/) { |word| counts[word.downcase] += 1 }
end
open("counts-descreasing-ruby", "w") do |out|
counts.sort { |a, b| b[1] <=> a[1] }.each { |pair| out << "#{pair[0]}\t#{pair[1]}\n" }
import java.io._
val rootDir = new File("/home/zcox/dev/20_newsgroups")
if (!rootDir.exists) throw new IllegalArgumentException(rootDir + " does not exist")
var counts = Map.empty[String, Int].withDefaultValue(0)
files(rootDir) { _.split("""\W+""").foreach { word => counts = counts(word.toLowerCase) += 1 }}
write(counts, "counts-descreasing-scala") {_._2 > _._2}
write(counts, "counts-alphabetical-scala") {_._1 < _._1}
/** Writes the specified map to the specified file in tab-delimited format, sorted accordingly. */
def using[A <: {def close(): Unit}, B](param: A)(f: A => B): B =
try {
f(param)
} finally {
param.close()
}
<properties>
<scala.version>2.8.0</scala.version>
</properties>
...
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
@zcox
zcox / gist:1196660
Created September 6, 2011 05:22
FluentPipeline Scala Example
import com.tinkerpop.blueprints.pgm._
import com.tinkerpop.blueprints.pgm.impls.tg._
import com.tinkerpop.pipes._
import com.tinkerpop.pipes.util._
import scala.collection.JavaConversions.asScalaIterator
import java.lang.{Boolean => JBoolean}
val graph = TinkerGraphFactory.createTinkerGraph()
//Adapter from T => Boolean to PipeClosure along with an implicit conversion
@zcox
zcox / gist:1256666
Created October 1, 2011 21:13
Typesafe fluent interface subclass in Java
public class ABTest {
public static void main(String[] args) {
A<String> a = new A<String>().f1().f2();
B<String> b = new B<String>().f1().f2().f3();
System.out.println(a + " " + b);
}
/** The type parameter T just shows we can parameterize these types. */
@zcox
zcox / gist:1295890
Created October 18, 2011 16:36
Typesafe fluent interface hierarchy using type constructors
trait PipeFunction[A, B]
class NullPipeFunction[A, B] extends PipeFunction[A, B]
trait Pipe[S, E]
class Pipeline[S, E, T[S, E]] extends Pipe[S, E] {
def add[F](pipe: Pipe[E, F]): T[S, F] =
throw new UnsupportedOperationException()
}
@zcox
zcox / gist:1296389
Created October 18, 2011 19:14
Java forgetting parameterized return type
public class Base<T extends Base> {
public T f1() {
return (T) this; //Warning => Type safety: Unchecked cast from Forgetful.Base<T> to T
}
public T f2() {
return (T) this; //Warning => Type safety: Unchecked cast from Forgetful.Base<T> to T
}
}
@zcox
zcox / gist:1313183
Created October 25, 2011 15:41
blueprints-neo4j perf test
package write
import com.tinkerpop.blueprints.pgm.{ Index, Vertex, Edge, IndexableGraph, TransactionalGraph }
import com.tinkerpop.blueprints.pgm.impls.orientdb.OrientGraph
import com.tinkerpop.blueprints.pgm.impls.neo4j.Neo4jGraph
import com.tinkerpop.blueprints.pgm.TransactionalGraph.Conclusion._
import scala.collection.JavaConversions._
import java.util.Date
object PerfTest {
@zcox
zcox / gist:1933265
Created February 28, 2012 15:48
Sanyo SCP-3810 User-Agent
Mozilla/4.0 (BREW 3.1.5; U; en-us; Sanyo; NetFront/3.5.1/AMB) Boost SCP3810