Skip to content

Instantly share code, notes, and snippets.

Avatar

benhardy benhardy

View GitHub Profile
View shiny-siney.cl
#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 Nov 7, 2017
A little experiment in parsing English and answering questions
View TheOracle.scala
/** 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
View thing.demo
# 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
View PhaserDemo.scala
/**
* 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.
*/
View Either.java
/**
* 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) {
View java8FunctionAdapters.scala
// 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) }
View gist:9b3e80a30ba18f8c86bd
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 Aug 29, 2015
In case you thought the Monty Hall Problem hadn't been done to death.
View MontyHallSimulationTest.java
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;
View singularity-resource-usage-report.sh
#!/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 Dec 3, 2014
typesafe fluent builders are much more convenient now Java has lambdas
View j8builder.java
// 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 {