Skip to content

Instantly share code, notes, and snippets.

@non
non / Tag.scala
Last active September 10, 2015 14:50
Quick little demo of type tagging in Scala that doesn't seem to have appreciable overhead for non-primtive types. For primtive types (e.g. `Int`) the overhead is equivalent to the kind of boxing you get with `.sum`.
package cats
import cats.macros.Tags
object Tag {
type @@[A, T] = Tags.Tagged[A, T]
implicit class TagOps[A](a: A) {
def tag[T]: A @@ T = macro Tags.tagMacro[A, T]
@non
non / gist:1482907
Created December 15, 2011 21:13
d_m's nescala talk proposal
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.
@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
@non
non / pluck.py
Last active September 30, 2015 08:37
#!/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:
#!/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; }
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
}
}
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 March 24, 2012 15:59
using SIP-15 value classes to encode complex numbers
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 March 31, 2012 23:42
Value classes and unboxed tuples
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)
[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]