Skip to content

Instantly share code, notes, and snippets.

View erikerlandson's full-sized avatar

Erik Erlandson erikerlandson

View GitHub Profile
@erikerlandson
erikerlandson / unitvalue.scala
Created August 26, 2015 14:44
Testing ability to constrain type variable bindings to emulate unit algebra
object commutative {
import scala.language.implicitConversions
sealed class =!=[A,B]
trait LowerPriorityImplicits {
implicit def equal[A]: =!=[A, A] = sys.error("should not be called")
}
object =!= extends LowerPriorityImplicits {
implicit def nequal[A,B](implicit same: A =:= B = null): =!=[A,B] =
@erikerlandson
erikerlandson / charts.scala
Created August 28, 2015 19:54
A scatter plot in scala-bokeh
object charts {
import com.redhat.et.silex.util.OptionalArg
import OptionalArg.fullOptionSupport
import io.continuum.bokeh._
private def defaultTools(plot: Plot) = List(
new PanTool().plot(plot),
new BoxZoomTool().plot(plot),
new ResetTool().plot(plot),
new PreviewSaveTool().plot(plot))
// initialize this with the maximum possible distance:
Dbest = M+N;
P = 0;
while (true) {
// the minimum possible distance for the current P value
Dmin = 2*(P-1) + delta;
// if the minimum possible distance is >= our best-known distance, we can halt
if (Dmin >= Dbest) return Dbest;
@erikerlandson
erikerlandson / sizes.cpp
Created March 29, 2014 19:11
dump sizes of atomic numeric types in c/c++
#include <iostream>
using namespace std;
#define showsize(tname) cout << #tname << ": " << sizeof(tname) << endl
int main(int argv, char** argc) {
showsize(char);
showsize(short);
showsize(int);
@erikerlandson
erikerlandson / find_git_dirs_example.txt
Created May 5, 2014 23:27
A find command that identifies directory names housing a git repo
[eje@localhost git]$ for dir in `find /home/eje/git -maxdepth 3 -exec test -d \{\}/.git \; -print` ; do echo $dir; cd $dir ; git reset --hard HEAD; done
scala> val f1 = (x:Int, t:Int) => (x > t)
f1: (Int, Int) => Boolean = <function2>
scala> f1
res22: (Int, Int) => Boolean = <function2>
scala> val f2 = (x:Int, t:Int=7) => (x > t)
<console>:1: error: ')' expected but '=' found.
val f2 = (x:Int, t:Int=7) => (x > t)
@erikerlandson
erikerlandson / scala_config_demo.scala
Created May 16, 2014 03:09
A crude prototype of a configuration system in scala that supports easy meta-config of policies for individual configuration parameters
import scala.collection.mutable
import scala.reflect.runtime.universe.{TypeTag, TypeRef, typeOf}
// A function that also keeps its domain and range types as a payload
class FunctionTP[D: TypeTag, R:TypeTag](f: D=>R) extends Function[D,R] {
val domainType = typeOf[D]
val rangeType = typeOf[R]
val func = f
override def apply(x: D):R = func(x)
def compose[E:TypeTag](g: E=>D):FunctionTP[E,R] = new FunctionTP(func compose g)
@erikerlandson
erikerlandson / generic_conversion_example.scala
Created May 19, 2014 21:33
Demonstrate generic conversion, where explicit invocations work but implicit invocations don't happen
trait CastOptionToV[V] {
def cast[T:TypeTag](o:Option[T]):V
}
class CastOptionToLong extends CastOptionToV[Long] {
def cast[T:TypeTag](o:Option[T]):Long = {
val tLong = typeOf[Long]
typeOf[T] match {
case `tLong` => o.asInstanceOf[Option[Long]].get
case _ => throw new Exception
}
@erikerlandson
erikerlandson / boost_date_time.cpp
Created June 14, 2014 18:07
Demonstrate basic boost date/time usage of clock, duration and time comparison. Compare cost of local_time() and universal_time(). The universal_time() function is about 2x faster.
#include <iostream>
#include "boost/date_time/posix_time/posix_time_types.hpp"
using namespace boost::posix_time;
int main(int argc, char** argv) {
const int dsec = 5;
{
ptime t0 = microsec_clock::local_time();
@erikerlandson
erikerlandson / gist:8518b1879f8748cb9120
Last active August 29, 2015 14:04
start up spark master and slaves in a sandbox
# start-slaves wants sshd running
# start up master
# web ui: http://localhost:8080/
$ ./sbin/start-master.sh
# start up 2 slaves
# web ui: http://localhost:8081/ (8082, 8083, ...)
$ env SPARK_WORKER_INSTANCES=2 ./sbin/start-slaves.sh