Skip to content

Instantly share code, notes, and snippets.

View arnolddevos's full-sized avatar

Arnold deVos arnolddevos

View GitHub Profile
@arnolddevos
arnolddevos / Barrier.scala
Last active February 24, 2021 01:16
Easy concurrency primitives
// take() blocks until next* offer()
// (similar but not the conventional meaning of barrier)
def barrier() = new Gate[Unit, Long] {
private val state = new Transactor(0l)
val take =
for {
v0 <- state.transact(v => Observed(succeed(v)))
v1 <- state.transact { v =>
if(v > v0) Observed(succeed(v))
@arnolddevos
arnolddevos / implicex.scala
Last active November 11, 2017 19:45
Implicit Functions Example in plain scala 2.12
import scala.collection.mutable.ListBuffer
object implicex {
trait ImplicitFunction1[-A, +B] {
def apply()(implicit a: A): B
}
def fn[A, B](f: A => B): ImplicitFunction1[A, B] = new ImplicitFunction1[A, B] {
def apply()(implicit a: A): B = f(a)
}
@arnolddevos
arnolddevos / KeyFunctionDemo.scala
Created March 18, 2016 07:07
d3 key function demo
package test
import scala.scalajs.js
import org.singlespaced.d3js.d3
import org.singlespaced.d3js.selection.Update
object KeyFunctionDemo extends js.JSApp {
def main() = {
for(i <- 1 to 3) {
@arnolddevos
arnolddevos / DT.scala
Created February 5, 2016 02:01
Dependent Types Example
// Example from Odersky et al http://infoscience.epfl.ch/record/215280/files/paper.pdf
object DT {
trait Key { type Value }
class Setting(val str: String) extends Key
val sort = new Setting("sort") { type Value = String }
val width = new Setting("width") { type Value = Int }
val params = HMap.empty.add(width)(120).add(sort)("time")
@arnolddevos
arnolddevos / git-log-consolidate.sh
Created November 4, 2014 07:04
Scan git repos and produce a time-sorted, consolidated log. (Hint: edit the variables in this.)
#! /bin/bash
# git-log-consolidate
set -e
SINCE="Aug 25"
UNTIL="Sep 19"
LOG=~/git-log-consolidated.log
TMP=$LOG.tmp
@arnolddevos
arnolddevos / thing.scala
Last active August 29, 2015 14:06
some sort of monad defined in terms of a sort of fold
package thing
import scala.language.higherKinds
trait Thing[+T, Context[_]] { parent =>
def foldish[S](s0: S)(f: (S, T) => Context[S]): Context[S]
def map[U](g: T => U) = new Thing[U, Context] {
def foldish[S](s0: S)(f: (S, U) => Context[S]) =
parent.foldish(s0)((s, t) => f(s, g(t)))
@arnolddevos
arnolddevos / bash
Created November 4, 2012 09:46
understand shell quoting
decorate() {
for f in "$@"
do
echo -n "[$f] "
done
echo
}
@arnolddevos
arnolddevos / CharSeqView.scala
Created June 12, 2012 22:50
A view of a CharSequence that implements subSequence() without copying the whole contents.
package au.com.langdale
package util
class CharSeqView( base: CharSequence, offset: Int, val length: Int) extends CharSequence {
def this(base: CharSequence) = this(base, 0, base.length)
def charAt(index: Int): Char = {
if( 0 <= index && index < length)
base.charAt(index + offset)
else
import Pattern._
import Family._
def pattern[B](pf: PartialFunction[Name,B]) = new Extractor(pf.lift)
val Parents = new Extractor(parents.get)
val Children = new Extractor(children.get)
"Julie" match {
case Parents(p) => "Julies parents are: " + p
@arnolddevos
arnolddevos / gist:574873
Created September 11, 2010 05:36
Idea for generator and suspendable wrapper
import scala.util.continuations._
class Generator[A] extends Iterator[A] with (A => Unit @ suspendable) {
private var a: Option[A] = None
private var k: Option[Unit => Unit] = None
def next = {
val a0 = a.get
val k0 = k.get
a = None