Skip to content

Instantly share code, notes, and snippets.

@jdunne
jdunne / Makefile
Created August 3, 2012 22:04
Makefile for running a command on a bunch input files (*.txt)
.PHONY: clean all
%.out : %.txt
# Substitute real command for cat.
cat $?
touch $@
all : $(patsubst %.txt,%.out,$(wildcard *.txt))
clean:
@jdunne
jdunne / trouble-strategy.markdown
Last active July 9, 2021 20:54
How to win at Trouble.

The winning strategy in Trouble is to maximize the number of pieces that you have in play, while minimizing your opponents' pieces and limiting the exposure of your own pieces to attack.

I think this is the best possible way to accomplish that:

  • If you rolled a 6:
    • If you still have a piece in the home area:
      • Move that piece into the start position.
    • Else if you have a piece in the start position:
  • Move it out of the start position.
interface IAuditor
{
void RecordThatSomethingWasDone();
}
class Service
{
private readonly IAuditor _defaultAuditor = new StandardAuditor();
public void DoSomething(IAuditor auditor = null)
class Service
{
private readonly IAuditor _defaultAuditor = new StandardAuditor();
public void DoSomething()
  {
  DoSomething(_defaultAuditor);
  }
public void DoSomething(IAuditor auditor)
class Service
{
public void DoSomething(IAuditor auditor = null)
  {
  // do something…
  if(auditor != null)
  {
  auditor.RecordThatSomethingWasDone();
  }
  }
class Service
{
class NullAuditor : IAuditor
  {
  void RecordThatSomethingWasDone()
  {
  // does nothing
  }
  }
string uri = configFile.GetValue(“serviceUri”); 
if(uri == null)
{
  uri = “http://example.com/service";
}
ConnectToService(uri);
// One solution is to pass a default value into the method.
string uri = configFile.GetValueOrDefault(“serviceUri”,
“http://example.com/service");
ConnectToService(uri);
// Another possibility is to introduce a Maybe type to hold the return value.
Maybe<string> uri = configFile.GetValue(“serviceUri”); 
if(uri.HasValue)
{
ConnectToService(uri.HasValue ?
@jdunne
jdunne / anagram.py
Created August 5, 2017 02:23
Anagram Finder
"""
anagram.py - Finds anagrams in stdin.
"""
import sys
import collections
anagrams = collections.defaultdict(list)
for line in sys.stdin:
word = line.rstrip()

Keybase proof

I hereby claim:

  • I am jdunne on github.
  • I am johndunne (https://keybase.io/johndunne) on keybase.
  • I have a public key ASCXNWFChX7jLC1xxZIG3UiFVhXwqs2RdlVdq7SvrBDpYAo

To claim this, I am signing this object: