Skip to content

Instantly share code, notes, and snippets.

View mepcotterell's full-sized avatar
💭
I may be slow to respond.

Michael Cotterell mepcotterell

💭
I may be slow to respond.
View GitHub Profile
@mepcotterell
mepcotterell / DSL.scala
Created March 2, 2011 14:08
Here's a small example of how to extends types by giving them more operations in Scala.
object DSL {
implicit def MkDSLRichAnyOps[T](elem: T) =
new DSLRichAny(elem)
}
@mepcotterell
mepcotterell / Vec.scala
Created June 23, 2011 16:11
Here is an implementation of a generic, numeric vector in Scala that utilizes mixin compositions with context-bounded type parameters.
package scalation
package advmath
object Vec {
}
class Vec [A: Numeric] extends VecLike[A, Vec[A]] {
}
@mepcotterell
mepcotterell / ExponentiationApp.scala
Created September 21, 2011 01:41
Examples of Unicode Operators using the ScalaTion DSL
object ExponentiationApp extends App with ScalaTion
{
val exp1 = 2 ↑ 2 // 4
val exp2 = 2 ↑ 2 ↑ 2 // 16
println("2↑2 = %s".format(exp1))
println("2↑2↑2 = %s".format(exp2))
}
case class MyNum (val d: Double) extends Numeric [MyNum]
{
def fromInt (n: Int) = MyNum (n)
def + (rhs: MyNum) = plus(this, rhs)
def plus (x: MyNum, y: MyNum): MyNum = MyNum (x.d + y.d)
def minus (x: MyNum, y: MyNum): MyNum = MyNum (x.d - y.d)
def times (x: MyNum, y: MyNum): MyNum = MyNum (x.d * y.d)
def negate (x: MyNum): MyNum = MyNum (-x.d)
def toInt (x: MyNum): Int = x.d.toInt
def toLong (x: MyNum): Long = x.d.toLong
@mepcotterell
mepcotterell / Combo.scala
Created February 2, 2012 19:09
Combination Examples
object Combo extends App {
val vars = List("a", "b", "c", "d")
val list = vars map (e => ("-%s".format(e), "+%s".format(e)))
val len = list.size
val combos = math.pow(2, len).toInt
for (i <- 0 until combos) {
for (j <- 0 until len) {
val (off, on) = list(j)
print ("%s ".format(if ((i >> j) % 2 == 0) off else on))
@mepcotterell
mepcotterell / demo1.results
Created February 15, 2012 05:45
Example SPARQL Queries
:Albert_Einstein :hasAcademicAdvisor :Alfred_Kleiner .
@mepcotterell
mepcotterell / Parens.scala
Created February 28, 2012 02:39
Balanced parentheses example.
object Parens extends App {
def balanced (input: String): Boolean = {
val stack = collection.mutable.Stack.empty[String]
import stack._
for (i <- input) i match {
case '(' => push("(")
case '[' => push("[")
case ')' => if (isEmpty) return false else if (pop() != "(") return false
case ']' => if (isEmpty) return false else if (pop() != "[") return false
google.load('visualization', '1', {'packages': ['geochart']});
google.setOnLoadCallback(drawMarkersMap);
function drawMarkersMap() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'City');
data.addColumn('number', 'Population');
data.addColumn('number', 'Area');
data.addRows([
['Rome', 2761477, 1285.31],
@mepcotterell
mepcotterell / actor.rdf
Created April 2, 2012 15:21
Example RDF output of Sakila database.
<http://johnson.cs.uga.edu/triplify/index.php/actor> <http://www.w3.org/2000/01/rdf-schema#comment> "Generated by Triplify V0.7.1 (http://Triplify.org)" .
<http://johnson.cs.uga.edu/triplify/index.php/actor> <http://creativecommons.org/ns#license> <http://creativecommons.org/licenses/by/3.0/us/> .
<http://johnson.cs.uga.edu/triplify/index.php/actor/1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Actor> .
<http://johnson.cs.uga.edu/triplify/index.php/actor/2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Actor> .
<http://johnson.cs.uga.edu/triplify/index.php/actor/3> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Actor> .
<http://johnson.cs.uga.edu/triplify/index.php/actor/4> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Actor> .
<http://johnson.cs.uga.edu/triplify/index.php/actor/5> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Actor> .
<http://jo
@mepcotterell
mepcotterell / Schema.scala
Created September 7, 2012 12:46
TypeTags Example for Database Schema
// NOT TESTED YET
import scala.reflect.runtime.universe._
class Schema[T : TypeTag](val attributes: String*) extends Dynamic {
val length = attributes.length
def selectDynamic[A: TypeTag](field: String): A.tpe = null
def updateDynamic[A: TypeTag](field: String)(value: A.tpe) { }
} // Schema