Skip to content

Instantly share code, notes, and snippets.

Avatar

Erik Osheim non

  • Providence / Philly
View GitHub Profile
@non
non / gist:1482907
Created Dec 15, 2011
d_m's nescala talk proposal
View gist:1482907
Fast, accurate, generic, compatible: tensions around Scala's number types
Working with numbers in Scala can be a bit awkward. The number types are affected by
(and motivate) some of the weirder parts of Scala language and library: AnyVal types,
Arrays, Manifests, specialization, and type classes like Numeric. On the one hand, we
want operations on numbers to be as fast as Java operations on primitives. On the
other hand, Scala is an opportunity to do better than Java, especially with regard to
ad-hoc polymorphism, precision and power. This talk explores the problems, tensions and
opportunities the community faces as we evolve language and surrounding libraries.
View gist:1643619
@tailrec
def collapse[A](as:List[A])(f:(A, A) => A):Option[A] = as match {
case Nil => None
case a :: Nil => Some(a)
case as => collapse(pairwise(as, Nil)(f))(f)
}
@tailrec
View pluck.py
#!/usr/bin/python
import random
import sys
if __name__ == "__main__":
args = sys.argv[1:]
if args:
need = int(args[0])
f = open(args[1], 'r') if len(args) > 1 else sys.stdin
else:
View gist:1754388
#!/bin/bash
#
# A more capable sbt runner, coincidentally also called sbt.
# Author: Paul Phillips <paulp@typesafe.com>
# this seems to cover the bases on OSX, and someone will
# have to tell me about the others.
get_script_path () {
local path="$1"
[[ -L "$path" ]] || { echo "$path" ; return; }
View gist:1794165
import scala.{specialized => spec}
class Foo[@spec(Int) A](val a:A) {
def sink(x:A) = ()
def bar[@spec(Int) B](b:B):Int = {
sink(a)
99
}
}
View gist:2148027
trait WithContext {
var ctx = null
def setContext(c:Context): Unit = if (ctx == null) {
ctx = c
} else {
sys.error("already set")
}
def fromContext[T](k:String):T = if (ctx == null) {
sys.error("premature access")
} else {
@non
non / LongComplex.scala
Created Mar 24, 2012
using SIP-15 value classes to encode complex numbers
View LongComplex.scala
import scala.math._
object Main {
def main(args:Array[String]) {
val a = LongComplex(13, 0)
val b = LongComplex(6, 2)
//println(a)
//println(b)
val c = a + b
//println(c)
@non
non / Tuples.scala
Created Mar 31, 2012
Value classes and unboxed tuples
View Tuples.scala
object Util {
def unsigned(n:Byte) = (n + 0x100) & 0xFF
def unsigned(n:Short) = (n + 0x10000) & 0xFFFF
def b2i(a:Byte, b:Byte): Int =
(a<<8) + unsigned(b)
def b3i(a:Byte, b:Byte, c:Byte): Int =
(a<<16) + (b<<8) + unsigned(c)
def b4i(a:Byte, b:Byte, c:Byte, d:Byte): Int =
(a<<24) + (b<<16) + (c<<8) + unsigned(d)
View gist:2330315
[error] /home/erik/spire/src/main/scala/spire/buffer/Immutable.scala:33: ambiguous implicit values:
[error] both method manifest in class Reversed of type => Manifest[A]
[error] and value evidence$7 in class Reversed of type Manifest[A]
[error] match expected type Manifest[A]
@non
non / test.scala
Created Apr 29, 2012
Example of sorting list of lists
View test.scala
import annotation.tailrec
import scala.math.Ordering
import scala.util.Random
class ListOrdering extends Ordering[List[Int]] {
@tailrec final def compare(as:List[Int], bs:List[Int]) = (as, bs) match {
case (Nil, Nil) => 0
case (Nil, _) => -1
case (_, Nil) => 1
case (a::as, b::bs) if a < b => -1