Skip to content

Instantly share code, notes, and snippets.

View benhardy's full-sized avatar

benhardy benhardy

View GitHub Profile
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());
@benhardy
benhardy / keybase.md
Created May 27, 2014 21:55
keybase.md

Keybase proof

I hereby claim:

  • I am benhardy on github.
  • I am benhardy (https://keybase.io/benhardy) on keybase.
  • I have a public key whose fingerprint is F313 68B9 32AA 2C12 E021 D863 0A6F DC0E DF9F EB9C

To claim this, I am signing this object:

@benhardy
benhardy / bootstrap.sh
Created June 24, 2014 05:53
docker startup script for benhardy/ubuntu-java8
#!/usr/bin/env sh
TARBALL_URL=$1
APP_NAME=$2
echo "Cleaning up any previous $APP_NAME directory"
rm -rf $APP_NAME
echo "curling $TARBALL_URL -> $APP_NAME.tgz"
curl $TARBALL_URL -o $APP_NAME.tgz
@benhardy
benhardy / kern.log
Created August 22, 2014 16:44
shonky kern.log
Aug 21 00:14:10 ci-na-frontdoor-03 kernel: imklog 5.8.6, log source = /proc/kmsg started.
Aug 21 00:14:10 ci-na-frontdoor-03 kernel: [ 0.000000] Initializing cgroup subsys cpuset
Aug 21 00:14:10 ci-na-frontdoor-03 kernel: [ 0.000000] Initializing cgroup subsys cpu
Aug 21 00:14:10 ci-na-frontdoor-03 kernel: [ 0.000000] Linux version 3.2.0-67-generic (buildd@brownie) (gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) ) #101-Ubuntu SMP Tue Jul 15 17:46:11 UTC 2014 (Ubuntu 3.2.0-67.101-generic 3.2.60)
Aug 21 00:14:10 ci-na-frontdoor-03 kernel: [ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-3.2.0-67-generic root=UUID=02102d06-ff39-4079-899f-c3ec91691ea3 ro quiet splash
Aug 21 00:14:10 ci-na-frontdoor-03 kernel: [ 0.000000] KERNEL supported cpus:
Aug 21 00:14:10 ci-na-frontdoor-03 kernel: [ 0.000000] Intel GenuineIntel
Aug 21 00:14:10 ci-na-frontdoor-03 kernel: [ 0.000000] AMD AuthenticAMD
Aug 21 00:14:10 ci-na-frontdoor-03 kernel: [ 0.000000] Centaur CentaurHauls
Aug 21 00:14:10 ci
@benhardy
benhardy / keybase.md
Created September 25, 2014 05:58
keybase proof

Keybase proof

I hereby claim:

  • I am benhardy on github.
  • I am benhardy (https://keybase.io/benhardy) on keybase.
  • I have a public key whose fingerprint is E6EC 21D8 301F 8F48 375C D6C7 F32A 2090 4F46 ED42

To claim this, I am signing this object:

public static <T> Iterable<T> asIterable(final Enumeration<T> anotherHorribleEnumeration) {
return () -> new Iterator<T>() {
@Override
public boolean hasNext() {
return anotherHorribleEnumeration.hasMoreElements();
}
@Override
public T next() {
return anotherHorribleEnumeration.nextElement();
@benhardy
benhardy / horrorofhorrors.java
Last active August 29, 2015 14:09
java 8 enumeration as stream
public static <T> Stream<T> asStream(final Enumeration<T> horror) {
if (horror.hasMoreElements()) {
return Stream.concat(Stream.of(horror.nextElement()), asStream(horror));
} else {
return Stream.empty();
}
}
@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 {
#!/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 / 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;