Skip to content

Instantly share code, notes, and snippets.

sealed trait Conc[+T] {
def level: Int
def size: Int
def left: Conc[T] = throw new NoSuchElementException("no left subtree")
def right: Conc[T] = throw new NoSuchElementException("no right subtree")
def <>[U >: T](that: Conc[U]): Conc[U] = {
if (this == Empty) that
else if (that == Empty) this
else concat(this, that)
package week2
object Lecture6 extends App {
def reduceSeg1[A](inp: Array[A], left: Int, right: Int, a0: A, f: (A, A) => A): A = {
var a = a0
var i = left
while (i < right) {
a = f(a, inp(i))
i += 1
}
package week2
import java.util.Arrays
import common.ParallelConstructs._
import scala.util.Random
import org.scalameter._
object ParallelMergeSortLecture extends App {
@george-hawkins
george-hawkins / StreamCost.java
Created August 27, 2016 10:44
Do streams involve upfront calculation in Java?
package net.betaengine;
import java.util.stream.IntStream;
public class StreamCost {
private boolean isPrime(int n) {
System.out.println("Checking if " + n + " is a prime.");
return IntStream.range(2, n).allMatch(x -> n % x != 0);
}
package mytests
import org.scalatest.FunSuite
class MonadLawSuite extends FunSuite {
// 1. Remind ourselves about associativity.
test("addition is associative") {
val rtl = (1 + 2) + 3
val ltr = 1 + (2 + 3)
def union(that: TweetSet): TweetSet = {
(left union (right union that)) incl elem
// If we have two trees x={{{.a.}b.}c{.d{.e.}}} and y={{{.f.}g.}h{.i{.j.}}} the number of calls to union varies dramatically according to how we order things:
//
// (left union (right union that)) incl elem // calls=5
// (right union (left union that)) incl elem // calls=5
// (right union (that union left)) incl elem // calls=36
// (left union (that union right)) incl elem // calls=33
// (that union (left union right)) incl elem // calls=26
// (that union (right union left)) incl elem // calls=26
@george-hawkins
george-hawkins / SpringPropertiesTest.java
Last active June 9, 2016 10:07
Accessing Spring properties in a non-Spring application. This test demonstrates creating an application context that doesn't create beans but does setup the environment.
package com.example;
import java.util.Arrays;
import java.util.NavigableSet;
import java.util.Set;
import java.util.TreeSet;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.boot.ApplicationArguments;
@george-hawkins
george-hawkins / hcidump.out
Last active November 25, 2015 17:00
Noble #269
package hello;
import com.stormpath.sdk.servlet.config.CookieConfig;
public class DelegatingCookieConfig implements CookieConfig {
private final CookieConfig delegate;
public DelegatingCookieConfig(CookieConfig config) {
this.delegate = config;
}
'use strict';
var globalFoo;
function Foo(name) {
this.name = name;
}
function captureGlobalFoo(name) {
globalFoo = new Foo(name);