Skip to content

Instantly share code, notes, and snippets.

@runarorama
runarorama / gist:e2c3a206c94c00e12ed7
Created September 22, 2014 20:06
Interactive location plot of busses in Iceland
Block[{},
BusData[bus_] :=
"results" /. First[Import[StringJoin["http://apis.is/bus/realtime?busses=", bus], "JSON"]];
AllBusData := "results" /. First[Import["http://apis.is/bus/realtime?busses=", "JSON"]];
Dist[{x_ -> y_, a_ -> b_}] := (Flatten[{#1, a -> b}] & ) /@ y;
CommaSep[xs_] := StringTemplate["<*Row[`1`,\",\"]*>"][xs];
@runarorama
runarorama / gist:a6f4c110619837f8c659
Created September 22, 2014 15:54
If you want to see something terrifying...
Dynamic[With[{img = CurrentImage[]}, Block[{},
faces = FindFaces[img];
If[Length[faces] > 0, (
mask =
Graphics[Disk[Mean[##], First@Differences[##]/2] & /@ faces,
PlotRange -> Transpose[{{0, 0}, ImageDimensions[img]}],
ImageSize -> 1 -> 1];
blur = Max[Abs[#2 - #1] & @@@ faces]/5;
ImageCompose[img,
SetAlphaChannel[ColorNegate[img],
@runarorama
runarorama / gist:a9b0b1cbe88ae1ec256f
Last active August 29, 2015 14:06
Where's the MBTA bus?
Block[{},
Query :=
Import["http://webservices.nextbus.com/service/publicXMLFeed?a=mbta&command=vehicleLocations", "XML"];
UpdatePositions :=
busPositions = Cases[Query,
XMLElement["vehicle",
{__,
"routeTag" -> rt_,
@runarorama
runarorama / gist:01ee398aa85dabedaf6d
Created September 12, 2014 01:14
Catenable deques in Scala
package scalaz
import Deque._
/**
* A purely functional binary catenable deque.
* Based on "Notes on Catenable Deques in Pure LISP" by Mihaesau and Tarjan.
*/
sealed trait Deque[A] {
sealed trait WriterBox {
type T
def value: T
def evidence: Writer[T]
}
object WriterBox {
def apply[B](b:B)(implicit W: Writer[B]): WriterBox =
new WriterBox {
type T = B
import scala.language.experimental.macros
import scala.reflect.macros.whitebox.Context
class Foo {
override def toString: String = macro Foo.toStringImpl
}
object Foo {
def toStringImpl(c: Context): c.Expr[String] = {
import c.universe._
@runarorama
runarorama / gist:78c8fefbab74701afab3
Last active June 24, 2018 17:06
Playing around with "Finally Tagless, Partially Evaluated" in Scala
// Finally tagless lambda calculus
object Final {
def varZ[A,B](env1: A, env2: B): A = env1
def varS[A,B,T](vp: B => T)(env1: A, env2: B): T = vp(env2)
def b[E](bv: Boolean)(env: E): Boolean = bv
def lam[A,E,T](e: (A, E) => T)(env: E): A => T = x => e(x, env)
def app[A,E,T](e1: E => A => T, e2: E => A)(env: E): T = e1(env)(e2(env))
def testf1[E,A](env: E): Boolean =
app(lam[Boolean,E,Boolean](varZ) _, b(true))(env)
@runarorama
runarorama / gist:c1341e69136bad8ecc39
Last active August 29, 2015 14:03
Why doesn't this recur?
def fix[H,DA:c.WeakTypeTag,DB:c.WeakTypeTag](
f: (c.Expr[DA => DB], H) => c.Expr[DA => DB]): H => c.Expr[DA => DB] =
h => c.Expr[DA => DB](
q"{ def self(n: ${c.weakTypeOf[DA]}): ${c.weakTypeOf[DB]} = ${f((c.Expr(q"self"), h))}(n); self _}")
import scala.language.experimental.macros
import scala.reflect.macros.whitebox.Context
sealed trait Trampoline[A] {
def flatMap[B](f: A => Trampoline[B]): Trampoline[B] = macro Trampoline.flatMapImpl[A,B]
}
case class Return[A](a: A) extends Trampoline[A]
case class Suspend[A](k: () => Trampoline[A]) extends Trampoline[A]
import scala.reflect.macros.whitebox.Context
import scala.language.experimental.macros
sealed trait Trampoline[A] {
def flatMap[B](f: A => Trampoline[B]): Trampoline[B] = macro Trampoline.flatMapImpl[A,B]
}
case class Return[A](a: A) extends Trampoline[A]
case class Suspend[A](k: () => Trampoline[A]) extends Trampoline[A]