Skip to content

Instantly share code, notes, and snippets.

View b-studios's full-sized avatar

Jonathan Immanuel Brachthäuser b-studios

View GitHub Profile
@b-studios
b-studios / exceptions.js
Created May 21, 2013 17:28
short example for cps based exception handling without propagation
/*
* try {
* var foo = 3
* raise "some exception"
* console.log("do some stuff");
* } catch(msg) {
* console.log("caught exception")
* proceed
* }
*
@b-studios
b-studios / exceptions_global.js
Created May 21, 2013 18:07
short excample for cps based exception handling in JavaScript to allow proceedable exception. Uses a global manually maintained stack.
function foo() {
console.log("before exception");
_raise("some exception", function() {
console.log("after exception");
})
}
function bar() {
@b-studios
b-studios / proceedable.rb
Created May 21, 2013 22:59
proceedable exceptions in ruby
require "continuation"
module Proceedable
class ProceedableException < Exception
def initialize(msg, cont)
super(msg)
@cont = cont
end
@b-studios
b-studios / loopcompiler.js
Created June 13, 2013 13:08
Simple loop compilation
function iterator(f, bindings) {
bindings = bindings || {};
var matches = f.toString().match(/function[^(]*\(\s*(\w+)\s*\)[^{]*(.*)/);
if (!matches) {
console.log("iterator function has wrong format, please use only one argument");
}
@b-studios
b-studios / Debugger.scala
Created June 20, 2013 17:50
An attempt to visualize kiama's rewriting process, the html output can be seen at http://b-studios.de/demos/kiama_debug/.
trait DebugRewriter extends org.kiama.rewriting.CallbackRewriter {
import org.bitbucket.inkytonik.dsprofile.Events.{Event, Start}
import org.bitbucket.inkytonik.dsprofile.Events
Events.profiling = true
// ... some outputbuffering and pretty printing here
override def rewriting[T] (oldTerm : T, newTerm : T) : T = {
@b-studios
b-studios / options.js
Last active December 26, 2015 14:59
Encoding Haskells `Maybe` or Scala's `Option` type in JavaScript using the new generators (es6)
function Option(value) {
this.value = value;
this.some = arguments.length == 1
}
Option.none = new Option()
Option.some = v => new Option(v)
Option.prototype.iterator = function () {
if (this.some) yield this.value
}
@b-studios
b-studios / nat.scala
Created November 16, 2013 18:22
Dependent Types and Generalized Type Constraints
sealed trait Nat
sealed trait Zero extends Nat
sealed trait Succ[N <: Nat] extends Nat
@b-studios
b-studios / ui.scala
Last active December 31, 2015 23:08
Example code to illustrate a different approach for Vaadin's scala wrapper classes using the "pimp my library" pattern.
package scalavaadin
import com.vaadin.{ ui => orig }
import scala.language.implicitConversions
import scala.collection.mutable
import scala.collection.JavaConversions
package object ui {
// Adding new Methods
@b-studios
b-studios / Sizeable.scala
Created December 30, 2013 17:15
Strongly Typed Version of Sizeable
package vaadin.scala
package server
import com.vaadin.{ server => orig }
import scala.language.implicitConversions
import scala.PartialFunction.condOpt
import apigen.annotations.accessors
@b-studios
b-studios / IdentityHashMap.scala
Created January 12, 2014 17:32
`IdentityHashMap` as adopted from `scala.collection.mutable.WeakHashMap`
package de.bstudios.collection.mutable
import scala.collection.mutable._
import scala.collection.generic._
import scala.collection.convert.Wrappers._
class IdentityHashMap[A, B] extends JMapWrapper[A, B](new java.util.IdentityHashMap)
with JMapWrapperLike[A, B, IdentityHashMap[A, B]] {
override def empty = new IdentityHashMap[A, B]
}