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) }
@benhardy
benhardy / spheres.scala
Last active December 29, 2015 22:29
little functional raytracer. it may be longer that the "35 lines of javascript" one but is actually readable. (albeit in need of cleanup, optimizing, etc)
/**
* See bottom for file for example spheres scene
*/
import java.awt.Color
import java.awt.image.BufferedImage
import java.io.File
import javax.imageio.ImageIO
import math.sqrt
import math.min
import math.max
@benhardy
benhardy / broc.pov
Last active December 16, 2015 11:18
Generate a broccoli using POV-Ray's macros, which can be recursive.
// Generate a broccoli like structure
#include "colors.inc"
#include "textures.inc"
light_source {
<-100, 400,-200>
color <0.8,0.8,0.8>
area_light <50,0,0>, <0,0,50>, 5, 5
adaptive 1
jitter
@benhardy
benhardy / WebConfig.scala
Created November 18, 2012 03:22
Spring/Scala web config
@Configuration("springConf")
@EnableWebMvc
@ComponentScan(Array("skellybones"))
@PropertySource(Array("classpath:/app.properties", "${runtime.properties.file}"))
class WebConfig {
@Autowired
var environment: Environment = null
/**
@benhardy
benhardy / WebAppInit.scala
Created November 18, 2012 03:38
webapp config
/**
* Our instance of WebApplicationInitializer. This replaces web.xml for the initial
* configuration of the servlet.
*/
class WebAppInit extends WebApplicationInitializer {
override def onStartup(servletContext: ServletContext) {
// Create the 'root' Spring application context
val root = new AnnotationConfigWebApplicationContext()