Skip to content

Instantly share code, notes, and snippets.

View zentrope's full-sized avatar

Keith Irwin zentrope

  • Portland, Oregon
View GitHub Profile
private class Http(val address: String) {
// Simple class for snagging content from and posting content
// to a URL. No error handling.
import java.net._
import java.io._
import scala.io._
def get(): String = {
val url: URL = new URL(address)
@zentrope
zentrope / Delegate.scala
Created October 19, 2010 04:22
Example of having one actor create other actors to manage external resources dynamically.
// A sample app demonstrating one way to use actors to manage other
// actors created dynamically. Use case: an app that needs to maintain
// short-lived connections to remote resources (like a messaging queue).
import scala.actors._
import scala.actors.Actor._
class DelegateActor(val id: String) extends DaemonActor {
// Represents an actor that does some work, or hangs on to
// an external resource, such as a socket connection to an MQ
@zentrope
zentrope / transform.scala
Created December 4, 2010 08:23
Transforming XML using RewriteRule and RuleTransformer
// Import the magic libraries
import scala.xml._
import scala.xml.transform._
// Source xml. In Scala, xml is literal.
val xml =
<user>
<email>joe@example.com</email>
@zentrope
zentrope / ScalatePlugin.scala
Created June 9, 2011 21:21
Scalate Plugin using Source Generator for SBT 0.10.0
import sbt._
import Keys._
import Project.Initialize
import org.fusesource.scalate.TemplateSource
import org.fusesource.scalate.TemplateEngine
import java.io.File
@zentrope
zentrope / CoffeeScriptPlugin.scala
Created June 10, 2011 00:29
xsbt coffee script plugin (example) using sourceGenerators
import sbt._
import Keys._
import Process._
import Project.Initialize
import java.io.File
object CoffeeScriptPlugin extends Plugin {
@zentrope
zentrope / grib2-local.scala
Created June 27, 2011 18:40
Fragment: decoding weather strings in a weather.gov grib2 file
// convert a byte into a list of ones and zeros
def bits(num: Int): List[Int] = (for (i <- Range(0,8)) yield (((num << i) & 0xff) >>> 7)).toList
// given a list, take the first N bits
def next(bits: List[Int], numBits: Int) = {
val filler = List[Int](8 - numBits) map (_ => 0)
val bitList = filler ++ bits.take(numBits)
@zentrope
zentrope / MemoryMapInputStream.scala
Created July 1, 2011 03:38
Scala InputStream for Memory Mapped File
// Just an example of some code I ended up not using, but which is kinda neat.
object Utils {
def linesFromFile(file: File) = {
io.Source.fromInputStream(getMemoryMappedFileInputStream(file)).getLines
}
def getMemoryMappedFileInputStream(file: File): InputStream = {
@zentrope
zentrope / GenImage.scala
Created August 22, 2011 15:58
PNG on-the-fly image generation
import java.io.File
import javax.imageio.ImageIO
import java.awt.image.BufferedImage
import java.awt.geom._
import java.awt.Color
import java.awt.Font
import java.awt.Graphics2D
import java.util.Date
@zentrope
zentrope / bytes_to_int.clj
Created January 11, 2012 00:22
bytes->int clojure
;; Surely I can do better than this.
(defn- ^BigInteger bytes->int
[^bytes bytes & {:keys [little-endian]
:or {little-endian true}}]
(let [b (if little-endian (reverse bytes) bytes)]
(->> b
(cons (byte 0))
(byte-array)
(biginteger))))
@zentrope
zentrope / gist:2228433
Created March 28, 2012 17:26
homebrew mysql 5.1 failure
Homebrew 0.9
==> Downloading http://mysql.mirrors.pair.com/Downloads/MySQL-5.1/mysql-5.1.58.tar.gz
Already downloaded: /Library/Caches/Homebrew/mysql51-5.1.58.tar.gz
/usr/bin/tar xf /Library/Caches/Homebrew/mysql51-5.1.58.tar.gz
==> Patching
/usr/bin/patch -f -p1 -i 000-homebrew.diff
patching file scripts/mysqld_safe.sh
Hunk #1 succeeded at 384 (offset 1 line).
patching file scripts/mysql_config.sh
==> ./configure --without-docs --without-debug --disable-dependency-tracking --prefix=/usr/local/Cellar/mysql51/5.1.58 --localstatedir=/usr/local/var/mysql --sysconfdir=/usr/local/etc --with-plugins=innobase,myisam --with-extra-charsets=complex --with-ssl --without-readline --enable-assembler --enable-thread-safe-client --enable-local-infile --enable-shared --with-partition