Skip to content

Instantly share code, notes, and snippets.

View benhardy's full-sized avatar

benhardy benhardy

View GitHub Profile
@benhardy
benhardy / spring-di-java-conf-hadoop.java
Created May 17, 2012 22:31
Perform Spring based DI in Hadoop 0.18.x api without XML, and using supplied JobConf.
/**
* say you'd like to have a Hadoop Mapper inject its dependencies via a constructor
* instead of that super annoying post-construction configure call. e.g.
*
* Obviously Hadoop won't be able to instantiate this by itself, but we'll
* help it out below.
*
*/
public class TestMapper implements Mapper<IntWritable, Text, IntWritable, Text> {
private final NumberService numbers; // immutable goodness
@benhardy
benhardy / vagrant-bombing-out.txt
Created October 23, 2012 16:23
vagrant bombing out. hm.
ben@mac:~/scala/lascale> vagrant box add lascale https://dl.dropbox.com/u/4080054/la-scale.box
[vagrant] Downloading with Vagrant::Downloaders::HTTP...
[vagrant] Downloading box: https://dl.dropbox.com/u/4080054/la-scale.box
[vagrant] Extracting box...
[vagrant] Cleaning up downloaded box...
/Applications/Vagrant/embedded/gems/gems/archive-tar-minitar-0.5.2/lib/archive/tar/minitar.rb:493:in `read': undefined method `size' for nil:NilClass (NoMethodError)
from /Applications/Vagrant/embedded/gems/gems/archive-tar-minitar-0.5.2/lib/archive/tar/minitar.rb:740:in `block (2 levels) in extract_entry'
from /Applications/Vagrant/embedded/gems/gems/archive-tar-minitar-0.5.2/lib/archive/tar/minitar.rb:739:in `loop'
from /Applications/Vagrant/embedded/gems/gems/archive-tar-minitar-0.5.2/lib/archive/tar/minitar.rb:739:in `block in extract_entry'
from /Applications/Vagrant/embedded/gems/gems/archive-tar-minitar-0.5.2/lib/archive/tar/minitar.rb:738:in `open'
@benhardy
benhardy / Person.scala
Created November 17, 2012 17:07
Person model
/**
* an example model
*/
case class Person(@BeanProperty id: Int,
@BeanProperty name: String,
@BeanProperty phone: String,
@BeanProperty dob: DateMidnight) {
}
@benhardy
benhardy / demo-fragment.html
Created November 18, 2012 02:18
fragment of demo.jsp
<html>
<body>
<p>
Person:
<p>
ID: ${person.id} <br>
Name: ${person.name} <br>
DOB: ${person.dob} <br>
Phone: ${person.phone} <br>
<p>
@benhardy
benhardy / PersonController.scala
Created November 18, 2012 02:25
the Person controller
package skellybones.web.controller
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation._
import org.springframework.web.servlet.ModelAndView
import org.springframework.beans.factory.annotation.Autowired
import skellybones.service.PersonService
import skellybones.web.ResourceNotFoundException
import skellybones.bo.Person
@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()
@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 / 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
public List<String> randomDictionaryWords() throws IOException {
try (InputStream is = new FileInputStream(new File("/usr/share/dict/words"))) {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
return br.lines()
.filter(line -> line.length() >= MIN_WORD_LENGTH && line.length() <= MAX_WORD_LENGTH)
.filter(word -> word.equals(word.toLowerCase()))
.filter(word -> random() < DICTIONARY_SAMPLING_RATE)
.limit(50)
.collect(Collectors.toList());