Skip to content

Instantly share code, notes, and snippets.

View benhardy's full-sized avatar

benhardy benhardy

View GitHub Profile
#define MAX_STEPS 100000
#define MAX_DIST 300.0f
#define EPSILON 0.001f
#define ID_SPHERE 1
#define ID_BIZZO 2
#define ID_FLOOR 3
#define ID_COLUMN 4
#define BIZZO_ITERATIONS 6
@benhardy
benhardy / TheOracle.scala
Created November 7, 2017 16:26
A little experiment in parsing English and answering questions
/** kinds of things that can make up a sentence */
sealed trait Term {
def show: String
def complexity: Int = 1
def root: Term = this
}
case class Word(show: String) extends Term
trait NounTerm extends Term
# layout - kinda python-like.
# indents are multiples of 4 spaces, no tabs allowed, violations won't compile
# if something is indented from the line before, it's implied that it is part of
# the thing defined on the line before
# defining an interface
# when things are immutable, a property is no different to a no-arg function
Verified
verifications: Int
/**
* simple demo of using Phasers to sync completion and waiting for things to complete.
*/
import java.util.concurrent._
import java.lang.{Thread, Runnable}
/**
* Run a function in another thread, yeah it's ugly, whatever, this is a demo.
* This is not the interesting part.
*/
/**
* I know Either is a well known class in Scala, but I'm wondering if I've stumbled onto a well known pattern with this
* extract() function?
*/
public static final class Either<A,B> {
private final A left;
private final B right;
public boolean ifLeft(@Nonnull Consumer<A> todo){
if (left != null) {
// fully qualified Java clases deliberately, so it screams THIS IS FOR JAVA
// please excuse ASCII art for readability of very similar functions
implicit def toJavaConsumer[T] (con: T=>Unit) = new java.util.function.Consumer[T] { def accept(t:T) = con(t) }
implicit def toJavaBiConsumer[A,B] (con: (A,B)=>_) = new java.util.function.BiConsumer[A,B] { def accept(a:A,b:B) = con(a,b) }
implicit def toJavaSupplier[T] (sup: Unit=>T) = new java.util.function.Supplier[T] { def get(): T = sup() }
implicit def toJavaFunction[T,R] (fun: T=>R) = new java.util.function.Function[T,R] { def apply(t:T) = fun(t) }
implicit def toJavaBiFunction[T,U,R] (fun: (T,U)=>R) = new java.util.function.BiFunction[T,U,R] { def apply(t:T, u:U) = fun(t, u) }
public HttpField getField(String name)
{
for (int i=0;i<_fields.size();i++)
{
HttpField f=_fields.get(i);
if (f.getName().equalsIgnoreCase(name))
return f;
}
return null;
}
@benhardy
benhardy / MontyHallSimulationTest.java
Last active August 29, 2015 14:19
In case you thought the Monty Hall Problem hadn't been done to death.
package monty;
import static com.google.common.collect.ImmutableSet.of;
import static com.google.common.collect.Sets.difference;
import static java.util.stream.Collectors.toList;
import static org.junit.Assert.assertEquals;
import java.util.List;
import java.util.Set;
#!/bin/sh
#
# Show resource usage as reported by Singularity
#
# author: github.com/benhardy
set -o errexit -o nounset
if [ $# -ne 1 ]; then
echo "Usage: $0 singularity-resource-usage.sh SINGULARITY_SERVER"
@benhardy
benhardy / j8builder.java
Created December 3, 2014 16:05
typesafe fluent builders are much more convenient now Java has lambdas
// I probably should make a Maven plugin that generates this code.
public final class AddressBuilder {
public interface Street2 {
City street2(String street2);
}
public interface City {
State city(String city);
}
public interface State {