Skip to content

Instantly share code, notes, and snippets.

Avatar

Arnold deVos arnolddevos

View GitHub Profile
@arnolddevos
arnolddevos / Barrier.scala
Last active Feb 24, 2021
Easy concurrency primitives
View Barrier.scala
// 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 Nov 11, 2017
Implicit Functions Example in plain scala 2.12
View implicex.scala
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)
}
View KeyFunctionDemo.scala
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 Feb 5, 2016
Dependent Types Example
View DT.scala
// 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 Nov 4, 2014
Scan git repos and produce a time-sorted, consolidated log. (Hint: edit the variables in this.)
View git-log-consolidate.sh
#! /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 Aug 29, 2015
some sort of monad defined in terms of a sort of fold
View thing.scala
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 Nov 4, 2012
understand shell quoting
View bash
decorate() {
for f in "$@"
do
echo -n "[$f] "
done
echo
}
@arnolddevos
arnolddevos / CharSeqView.scala
Created Jun 12, 2012
A view of a CharSequence that implements subSequence() without copying the whole contents.
View CharSeqView.scala
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
View examples.scala
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 Sep 11, 2010
Idea for generator and suspendable wrapper
View gist:574873
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